C and Java AES encryption and decryption

Reference documents: https://www.cnblogs.com/xbzhu/p/7064642.html

A few days ago, when docking with java interface, C ා encryption parameter is required, Java decrypts. However, a lot of similar encryption and decryption methods are found on the Internet, which are inconsistent with the encrypted text of Jaca. Java interface can't decrypt until you see the second method linked above. The normal decryption of Java encrypted ciphertext shows that this method is effective, but here only decryption is needed. What I need is an encryption method (I'm used to it). I can't read the code to see how to decrypt it. I happen to see that CreateDecryptor changes it and tries to decrypt it completely in line with Java! Thank you!

This is the Demo written by the blogger of the reference document: https://github.com/zhu-xb/AES-Cryptography

    /// <summary>
    /// AES Decrypt
    /// </summary>
    /// <param name="data"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string AESDecrypt(string content, string key)
    {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(Encoding.ASCII.GetBytes(key));
        kgen.init(128, secureRandom);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();

        using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
        {
            aesProvider.Key = enCodeFormat;
            aesProvider.Mode = CipherMode.ECB;
            aesProvider.Padding = PaddingMode.PKCS7;
            using (ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor())
            {
                byte[] inputBuffers = Convert.FromBase64String(content);
                byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
                aesProvider.Clear();
                return Encoding.UTF8.GetString(results);
            }
        }
    }
 /// <summary>
    /// AES encryption
    /// </summary>
    /// <param name="data"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string AESEncrypt(string content, string key)
    {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(Encoding.ASCII.GetBytes(key));
        kgen.init(128, secureRandom);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();

        using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider())
        {
            aesProvider.Key = enCodeFormat;
            aesProvider.Mode = CipherMode.ECB;
            aesProvider.Padding = PaddingMode.PKCS7;
            using (ICryptoTransform cryptoTransform = aesProvider.CreateEncryptor())
            {
                byte[] inputBuffers = Encoding.UTF8.GetBytes(content);
                byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
                aesProvider.Clear();
                return Convert.ToBase64String(results);
            }
        }
    }

Thanks again to Blogger Zhu Xiaobo https://www.cnblogs.com/xbzhu/

Tags: ASP.NET Java encoding ascii github

Posted on Tue, 03 Dec 2019 05:43:33 -0800 by mrgrammar