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"; }
//}
/////
///// 获取向量
/////
//private static string IV
//{
// get { return @"L+~f4,Ir)b$=pkf"; }
//}
//public static void Decrypt(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);
}

}

}

AES加密代码知道了,现在怎么把读取出来的身份证和代码联系起来

查看全部
qq_29651709
qq_29651709
2015/07/17 01:06
  • c#
  • 点赞
  • 收藏
  • 回答
    私信

3个回复