在Java中的C#和解密使用RSA加密时填充错误 [英] Padding error when using RSA Encryption in C# and Decryption in Java

查看:1250
本文介绍了在Java中的C#和解密使用RSA加密时填充错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用的Java时解密Base64编码的RSA加密,这是在C#中取得字符串收到以下错误:

Currently I am receiving the following error when using Java to decrypt a Base64 encoded RSA encrypted string that was made in C#:

使用javax .crypto.BadPaddingException:不PKCS#1块类型2或补零

javax.crypto.BadPaddingException: Not PKCS#1 block type 2 or Zero padding

从.NET和Java是交换之间的安装过程从提取的PEM文件创建在.NET密钥存储私钥,然后完成,创建使用密钥工具来创建一个JKS版本的私钥。 Java的加载已经创建JKS和Base64编码字符串转换成字节数组进行解码,然后使用私钥解密。

The setup process between the exchange from .NET and Java is done by creating a private key in the .NET key store then from the PEM file extracted, created use keytool to create a JKS version with the private key. Java loads the already created JKS and decodes the Base64 string into a byte array and then uses the private key to decrypt.

下面是我在C#创建的代码加密字符串:

Here is the code that I have in C# that creates the encrypted string:

public string Encrypt(string value) {
    byte[] baIn = null;
    byte[] baRet = null;
    string keyContainerName = "test";

    CspParameters cp = new CspParameters();
    cp.Flags = CspProviderFlags.UseMachineKeyStore;
    cp.KeyContainerName = keyContainerName;
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);

    // Convert the input string to a byte array 
    baIn = UnicodeEncoding.Unicode.GetBytes(value);

    // Encrypt
    baRet = rsa.Encrypt(baIn, false);

    // Convert the encrypted byte array to a base64 string
    return Convert.ToBase64String(baRet);
}

下面是我用Java进行解密输入的字符串代码:

Here is the code that I have in Java that decrypts the inputted string:

public void decrypt(String base64String) {
    String keyStorePath = "C:\Key.keystore";
    String storepass = "1234";
    String keypass = "abcd";
    byte[] data = Base64.decode(base64String);
    byte[] cipherData = null;

    keystore = KeyStore.getInstance("JKS");
    keystore.load(new FileInputStream(keyStorePath), storepass.toCharArray());

    RSAPrivateKey privateRSAKey = (RSAPrivateKey) keystore.getKey(alias, keypass.toCharArray());

    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(Cipher.DECRYPT_MODE, privateRSAKey);
    cipherData = cipher.doFinal(data);

    System.out.println(new String(cipherData));
}



有谁看到一个步骤中缺少或者需要改变填充或项目?我做阅读本网站和其它几个小时,但还没有真正找到一个具体的解决方案。

Does anyone see a step missing or where the padding or item needs to be changed? I have done hours of reading on this site and others but haven't really found a concrete solution.

你的帮助是极大的赞赏。

You're help is vastly appreciated.

感谢。 -Matt

推荐答案

检查是否已正确交换的关键。

Check that you have correctly exchanged the key.

试图解密使用不正确的密钥是从解密严重填充的数据没有什么区别。

Trying to decrypt with an incorrect key is indistinguishable from decrypting badly padded data.

这篇关于在Java中的C#和解密使用RSA加密时填充错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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