【发布时间】:2014-03-11 04:56:13
【问题描述】:
我的加密知识非常基础,因此对我的任何无知表示歉意。
在 Android 应用程序中,我目前正在尝试使用 SpongyCastle library 和标准 java.security 库来模拟此命令的执行:
echo 'test' | openssl rsautl -encrypt -pubin -inkey test.pub | base64 > encrypted_file
应该注意的是,命令中的文件读取/写入不会实现,我将我的公钥(即test.pub)作为Base64编码字符串@ 987654325@ 在我的代码中。
我尝试了以下方法,但确定它不起作用:
static {
Security.insertProviderAt(new BouncyCastleProvider(), 1);
}
//...more code here
byte[] pka = Base64.decode(base64key);
X509EncodedKeySpec x = new X509EncodedKeySpec(pka);
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(x);
byte[] testToByte = "test".getBytes("UTF8");
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = cipher.doFinal(testToByte);
String encrypted = Base64.encode((new String(cipherText, "UTF8").toString().getBytes()))
我知道这很遥远,但不知道该转向哪里。任何帮助,将不胜感激。
【问题讨论】:
标签: java android encryption spongycastle