RSA加密/解密问题:.Net中的加密文本未在Java中解密
||
我在Java解密中遇到问题。以下是遇到的错误:
javax.crypto.BadPaddingException: Blocktype mismatch: -127
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:311)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:255)
文本在.Net中使用以下代码加密:
public string EncryptString( string inputString, int dwKeySize, string xmlString )
{
RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider( dwKeySize );
rsaCryptoServiceProvider.FromXmlString( xmlString );
int keySize = dwKeySize / 8;
byte[] bytes = Encoding.UTF32.GetBytes( inputString );
int maxLength = keySize - 42;
int dataLength = bytes.Length;
int iterations = dataLength / maxLength;
StringBuilder stringBuilder = new StringBuilder();
for( int i = 0; i <= iterations; i++ )
{
byte[] tempBytes = new byte[ ( dataLength - maxLength * i > maxLength ) ? maxLength : dataLength - maxLength * i ];
Buffer.BlockCopy( bytes, maxLength * i, tempBytes, 0, tempBytes.Length );
byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt( tempBytes, true );
Array.Reverse( encryptedBytes );
stringBuilder.Append( Convert.ToBase64String( encryptedBytes ) );
}
return stringBuilder.ToString();
}
用于解密的代码是JAVA是:
PrivateKey privKey = readPrivateKey(); // reads the private key
Cipher cipher = Cipher.getInstance(\"RSA/ECB/PKCS1Padding\");
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] encryptedBytes = Base64.decodeBase64(encryptedText.getBytes(\"UTF-32\"));
encryptedBytes = reverse(b); // reverse the bytes
byte[] decrypted = cipher.doFinal(encryptedBytes);
return new String(decrypted);
我在这里想念什么吗?如何进行双向加密/解密?
没有找到相关结果
已邀请:
2 个回复
钨蜡唤喉晤
博沮富教全