簡體   English   中英

使用RSA / ECB / PKCS1Padding進行.Net加密和Java解密

[英].Net encryption and java decryption with RSA/ECB/PKCS1Padding

我們在java中有現有的加密代碼,並且工作得很好。我正在嘗試在.net中創建相同的加密方法,這是失敗的java解密方法說錯誤的填充異常。 請參閱下面的代碼詳細信息:使用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;
    }

這是.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;                 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM