C# AES CBC PKCS7

The package openssl is compatible with built-in CMake targets:

find_package(OpenSSL REQUIRED)
target_link_libraries(main PRIVATE OpenSSL::SSL OpenSSL::Crypto)
The package workflow provides CMake targets:

    find_package(workflow CONFIG REQUIRED)
    target_link_libraries(main PRIVATE workflow)
The package civetweb provides CMake targets:

    find_package(civetweb CONFIG REQUIRED)
    # Note: 1 target(s) were omitted.
    target_link_libraries(main PRIVATE LIBDL::LIBDL LIBRT::LIBRT WINSOCK::WINSOCK civetweb::civetweb)

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Aes_Example
{
    class AesExample
    {
        public static void Main()
        {
            string original = "GET_METER,1637285907253";

            // Create a new instance of the Aes
            // class.  This generates a new key and initialization
            // vector (IV).
            //string strkey = "EC2659C602F51F5190456BC6E2AEFE37";
            //byte[] key = System.Text.Encoding.Default.GetBytes(strkey);
            byte[] keyArray = new byte[] { 0xEC, 0x26, 0x59, 0xC6, 0x02, 0xF5, 0x1F, 0x51, 0x90, 0x45, 0x6B, 0xC6, 0xE2, 0xAE, 0xFE, 0x37 };//UTF8Encoding.UTF8.GetBytes(key0);


            string encrypted = AES.AESEncrypt(original, keyArray);

            Console.WriteLine(encrypted);

            string round = AES.AESDEncrypt(encrypted, keyArray);
            Console.WriteLine(round);

        }
  
    }

    public class AES
    {
        /// 
        /// 获取密钥 必须是32字节
        /// 
        //private static string Key
        //{
       //     get { return @"ae125efkk4_54eeff444ferfkny6oxi8"; }
       // }

        /// 
        /// AES加密
        /// 
        /// 明文字符串
        /// 密文
        public static string AESEncrypt(string encryptStr,byte [] key0)
        {
            
            byte[] toEncryptArray = System.Text.Encoding.Default.GetBytes(encryptStr);// UTF8Encoding.UTF8.GetBytes(encryptStr);
            toEncryptArray = System.Text.Encoding.ASCII.GetBytes(encryptStr);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = key0;
            rDel.IV = key0;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        public static string AESDEncrypt(string encryptStr, byte[] key0)
        {
            //byte[] keyArray = new byte[] { 0xEC, 0x26, 0x59, 0xC6, 0x02, 0xF5, 0x1F, 0x51, 0x90, 0x45, 0x6B, 0xC6, 0xE2, 0xAE, 0xFE, 0x37 }; // UTF8Encoding.UTF8.GetBytes(key0);
            byte[] toEncryptArray = Convert.FromBase64String(encryptStr);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = key0;
            rDel.IV = key0;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
    }
}

你可能感兴趣的:(C# AES CBC PKCS7)