AES加密解密算法——使用.NET中Rijndael实现

public sealed class RijndaelEncDec
    {
        /// <summary>
        /// 初始化加密的key
        /// </summary>
        public static string Password
        {
            get;set;
        }

        private static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();

            Rijndael alg = Rijndael.Create();

            alg.Key = Key;
            alg.IV = IV;

            CryptoStream cs = new CryptoStream(ms,
                alg.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(clearData, 0, clearData.Length);

            cs.Close();

            byte[] encryptedData = ms.ToArray();

            return encryptedData;
        }

        /// <summary>
        /// 返回通过key加密后的字符串
        /// </summary>
        /// <param name="clearText"></param>
        /// <returns></returns>
        public static string Encrypt(string clearText)
        {
            return Encrypt(clearText, Password);
        }

        private static string Encrypt(string clearText, string password)
        {
            byte[] clearBytes =
                System.Text.Encoding.Unicode.GetBytes(clearText);

            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password,
                new byte[]
                {
                    0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
                    0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
                });

            byte[] encryptedData = Encrypt(clearBytes,
                pdb.GetBytes(32), pdb.GetBytes(16));

            return Convert.ToBase64String(encryptedData);

        }

        private static byte[] Encrypt(byte[] clearData)
        {
            return Encrypt(clearData, Password);
        }

        private static byte[] Encrypt(byte[] clearData, string password)
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password,
                new byte[]
                {
                    0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
                    0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
                });

            return Encrypt(clearData, pdb.GetBytes(32), pdb.GetBytes(16));

        }

        /// <summary>
        /// 导出加密后的文件
        /// </summary>
        /// <param name="fileIn">需要加密的文本</param>
        /// <param name="fileOut">加密后的文本</param>
        /// <param name="foo">预留标记未做使用,默认传入true即可</param>
        public static void Encrypt(string fileIn, string fileOut, bool foo)
        {
            Encrypt(fileIn, fileOut, Password);
        }

        private static void Encrypt(string fileIn, string fileOut, string password)
        {

            FileStream fsIn = new FileStream(fileIn,
                FileMode.Open, FileAccess.Read);
            FileStream fsOut = new FileStream(fileOut,
                FileMode.OpenOrCreate, FileAccess.Write);

            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password,
                new byte[]
                {
                    0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
                    0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
                });

            Rijndael alg = Rijndael.Create();
            alg.Key = pdb.GetBytes(32);
            alg.IV = pdb.GetBytes(16);

            CryptoStream cs = new CryptoStream(fsOut,
                alg.CreateEncryptor(), CryptoStreamMode.Write);

            int bufferLen = 4096;
            byte[] buffer = new byte[bufferLen];
            int bytesRead;

            do
            {
                bytesRead = fsIn.Read(buffer, 0, bufferLen);

                cs.Write(buffer, 0, bytesRead);
            } while (bytesRead != 0);

            cs.Close();
            fsIn.Close();
        }

        private static byte[] Decrypt(byte[] cipherData,
            byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();

            Rijndael alg = Rijndael.Create();

            alg.Key = Key;
            alg.IV = IV;

            CryptoStream cs = new CryptoStream(ms,
                alg.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(cipherData, 0, cipherData.Length);

            cs.Close();

            byte[] decryptedData = ms.ToArray();

            return decryptedData;
        }

        /// <summary>
        /// 返回通过key解密的原始字符串
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        public static string Decrypt(string cipherText)
        {
            return Decrypt(cipherText, Password);
        }

        private static string Decrypt(string cipherText, string password)
        {
            byte[] cipherBytes = Convert.FromBase64String(cipherText);

            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password,
                new byte[]
                {
                    0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
                    0x64, 0x76, 0x65, 0x64, 0x65, 0x76
                });

            byte[] decryptedData = Decrypt(cipherBytes,
                pdb.GetBytes(32), pdb.GetBytes(16));

            return System.Text.Encoding.Unicode.GetString(decryptedData);
        }

        private static byte[] Decrypt(byte[] cipherData, string password)
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password,
                new byte[]
                {
                    0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
                    0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
                });

            return Decrypt(cipherData, pdb.GetBytes(32), pdb.GetBytes(16));
        }

        /// <summary>
        /// 返回解密后的文本
        /// </summary>
        /// <param name="fileIn"></param>
        /// <param name="fileOut"></param>
        /// <param name="foo">预留标记未做使用,默认传入true即可</param>
        public static void Decrypt(string fileIn, string fileOut, bool foo)
        {
            Decrypt(fileIn, fileOut, Password);
        }

        private static void Decrypt(string fileIn, string fileOut, string password)
        {

            FileStream fsIn = new FileStream(fileIn,
                FileMode.Open, FileAccess.Read);
            FileStream fsOut = new FileStream(fileOut,
                FileMode.OpenOrCreate, FileAccess.Write);

            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password,
                new byte[]
                {
                    0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
                    0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
                });
            Rijndael alg = Rijndael.Create();

            alg.Key = pdb.GetBytes(32);
            alg.IV = pdb.GetBytes(16);

            CryptoStream cs = new CryptoStream(fsOut,
                alg.CreateDecryptor(), CryptoStreamMode.Write);

            int bufferLen = 4096;
            byte[] buffer = new byte[bufferLen];
            int bytesRead;

            do
            {
                bytesRead = fsIn.Read(buffer, 0, bufferLen);

                cs.Write(buffer, 0, bytesRead);

            } while (bytesRead != 0);

            cs.Close();
            fsIn.Close();
        }
    }
