简体   繁体   English

C#中的对称加密类似于JAVA

[英]symmetric encryption in c# resembles JAVA

private static byte[] encryptData(ByteArrayOutputStream data, byte[] symmetricKey) throws EncryptionException {
        try {
            SecretKey secKey = new SecretKeySpec(symmetricKey, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secKey);
            return cipher.doFinal(data.toByteArray());
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException |
                InvalidKeyException |
                BadPaddingException e) {
            throw new EncryptionException(e);
        }
    }

I have a situation where I need to encrypt data using .NET and decrypt the same data using JAVA. 我遇到需要使用.NET加密数据并使用JAVA解密相同数据的情况。 Essentially, I need to rewrite the above encryption method in .NET. 本质上,我需要在.NET中重写上述加密方法。

public byte[] Encrypt(byte[] key, byte[] plainText)
        {
            using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
            {
                using (ICryptoTransform encryptor = aesProvider.CreateEncryptor(key, magicIV))
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                        {
                            cs.Write(plainText, 0, plainText.Length);
                        }
                        byte[] cipherText = ms.ToArray();
                        return cipherText;
                    }
                }
            }
        }

The above code I used somewhere mandates IV which JAVA is not asking for. 我在某处使用的上述代码强制执行JAVA不需要的IV。 What is the IV used in JAVA code? JAVA代码中使用的IV是什么?

I tried many links which didn't work. 我尝试了许多无效的链接。 Symmetric Encryption between .NET and Java .NET和Java之间的对称加密

Please help 请帮忙

If your current Java decryption code also does not ask for an IV (and your decryption returns the same data you encrypted) then Cipher.getInstance("AES") is returning an object using the ECB block mode. 如果您当前的Java解密代码也没有要求IV(并且您的解密返回了您加密的相同数据),则Cipher.getInstance("AES")将使用ECB块模式返回一个对象。

.NET symmetric algorithms default to the CBC block mode, which requires an IV. .NET对称算法默认为CBC块模式,这需要IV。

You have a couple of options: 您有两种选择:

  • Set aesProvider.Mode = CipherMode.ECB before calling CreateEncryptor . 在调用CreateEncryptor之前,设置aesProvider.Mode = CipherMode.ECB
  • Pass aesProvider.IV to the IV parameter of CreateEncryptor . aesProvider.IV传递给CreateEncryptor的IV参数。 The IV property will make a cryptographically random value on the first read if it's not set. 如果未设置IV属性,则在第一次读取时将使其成为密码随机值。
    • You will need to pass this data to the decryption routine, which should then use "AES/CBC/PKCS5Padding", and set the IV however one does that in Java. 您将需要将此数据传递给解密例程,然后该解密例程应使用“ AES / CBC / PKCS5Padding”并设置IV,但是用Java可以做到这一点。
    • One common method of transport is to simply prepend the data to the ciphertext, then just pick off the first 16 bytes at decryption time. 一种常见的传输方法是将数据简单地放在密文之前,然后在解密时选择前16个字节。
    • DO NOT use a fixed value for an IV, because it's then almost the same as ECB. 不要对IV使用固定值,因为它与ECB几乎相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM