zoukankan      html  css  js  c++  java
  • C# 之 AES加密源码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    using Exam.EncryptPrivate;
    
    using System.Text;
    using System.Security.Cryptography;
    
    /// <summary> 
    /// Aes加密、解密,密钥长度256,密钥长度不大于32个字节 
    /// </summary> 
    public class AesCryptoHelper
    {
        public const string RET_ERROR = "x07x07x07x07x07";
        private const string CRYPTO_IV = "XIANJIAN";
        private const string CRYPTO_KEY = "XIANJIAN";
        private const int CRYPTO_KEY_LENGTH = 32;
        private const int CRYPTO_IV_LENGTH = 16;
    
        private AesCryptoServiceProvider m_aesCryptoServiceProvider;
        private string m_message;
        public string Message
        {
            get { return m_message; }
            set { m_message = value; }
        }
    
        private bool m_containKey;
    
        /// <summary> 
        /// True:密文中包含密钥 
        /// False:密文中不包含密钥 
        /// </summary> 
        public bool ContainKey
        {
            get { return m_containKey; }
            set { m_containKey = value; }
        }
    
        public AesCryptoHelper()
        {
            m_aesCryptoServiceProvider = new AesCryptoServiceProvider();
            m_containKey = true;
            m_message = string.Empty;
        }
    
        public AesCryptoHelper(bool containKey):this()
        {
            m_containKey = containKey;
        }
    
        private string Encrypt(string s_crypto, byte[] key, byte[] iv)
        {
            string s_encryped = string.Empty;
            byte[] crypto, encrypted;
            ICryptoTransform ct;
    
            try
            {
                crypto = string2Byte(s_crypto);
                m_aesCryptoServiceProvider.Key = key;
                m_aesCryptoServiceProvider.IV = iv;
                ct = m_aesCryptoServiceProvider.CreateEncryptor();
                encrypted = ct.TransformFinalBlock(crypto, 0, crypto.Length);
    
                if (m_containKey)
                {
                    s_encryped += byte2HexString(key);
                }
    
                s_encryped += byte2HexString(encrypted);
                return s_encryped;
            }
    
            catch (Exception ex)
            {
                m_message = ex.ToString();
                return RET_ERROR;
            }
        }
    
        /// <summary> 
        /// 指定密钥对明文进行AES加密 
        /// </summary> 
        /// <param name="s_crypto">明文</param> 
        /// <param name="s_key">加密密钥</param> 
        /// <returns></returns> 
        public string Encrypt(string s_crypto, string s_key)
        {
            byte[] key = new byte[CRYPTO_KEY_LENGTH], iv = new byte[CRYPTO_IV_LENGTH];
            byte[] temp = string2Byte(s_key);
    
            if (temp.Length > key.Length)
            {
                m_message = "Key too long,need less than 32 Bytes key.";
                return RET_ERROR;
            }
    
            key = string2Byte(s_key.PadRight(key.Length));
            iv = string2Byte(CRYPTO_IV.PadRight(iv.Length));
            return Encrypt(s_crypto, key, iv);
        }
    
        /// <summary> 
        /// 动态生成密钥,并对明文进行AES加密 
        /// </summary> 
        /// <param name="s_crypto">明文</param> 
        /// <returns></returns> 
        public string Encrypt(string s_crypto)
        {
            byte[] key = new byte[CRYPTO_KEY_LENGTH], iv = new byte[CRYPTO_IV_LENGTH];
            m_aesCryptoServiceProvider.GenerateKey();
            key = m_aesCryptoServiceProvider.Key;
            iv = string2Byte(CRYPTO_IV.PadRight(iv.Length));
            return Encrypt(s_crypto, key, iv);
        }
    
        private string Decrypt(string s_encrypted, byte[] key, byte[] iv)
        {
            string s_decrypted = string.Empty;
            byte[] encrypted, decrypted;
            ICryptoTransform ct;
            try
            {
                encrypted = hexString2Byte(s_encrypted);
                m_aesCryptoServiceProvider.Key = key;
                m_aesCryptoServiceProvider.IV = iv;
                ct = m_aesCryptoServiceProvider.CreateDecryptor();
                decrypted = ct.TransformFinalBlock(encrypted, 0, encrypted.Length);
                s_decrypted += byte2String(decrypted);
                return s_decrypted;
            }
            catch (Exception ex)
            {
                m_message = ex.ToString();
                m_message = "Decrypt fail.";
                return RET_ERROR;
            }
        }
    
        /// <summary> 
        /// 从密文中解析出密钥,并对密文进行解密 
        /// </summary> 
        /// <param name="s_encrypted">密文</param> 
        /// <returns></returns> 
        public string Decrypt(string s_encrypted)
        {
            string s_key = string.Empty;
            byte[] key = new byte[CRYPTO_KEY_LENGTH], iv = new byte[CRYPTO_IV_LENGTH];
            if (s_encrypted.Length <= CRYPTO_KEY_LENGTH * 2)
            {
                m_message = "Encrypted string invalid.";
                return RET_ERROR;
            }
    
            if (m_containKey)
            {
                s_key = s_encrypted.Substring(0, CRYPTO_KEY_LENGTH * 2);
                s_encrypted = s_encrypted.Substring(CRYPTO_KEY_LENGTH * 2);
            }
    
            key = hexString2Byte(s_key);
            iv = string2Byte(CRYPTO_IV.PadRight(iv.Length));
            return Decrypt(s_encrypted, key, iv);
        }
    
        /// <summary> 
        /// 指定密钥,并对密文进行解密 
        /// </summary> 
        /// <param name="s_encrypted">密文</param> 
        /// <param name="s_key">密钥</param> 
        /// <returns></returns> 
        public string Decrypt(string s_encrypted, string s_key)
        {
            byte[] key = new byte[CRYPTO_KEY_LENGTH], iv = new byte[CRYPTO_IV_LENGTH];
            byte[] temp = string2Byte(s_key);
            if (temp.Length > key.Length)
            {
                m_message = "Key invalid.too long,need less than 32 Bytes";
                return RET_ERROR;
            }
    
            key = string2Byte(s_key.PadRight(key.Length));
            iv = string2Byte(CRYPTO_IV.PadRight(iv.Length));
            if (m_containKey)
            {
                s_encrypted = s_encrypted.Substring(CRYPTO_KEY_LENGTH * 2);
            }
    
            return Decrypt(s_encrypted, key, iv);
        }
    
        private string byte2HexString(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in bytes)
            {
                sb.AppendFormat("{0:X2}", b);
            }
    
            return sb.ToString();
        }
    
        private byte[] hexString2Byte(string hex)
        {
            int len = hex.Length / 2;
            byte[] bytes = new byte[len];
    
            for (int i = 0; i < len; i++)
            {
                bytes[i] = (byte)(Convert.ToInt32(hex.Substring(i * 2, 2), 16));
            }
            return bytes;
        }
    
        private byte[] string2Byte(string str)
        {
            return Encoding.UTF8.GetBytes(str);
        }
    
        private string byte2String(byte[] bytes)
        {
            return Encoding.UTF8.GetString(bytes);
        }
    } 
  • 相关阅读:
    点击表单提交 却打开了另存为对话框
    js中对象的复制,浅复制(浅拷贝)和深复制(深拷贝)
    html怎么隐藏input标签
    莫名其妙地改变了数组的值,原来是……
    addEventListener 用法
    JavaScript DOM元素常见操作详解【添加、删除、修改等】
    MySQL用户权限验证与管理方法详解
    mysql如何修改root用户的密码
    python案例
    IDEA启动报 Error:java: JDK isn't specified for module 'xxx' 已解决
  • 原文地址:https://www.cnblogs.com/xinaixia/p/4991379.html
Copyright © 2011-2022 走看看