2 qq 29651709 qq_29651709 于 2015.07.16 09:39 提问

C# AES加密的问题~~~~

将从CSV文件中读取到的身份证号码加密,要求使用AES加密,加密的函数接口请调用”AES.CS”源文件。
ASE.CS源文件的代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Utility
{
public class AES
{
/////
///// 获取密钥
/////
//private static string Key
//{
// get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; }
//}

    ///// <summary>
    ///// 获取向量
    ///// </summary>
    //private static string IV
    //{
    //    get { return @"L+\~f4,Ir)b$=pkf"; }
    //}
    //public static void Decrypt<T>(T info)
    //{

    //    foreach (var property in info.GetType().GetProperties())
    //    {
    //        var pro = property.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "DecodeAttribute");
    //        if (pro != null)
    //         {
    //            property.SetValue(info,AESDecrypt(property.GetValue(info).ToString()));
    //        }
    //    }
    //}
    //public static void Encrypt<T>(T info)
    //{
    //    foreach (var property in info.GetType().GetProperties())
    //    {
    //        var pro = property.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "DecodeAttribute");
    //        if (pro!=null)
    //        {
    //            property.SetValue(info, AESEncrypt(property.GetValue(info).ToString()));
    //        }
    //    }
    //}
    /// <summary>
    /// AES加密
    /// </summary>
    /// <param name="plainStr">明文字符串</param>
    /// <returns>密文</returns>
    public static string AESEncrypt(string plainStr)
    {
        byte[] bKey = Encoding.UTF8.GetBytes(Config.aesKey);
        byte[] bIV = Encoding.UTF8.GetBytes(Config.aesIV);
        byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);

        string encrypt = null;
        Rijndael aes = Rijndael.Create();
        try
        {
            using (MemoryStream mStream = new MemoryStream())
            {
                using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
                {
                    cStream.Write(byteArray, 0, byteArray.Length);
                    cStream.FlushFinalBlock();
                    encrypt = Convert.ToBase64String(mStream.ToArray());
                }
            }
        }
        catch { }
        aes.Clear();

        return encrypt;
    }

    /// <summary>
    /// AES加密
    /// </summary>
    /// <param name="plainStr">明文字符串</param>
    /// <param name="returnNull">加密失败时是否返回 null,false 返回 String.Empty</param>
    /// <returns>密文</returns>
    public static string AESEncrypt(string plainStr, bool returnNull)
    {
        string encrypt = AESEncrypt(plainStr);
        return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt);
    }

    /// <summary>
    /// AES解密
    /// </summary>
    /// <param name="encryptStr">密文字符串</param>
    /// <returns>明文</returns>
    public static string AESDecrypt(string encryptStr)
    {
        byte[] bKey = Encoding.UTF8.GetBytes(Config.aesKey);
        byte[] bIV = Encoding.UTF8.GetBytes(Config.aesIV);
        byte[] byteArray = Convert.FromBase64String(encryptStr);

        string decrypt = null;
        Rijndael aes = Rijndael.Create();
        try
        {
            using (MemoryStream mStream = new MemoryStream())
            {
                using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
                {
                    cStream.Write(byteArray, 0, byteArray.Length);
                    cStream.FlushFinalBlock();
                    decrypt = Encoding.UTF8.GetString(mStream.ToArray());
                }
            }
        }
        catch { }
        aes.Clear();

        return decrypt;
    }

    /// <summary>
    /// AES解密
    /// </summary>
    /// <param name="encryptStr">密文字符串</param>
    /// <param name="returnNull">解密失败时是否返回 null,false 返回 String.Empty</param>
    /// <returns>明文</returns>
    public static string AESDecrypt(string encryptStr, bool returnNull)
    {
        string decrypt = AESDecrypt(encryptStr);
        return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt);
    }
}

}


新手,求告诉,怎么把这结合起来

3个回答

guwei4037
guwei4037   Ds   Rxr 2015.07.16 09:44

string oldStr1 = "你要加密的明文字符串";

string newStr = AES.AESEncrypt(oldStr);

string oldStr2 = AES.AESDecrypt(newStr);

//oldStr1和oldStr2应该相等

qq_29651709
qq_29651709 你这个是什么用。。
2 年多之前 回复
oyljerry
oyljerry   Ds   Rxr 2015.07.16 09:54

直接引用这个类
然后申明一个实例,调用AESEncrypt来加密,AESDecrypt解密

qq_29651709
qq_29651709 怎么引用,怎么申明,怎么调用~~
2 年多之前 回复
CSDNXIAOC
CSDNXIAOC   2015.07.16 15:33

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

namespace Encrypt
{
///
/// Description of AESEncrypt.
///
public class AESEncrypt
{
public AESEncrypt()
{......
答案就在这里:C#_AES加密
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

qq_29651709
qq_29651709 能告诉我 怎么将你这些代码 ,和我原来读取出来的数据联系在一起吗
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片