using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
class Program
{
private static string ToHexString( byte[] bytes)
{
string byteStr = string.Empty;
if (bytes != null || bytes.Length > 0)
{
foreach (var item in bytes)
{
byteStr += string.Format("{0:x2}", item);
}
}
return byteStr;
}
private static string AESEncrypt(String Data, String Key)
{
MemoryStream mStream = new MemoryStream();
RijndaelManaged aes = new RijndaelManaged();
byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
Byte[] bKey = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 128;
aes.Key = bKey;
CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
try
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
return ToHexString(mStream.ToArray());
}
finally
{
cryptoStream.Close();
mStream.Close();
aes.Clear();
}
}
public static void Main(string[] args)
{
Console.WriteLine(AESEncrypt("1234","1234567890abcdef"));
}
}
csharp 实现aes/ecb/pkcs7 128位加密
最新推荐文章于 2024-05-07 17:49:42 发布