使用不同的语言获得不同的RSA加密

本文关键字:RSA 加密 语言 | 更新日期: 2023-09-27 18:29:14

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

在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代码中缺少的任何部分。

使用不同的语言获得不同的RSA加密

private static final String algorithm="AES/CCB/NOPPadding";如果我们转换类型中國傳統的语言,然后我们在加密时得到空值。为了克服这个问题,我们使用下面的东西。private static final String algorithm="AES/CCB/PKCS5Padding";如果我们使用不同类型的语言中國傳統的converion可以被加密。