获得不同的RSA加密导致不同的语言?

时间:2014-11-05 06:42:28

标签: java c# encryption rsa sha512

我正在尝试使用RSA公钥加密一些数据并使用SHA-512算法进行签名。但收到的回复在不同的平台上是不同的 形成。

在C#中:

    RSACryptoServiceProvider crypto = new RSACryptoServiceProvider();
    crypto.ImportCspBlob(Convert.FromBase64String(publickey));
    crypto.exportParameters(false); // and got the public key modulus and exp
    byte[] response = crypto.SignData(data, "SHA512");

在Java中:

    // got modulus and exp for public key from c# 
    byte[] modulo = {.....};         
    byte[] exp = {1,0,1};                
    BigInteger modulus = new BigInteger(1, modulo);     
    BigInteger pubExp = new BigInteger(1, exp);     
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec priKeySpec = new RSAPublicKeySpec(modulus, pubExp);
    RSAPublicKey Key = (RSAPublicKey)keyFactory.generatePublic(priKeySpec);

    // Calculate Hash
    MessageDigest sha1 = MessageDigest.getInstance("SHA-512");
    byte[] digest = sha1.digest(data);     
    // Encrypt digest
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, Key);
    byte[] response = cipher.doFinal(digest);

但两个响应都不匹配.C#生成正确的但是java不生成相同的byte [] java代码中缺少任何部分。

1 个答案:

答案 0 :(得分:0)

私有静态最终String算法=“ AES / CBC / NOPadding”; 如果我们转换类型中国传统的语言,那么当我们加密时,我们会得到空值。为了克服这个问题,我们使用以下方法。  私有静态最终String算法=“ AES / CBC / PKCS5Padding”; 如果我们使用像中国传统的converion这样的不同类型的语言,则会被加密。