我正在尝试用Java编写RSA加密和解密类,用于客户端来回传递字符串的服务器。我有类的以下代码:
NSApplication.sharedApplication()
我有一个测试类的代码:
public class RSAEncryption {
public static final String KEYGENALGORITHM = "RSA";
public static final String ALGORITHM = "RSA/ECB/PKCS1Padding";
public static KeyPairContainer generateKey() {
KeyPairGenerator keyGen;
KeyPair key;
try {
keyGen = KeyPairGenerator.getInstance(KEYGENALGORITHM);
keyGen.initialize(1024);
key = keyGen.generateKeyPair();
return new KeyPairContainer(key.getPublic(), key.getPrivate());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.out.println("Error: No such algorithm");
}
return null;
}
public static String pubkeyToString(PublicKey key){
byte[] array = key.getEncoded();
BASE64Encoder encoder = new BASE64Encoder();
String tempstring = encoder.encode(array);
return tempstring;
}
public static PublicKey stringToPubKey(String string){
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] array = decoder.decodeBuffer(string);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(array);
KeyFactory keyFact = KeyFactory.getInstance(KEYGENALGORITHM);
PublicKey pubKey = keyFact.generatePublic(x509KeySpec);
return pubKey;
} catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return null;
}
public static byte[] rSAencrypt(byte[] plaintext, String keystring) {
Cipher cipher;
try {
PublicKey key = stringToPubKey(keystring);
cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = cipher.doFinal(plaintext);
//String cipherText = new String(cipherTextbytes);
return cipherText;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static byte[] rSAdecrypt(byte[] ciphertext, PrivateKey key){
Cipher cipher;
try {
//byte[] ciphertext = ciphertextstring.getBytes();
cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedText = cipher.doFinal(ciphertext);
return decryptedText;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
但是,当我尝试运行测试类时,密钥生成和加密工作正常,但是当我尝试解密时,我抛出了一个错误。错误是javax.crypto.IllegalBlockSizeException:数据不能超过128个字节,但是,我的数据肯定少于128个字节。
我可以确认将公钥转换为字符串并返回相同的公钥。我也尝试使用字节测试代码,这很好。该错误显然是在尝试解密从字符串中获取的字节。
有没有办法解密从字符串中取出的字节而不会抛出此错误?在此先感谢,非常感谢。
编辑:我想我可能已经解决了这个问题。我试图将加密的bytearray转换为字符串,然后从中提取字节,但加密的bytearray无法正确转换为字符串,所以当我得到字节时,它不会提取原始加密的bytearray是什么。这是正确的,如果是这样,我如何正确地将bytearray转换为字符串,以便我们可以交换它?答案 0 :(得分:1)
如果你这样做
byte[] ciphertext = RSAEncryption.rSAencrypt(plaintext.getBytes(), pubKeytext);
//String ciphertextString = new String(ciphertext);
//System.out.println(ciphertextString);
PrivateKey privkey = keyPair.getPrivateKey();
byte[] decryptedText = RSAEncryption.rSAdecrypt(ciphertext, privkey);
代码运行得非常好。您不能将字节数组转换为字符串,因为字节转换为字符并返回字节(使用getBytes()) - 具体取决于您的默认字符集。 new String(ciphertext)
删除了不可打印的字符,这些字符会改变密文,从而使明文无法恢复。 (感谢Artjom B.指出这一点。)
只需使用Base64或二进制文件来传输密文,例如:
byte[] ciphertext = RSAEncryption.rSAencrypt(plaintext.getBytes(), pubKeytext);
String ciphertextString = Base64.toBase64String(ciphertext);
System.out.println(ciphertextString);
PrivateKey privkey = keyPair.getPrivateKey();
byte[] decryptedText = RSAEncryption.rSAdecrypt(Base64.decode(ciphertextString), privkey);
(我在这里使用BouncyCastle Base64编码器。)