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

问题描述 投票:1回答:1

我正在尝试使用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不生成相同的字节[] java代码中缺少任何部分。

java c# encryption rsa sha512
1个回答
0
投票

private static final String algorithm =“AES / CBC / NOPadding”;如果我们转换类型中国传统的语言,那么当我们加密时我们得到空值。为了克服这个问题,我们使用下面的东西。 private static final String algorithm =“AES / CBC / PKCS5Padding”;如果我们做不同类型的语言,如中国传统的转换可以加密。

© www.soinside.com 2019 - 2024. All rights reserved.