Sun*_*nil -1 java encryption rsa
我正在使用JAVA我的朋友使用SYMBIAN
我和我的朋友有相同的rsa模数.如果我使用公钥加密数据,那么我的朋友能够解密相同的数据.但如果我的朋友使用公钥加密数据,那么我无法解密数据.我收到一个错误,因为"数据必须从零开始"
public static byte[] encrypt(byte[] encrptdByte) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
byte[] encryptionByte = null;
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptionByte = cipher.doFinal(encrptdByte);
return encryptionByte;
}
public static void decrypt(byte[] encrptdByte) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
byte[] encryptionByte = null;
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
encryptionByte = cipher.doFinal(encrptdByte);
System.out.println("Recovered String ::: " + new String(encryptionByte));
}
Run Code Online (Sandbox Code Playgroud)
谢谢Sunil
该decrypt功能使用publicKey- 它来自哪里?请注意,使用公钥加密的数据必须使用相应的私钥解密,而不能使用相同的公钥解密.诸如RSA之类的非对称加密具有密钥对的概念,其中该对中的每个密钥可以解密用另一密钥加密的数据,这与诸如AES的对称加密相反,其中相同的密钥用于加密和解密.