zoukankan      html  css  js  c++  java
  • C#.NET AES CBC 加密

    重点:

    1. KEY 和 IV 转 byte[] 时的编码。

    2.要加密的字符串转 byte[] 时的编码。

    3.AES 的PADDING,MODE。

    4.加密后的byte[] 转字符串时的编码。

    先看加密代码:

    public static string AesEncrypt(string content, string aesKey, string aesIV)
            {
    
                byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);
                byte[] byteIV = Encoding.UTF8.GetBytes(aesIV);
    
                byte[] byteContnet = Encoding.UTF8.GetBytes(content);
    
                var _aes = new RijndaelManaged();
                _aes.Padding = PaddingMode.PKCS7;
                _aes.Mode = CipherMode.CBC;
                 
                _aes.Key = byteKEY;
                _aes.IV = byteIV;
    
                var _crypto = _aes.CreateEncryptor(byteKEY, byteIV);
                byte[] decrypted = _crypto.TransformFinalBlock(
                    byteContnet, 0, byteContnet.Length);
    
                _crypto.Dispose();
    
                return Convert.ToBase64String(decrypted);
            }

    byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);
    byte[] byteIV = Encoding.UTF8.GetBytes(aesIV);  

    KEY 和 IV 转 byte[] 时的编码使用UTF8。

    byte[] byteContnet = Encoding.UTF8.GetBytes(content); 

     要加密的字符串转 byte[] 时的编码使用UTF8。

    _aes.Padding = PaddingMode.PKCS7;
    _aes.Mode = CipherMode.CBC;

    AES 的PADDING 为PKCS7 (对应JAVA的PKCS5Padding) ,MODE 为 CBC (CBC模式需要有IV,ECB模式不需要IV) 。

    return Convert.ToBase64String(decrypted);  

    加密后的byte[] 转字符串时的编码,使用Base64。

    --解密过程也大致相同。

    1.先把密文字符串Base64 解码为 byte[] 。

    2.解密后的byte[] ,再用UTF8编码还原为String.

    解密代码:

    /// <summary>
            /// 解密
            /// </summary>
            /// <param name="decryptStr">要解密的串</param>
            /// <param name="aesKey">密钥</param>
            /// <param name="aesIV">IV</param>
            /// <returns></returns>
            public static string AesDecrypt(string decryptStr, string aesKey,string aesIV)
            {
    
                byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);
                byte[] byteIV = Encoding.UTF8.GetBytes(aesIV);
    
                byte[] byteDecrypt = System.Convert.FromBase64String(decryptStr);
    
                var _aes = new RijndaelManaged();
                _aes.Padding = PaddingMode.PKCS7;
                _aes.Mode = CipherMode.CBC;
                 
                _aes.Key = byteKEY;
                _aes.IV = byteIV;
    
                var _crypto = _aes.CreateDecryptor(byteKEY, byteIV);
                byte[] decrypted = _crypto.TransformFinalBlock(
                    byteDecrypt, 0, byteDecrypt.Length);
    
                _crypto.Dispose();
    
                return Encoding.UTF8.GetString(decrypted);
            }

    --

    完整的AES UTIL , AES CBC ,PCKS7。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    
    namespace Common.Utils
    {
       public static class AesUtil
        {
    
            /// <summary>
            /// 解密
            /// </summary>
            /// <param name="decryptStr">要解密的串</param>
            /// <param name="aesKey">密钥</param>
            /// <param name="aesIV">IV</param>
            /// <returns></returns>
            public static string AesDecrypt(string decryptStr, string aesKey,string aesIV)
            {
    
                byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);
                byte[] byteIV = Encoding.UTF8.GetBytes(aesIV);
    
                byte[] byteDecrypt = System.Convert.FromBase64String(decryptStr);
    
                var _aes = new RijndaelManaged();
                _aes.Padding = PaddingMode.PKCS7;
                _aes.Mode = CipherMode.CBC;
                 
                _aes.Key = byteKEY;
                _aes.IV = byteIV;
    
                var _crypto = _aes.CreateDecryptor(byteKEY, byteIV);
                byte[] decrypted = _crypto.TransformFinalBlock(
                    byteDecrypt, 0, byteDecrypt.Length);
    
                _crypto.Dispose();
    
                return Encoding.UTF8.GetString(decrypted);
            }
    
    
            public static string AesEncrypt(string content, string aesKey, string aesIV)
            {
    
                byte[] byteKEY = Encoding.UTF8.GetBytes(aesKey);
                byte[] byteIV = Encoding.UTF8.GetBytes(aesIV);
    
                byte[] byteContnet = Encoding.UTF8.GetBytes(content);
    
                var _aes = new RijndaelManaged();
                _aes.Padding = PaddingMode.PKCS7;
                _aes.Mode = CipherMode.CBC;
                 
                _aes.Key = byteKEY;
                _aes.IV = byteIV;
    
                var _crypto = _aes.CreateEncryptor(byteKEY, byteIV);
                byte[] decrypted = _crypto.TransformFinalBlock(
                    byteContnet, 0, byteContnet.Length);
    
                _crypto.Dispose();
    
                return Convert.ToBase64String(decrypted);
            }
    
        }
    }

    --

    调用DEMO,WINFORM写的。

    using Common.Utils;
    using System;
    using System.Windows.Forms;
    
    namespace AES加密
    {
    
        public partial class Form1 : Form
        {
    
            string _aesKey = "1234567890123456";
    
            string _aesIV = "abcdefghABCDEFGH";
    
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
    
            /// <summary>
            /// 加密
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btmJiaMi_Click(object sender, EventArgs e)
            {
                try
                {
                    if (string.IsNullOrEmpty(txtDaiJiaMi.Text))
                    {
                        MessageBox.Show("待加密字符串 为空!");
                        return;
                    }
    
                    string content = txtDaiJiaMi.Text;
    
    
                    txtJiaMiHou.Text = AesUtil.AesEncrypt(content, _aesKey, _aesIV);
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
    
            /// <summary>
            /// 解密
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnJieMi_Click(object sender, EventArgs e)
            {
                try
                {
                    if (string.IsNullOrEmpty(txtJiaMiHou.Text))
                    {
                        MessageBox.Show("加密后字符串 为空!");
                        return;
                    }
    
                    string content = txtJiaMiHou.Text;
    
    
                    txtJieMiHou.Text = AesUtil.AesDecrypt(content, _aesKey, _aesIV);
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }

    --

     只要 KEY ,IV , MODE,PADDING ,密文编码,明文编码这几项统一,和其它编程语言加解密就无障碍了。

    --

     
  • 相关阅读:
    触发器和存储过程简述
    sql笔记
    AES加密解密代码
    动态获取实体类及类中方法
    springboot metrics elk
    深入浅出Spring Security
    RandomValueStringGenerator
    ExceptionUtils
    spring.main.web-application-type: none
    excel前80%平均分
  • 原文地址:https://www.cnblogs.com/runliuv/p/13247119.html
Copyright © 2011-2022 走看看