RSA使用SpongyCastle [英] RSA using SpongyCastle
问题描述
在Android应用程序中,我正在尝试模仿此命令的执行使用 SpongyCastle库和标准的 java.security
libs:
echo'test'| openssl rsautl -encrypt -pubin -inkey test.pub | base64> encrypted_file
应该注意,命令中的/从文件的读/写是不是将被实现,我有我的公钥(即 test.pub
)作为Base64编码字符串 base64key
在我的代码。
我尝试过以下操作,但确定它不起作用:
static {
Security.insertProviderAt(new BouncyCastleProvider(),1);
}
//...更多代码这里
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.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE,publicKey);
byte [] cipherText = cipher.doFinal(testToByte);
String encrypted = Base64.encode((new String(cipherText,UTF8)。toString()。getBytes()))
我知道这是关闭的,但不知道在哪里转。任何帮助将不胜感激。
最终使用以下方法解决:
private void stripHeaders(){
public_key = public_key.replace(----- BEGIN PUBLIC KEY -----,);
public_key = public_key.replace(----- END PUBLIC KEY -----,);
}
public byte [] encryptWithPublicKey(String encrypt)throws异常{
byte [] message = encrypt.getBytes(UTF-8);
stripHeaders();
PublicKey apiPublicKey = getRSAPublicKeyFromString();
密码rsaCipher = Cipher.getInstance(RSA /无/ PKCS1Padding,SC);
rsaCipher.init(Cipher.ENCRYPT_MODE,apiPublicKey);
return rsaCipher.doFinal(message);
}
private PublicKey getRSAPublicKeyFromString()throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance(RSA,SC);
byte [] publicKeyBytes = Base64.decode(public_key.getBytes(UTF-8),Base64.DEFAULT);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
return keyFactory.generatePublic(x509KeySpec);
}
My knowledge of encryption is very basic, so apologies for any ignorance on my part.
Within an Android app I am currently trying to mimic the execution of this command using the SpongyCastle library and standard java.security
libs:
echo 'test' | openssl rsautl -encrypt -pubin -inkey test.pub | base64 > encrypted_file
It should be noted that the reading/writing to and from files in the command are not going to be implemented and I have my public key (i.e. test.pub
) as a Base64 encoded string base64key
in my code.
I have attempted the following but am certain it does not work:
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()))
I know this is way off, but am not sure where to turn. Any help would be appreciated.
This was eventually solved using the following methods:
private void stripHeaders(){
public_key = public_key.replace("-----BEGIN PUBLIC KEY-----", "");
public_key = public_key.replace("-----END PUBLIC KEY-----", "");
}
public byte[] encryptWithPublicKey(String encrypt) throws Exception {
byte[] message = encrypt.getBytes("UTF-8");
stripHeaders();
PublicKey apiPublicKey= getRSAPublicKeyFromString();
Cipher rsaCipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
rsaCipher.init(Cipher.ENCRYPT_MODE, apiPublicKey);
return rsaCipher.doFinal(message);
}
private PublicKey getRSAPublicKeyFromString() throws Exception{
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "SC");
byte[] publicKeyBytes = Base64.decode(public_key.getBytes("UTF-8"), Base64.DEFAULT);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes);
return keyFactory.generatePublic(x509KeySpec);
}
这篇关于RSA使用SpongyCastle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!