使用RSA/ECB/PKCS1Padding进行.NET加密和Java解密

4

我们有一段已经正常工作的Java加密代码。我尝试在.NET中创建相同的加密方法,但是解密时Java报错“bad padding exception(错误填充异常)”,请看下面的代码细节:


正常工作的Java代码:

private static byte[] doThis(String message) {
    byte[] messageCrypte = null;
       try {
        // Certificate Input Stream
        // LA SSL Certificate to be passed.
        InputStream inStream = new FileInputStream(certificate);

        // X509Certificate created
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);
        inStream.close();

        // Getting Public key using Certficate
        PublicKey rsaPublicKey = (PublicKey) cert.getPublicKey();

        Cipher encryptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunJCE");
        encryptCipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);

        byte[] messageACrypter = message.getBytes();
        // Encrypted String
        messageCrypte = encryptCipher.doFinal(messageACrypter);
       } catch (Exception e) {
        // TODO: Exception Handling
        e.printStackTrace();
       }
    return messageCrypte;
}

我正在尝试使用等效的c# .Net代码,但从java解密代码中得到了错误填充异常。

    static byte[] doThis(string message)
    {
        X509Certificate cert = new X509Certificate(@"C:\Data\abc-rsa-public-key-certificate.cer");
        byte[] aa = cert.GetPublicKey();

        RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
        RSAParameters RSAKeyInfo = new RSAParameters();
        byte[] Exponent = { 1, 0, 1 };

        RSAKeyInfo = RSA.ExportParameters(false);
        //Set RSAKeyInfo to the public key values. 
        RSAKeyInfo.Modulus = aa;
        //RSAKeyInfo.Exponent = Exponent;
        RSA.ImportParameters(RSAKeyInfo);
        byte[] bb = RSA.Encrypt(GetBytes(message), false);
        return bb;
    }

Java解密代码

private String getDecryptedString(byte[] credentials, PrivateKey secretKey) throws NoSuchAlgorithmException,
            NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
            BadPaddingException {
        String decryptedString;
        Cipher decryptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "SunJCE");
        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] messageDecrypte = decryptCipher.doFinal(credentials);
        decryptedString = new String(messageDecrypte);
        return decryptedString;
    }

1
你确定(= 你_知道_).net/C#使用相同的填充吗? - Fildor
1
message.getBytes(); <--- 绝对不要这样做。你无法控制用于获取字符串字节的字符集。这可能是问题的根源 - 找出在你的 C# 代码中等效的 GetBytes 方法使用了什么字符集,并且在你的 Java 代码中强制执行它 - Duncan Jones
我不确定这个问题:“你确定(=你知道).net/C#使用相同的填充方式吗?”有一个网站建议尝试这个选项,但是并没有起作用。 - user2791316
大家好,这个有人能帮忙吗? - user2791316
正确答案: public static string EncrypIt(string inputString, X509Certificate2 cert) { RSACryptoServiceProvider rsaservice = (RSACryptoServiceProvider)cert.PublicKey.Key; byte[] plaintext = Encoding.UTF8.GetBytes(inputString); byte[] ciphertext = rsaservice.Encrypt(plaintext, false); string cipherresult = Convert.ToBase64String(ciphertext); return cipherresult;
}
- user2791316
显示剩余3条评论
1个回答

1
这是 .NET 代码:

public static string EncrypIt(string inputString, X509Certificate2 cert)
{
    RSACryptoServiceProvider rsaservice = (RSACryptoServiceProvider)cert.PublicKey.Key;
    byte[] plaintext = Encoding.UTF8.GetBytes(inputString);
    byte[] ciphertext = rsaservice.Encrypt(plaintext, false);
    string cipherresult = Convert.ToBase64String(ciphertext);
    return cipherresult;                 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接