RSA加密/解密问题:.Net中的加密文本未在Java中解密 [英] RSA Encryption/Decryption Issue: Encrypted Text from .Net not decrypting in Java

查看:107
本文介绍了RSA加密/解密问题:.Net中的加密文本未在Java中解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java解密中遇到问题.以下是遇到的错误:

I am facing issues in decryption in Java. Following is the error encountered:

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中的文本使用以下代码加密:

The text is encrypted in .Net with the following code:

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是:

The code for decrytion is JAVA is:

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);

我在这里错过了什么吗?如何进行双向加密/解密?

Am I missing something here? How can I make both way encryption/decryption?

推荐答案

这可能有帮助:

http://jafetsanchez.com/post/7562162652/rijndael-on-c-and-java

这篇关于RSA加密/解密问题:.Net中的加密文本未在Java中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