解密 java.RSA 加密字符串中的 RSA Base64 编码字符串由 c#.Net 生成时,我遇到了一些问题。
实际上,我使用 java 创建了一个公钥和私钥。然后我将公钥交换给 .Net Team。他们通过使用公钥和RSACryptoServiceProvider
类来加密字符串。
.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");
但是,它给出了垃圾字符,我无法将其与我想要的纯文本联系起来。我想,我想在某个地方做某事。请指导我。