时间: 2024-10-08 20:04:23

AES加密解密算法——使用.NET中Rijndael实现的相关文章

php与java通用AES加密解密算法

php与java通用AES加密解密算法 AES指高级加密标准(Advanced Encryption Standard),是当前最流行的一种密码算法,在web应用开发,特别是对外提供接口时经常会用到,下面是我整理的一套php与java通用的AES加密解密算法. php版代码如下: <?php class CryptAES { protected $cipher = MCRYPT_RIJNDAEL_128; protected $mode = MCRYPT_MODE_ECB; protected

AES加密解密算法---java

package com.BFGJ.AES; import java.util.Random; import java.util.StringTokenizer; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParamet

C# AES加密解密算法

/// <summary> /// AES加密 /// </summary> /// <param name="encryptStr">明文</param> /// <param name="key">密钥</param> /// <returns></returns> public static string Encrypt(string encryptStr,stri

非对称技术栈实现AES加密解密

非对称技术栈实现AES加密解密 正如前面的一篇文章所述,https协议的SSL层是实现在传输层之上,应用层之下,也就是说在应用层上看到的请求还是明码的,对于某些场景下要求这些http请求参数是非可读的,这就要求在前端和后端不同的技术栈上完成信息的加密解密.当然我们通常完成这样专业的功能都会考虑使用相应的框架或者程序库来完成功能,前端或者NodeJS平台通常是JavaScript语言,JavaScript主流的加密解密库分别是SjclJS和CryptoJS, 本文以CryptoJS为例进行讨论.另

AES-128-ECB 16进制加密解密算法

import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /** * AES加密解密算法 * 默认采用AES-128-ECB加密模式 */ public class AESUtils { private static final String SKEY = "uHNTivV09dQxLor9"; /** * 加密 * @param sSrc * @return * @throws Exception */ p

JAVA中使用AES加密解密

技术交流群: 233513714 /** * AES加密测试 * * @param str 加密参数 */ public void aesTest(String str) { log.info("[rsaTest获取请求:{}]", str); try { String encryptResult = AESUtil.encrypt(str, "123456"); log.info("[AES加密后的参数为:{}]", encryptResult

【Android工具】DES终结者加密时报——AES加密演算法

转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 在前面的两篇文章中.我们介绍了DES算法,3DES算法以及他们的Android程序实现,并研究了怎样才干实现不同平台下加密算法的一致性. 只是话说起来,DES算法是在1976年被美国的国家标准局定为联邦资料的加密标准的,到如今已经接近40年了.我们都知道.在计算机的世界里有一个摩尔定律.就是每过18个月.计算机的晶体管的数量就会翻一番,相应的计算速度也会翻倍,尽管如今的发展速度有所放缓,可是每过三年左右,

Java 关于密码处理的工具类[MD5编码][AES加密/解密]

项目中又遇到了加密问题,又去翻了半天,然后做测试,干脆就把常用的两类小结一下. 1.第一种所谓的MD5加密 其实也不算加密,只是基于Hash算法的不可逆编码而已,等于说,一旦经过MD5处理,是不可能从编码后的字符串反推回去的. MD5的方法是基于散列的.本身信息不全.理论上是不能还原成唯一字符串的. 网上所谓的解密,也只是拥有一个足够大的字典映射,将编码前的源字符和编码后的目标字符关联起来而已,大多数常见的还行,复杂点的估计就会话费很长时间,有兴趣的可以试试. 至于MD5的用法,在初次录入的时候

AES加解密算法Qt实现

[声明] (1) 本文源码 在一位未署名网友源码基础上,利用Qt编程,实现了AES加解密算法,并添加了文件加解密功能.在此表示感谢!该源码仅供学习交流,请勿用于商业目的. (2) 图片及描述 除图1外,图片及部分解析来自http://zh.wikipedia.org/wiki/%E9%AB%98%E7%BA%A7%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86.图1为个人劳动成果,请勿盗用此图. [简介] AES(Advanced Encryption Standard,