C# AES CBC PKCS7

2021-12-16 09:49:15 浏览数 (1)

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

代码语言:javascript复制
find_package(OpenSSL REQUIRED)
target_link_libraries(main PRIVATE OpenSSL::SSL OpenSSL::Crypto)
代码语言:javascript复制
The package workflow provides CMake targets:

    find_package(workflow CONFIG REQUIRED)
    target_link_libraries(main PRIVATE workflow)
代码语言:javascript复制
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)
代码语言:javascript复制
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
    {
        /// <summary>
        /// 获取密钥 必须是32字节
        /// </summary>
        //private static string Key
        //{
       //     get { return @"ae125efkk4_54eeff444ferfkny6oxi8"; }
       // }

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="plainStr">明文字符串</param>
        /// <returns>密文</returns>
        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);
        }
    }
}

0 人点赞