在解密 java 中的 RSA Base64 编码字符串时,我遇到一些问题。RSA 加密字符串是由 c#.Net 生成的。
实际上,我使用java创建了公钥和私钥。然后我将公钥交换给.Net Team。他们使用公钥加密了一个字符串RSACryptoServiceProvider
class.
.Net代码:
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize);
rsa.FromXmlString(publicKey);
.......
.......
byte[] encryptedBytes = rsa.Encrypt(tempBytes, false);
Array.Reverse(encryptedBytes);
stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
Java解密代码:
public static void doDecrypt( BigInteger modules, BigInteger d , String encrypted )
{
try {
byte[] decodedBytes = Base64.decodeBase64( encrypted );
KeyFactory factory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA");
RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d);
PrivateKey privKey = factory.generatePrivate(privSpec);
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] decrypted = cipher.doFinal(decodedBytes) ;
System.out.println("decrypted: " + new String(decrypted));
}
catch (Exception e) {
e.printStackTrace();
}
}
解密字符串时,它显示以下错误。
javax.crypto.BadPaddingException: Data must start with zero
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:308)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:255)
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
然后,我尝试了
Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
但是,它给出了垃圾字符,我无法将其与我想要的纯文本联系起来。
我想,我在某个地方缺少做某事。请指导我。