在C#中解密AES加密字符串

时间:2016-01-07 19:28:40

标签: java c# encryption aes aescryptoserviceprovider

我正在尝试用C#解密来自Java的AES加密字符串。当我解密时,它返回乱码并且与通过Java代码加密的原始纯文本不匹配。请指导我这里出了什么问题。

附加用于加密的Java代码和用于解密的C#代码。如果您需要更多详细信息,请告诉我。

我也尝试了AesCryptoServiceProvider,它也没有用。您可以在C#中的注释代码中看到代码。

请注意,我只能对我的C#代码进行更改以匹配Java代码,并且无法对Java端进行任何编辑。

加密的Java代码:

/** encrypt cipher */
private static final Cipher ENCRYPT_CIPHER = generateCipher(Cipher.ENCRYPT_MODE);

private static String ENCRYPT_KEY = "key";

/**
 * @param val
 * @return encrypted value
 * @throws Exception
 */
public String encrypt(final String val) throws Exception {
    return new String(Base64.encodeBase64(ENCRYPT_CIPHER.doFinal(val.getBytes()), true)).toString();
}

/**
 * @param encrypt
 * @return cipher
 */
protected static Cipher generateCipher(final int encrypt) {
    try {
        final Cipher cipher = Cipher.getInstance("AES");
        cipher.init(encrypt, SecretKeyFactory.getInstance("AES").generateSecret(new IBMAESKeySpec(Base64.decodeBase64(ENCRYPT_KEY.getBytes()))));
        return cipher;
    } catch (final Exception e) {
        return null;
    }
}

用于解密的C#代码:

private static String ENCRYPT_KEY = "key";
public String decodeString (String encodedStr)
{

/*using (var aesCryptoProvider = new AesCryptoServiceProvider())
    {
    aesCryptoProvider.BlockSize = 128;
    aesCryptoProvider.KeySize = 256;
    aesCryptoProvider.Key = Convert.FromBase64String(ENCRYPT_KEY.ToString());
    aesCryptoProvider.Padding = PaddingMode.Zeros;
    aesCryptoProvider.Mode = CipherMode.ECB;

    using (var decryptor = aesCryptoProvider.CreateDecryptor())
    using (var memoryStream = new MemoryStream(Convert.FromBase64String(encodedStr)))
    using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
    using (var streamReader = new StreamReader(cryptoStream, Encoding.UTF8))
    {
        decodedStr = streamReader.ReadToEnd();
    }
    } 
 */
    using (AesManaged aesAlg = new AesManaged())
        {
        aesAlg.Key = Convert.FromBase64String(ENCRYPT_KEY.ToString()); ;
        aesAlg.BlockSize = 128;
        aesAlg.KeySize = 256;
        aesAlg.Mode = CipherMode.ECB;
        aesAlg.Padding = PaddingMode.Zeros;
        // Create a decrytor to perform the stream transform.
        ICryptoTransform decryptor = aesAlg.CreateDecryptor();

        // Create the streams used for decryption.
        using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(encodedStr)))
        {
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
            {

                // Read the decrypted bytes from the decrypting stream
                // and place them in a string.
                decodedStr = srDecrypt.ReadToEnd();
            }
            }
        }

     }
}

1 个答案:

答案 0 :(得分:0)

这只是一个快速的答案。 Haven没有对它做过大量的研究,但是你有没有检查过是否匹配了endian-ness?看起来C#(.NET)是little-endian,但JVM是big-endian。我不确定它是否将其交换为网络传输(然后它只是匹配硬件)。只是一个想法。如果我发现任何其他内容,我会更新我的答案。