数据加密标准 C#的简单应用
using System;
using System.Security.Cryptography;
using System.Text;
namespace Models.Config
{
public class DES : IDisposable
{
private ICryptoTransform m_decryptor;
private ICryptoTransform m_encryptor;
private System.Security.Cryptography.DES m_Des;
public DES()
{
m_Des = System.Security.Cryptography.DES.Create();
m_Des.BlockSize = 64;
m_Des.KeySize = 64;
m_Des.IV = new byte[8]
{
39,
44,
123,
11,
45,
0,
33,
12
};
m_Des.Key = new byte[8]
{
125,
114,
154,
11,
99,
88,
74,
14
};
m_decryptor = m_Des.CreateDecryptor();
m_encryptor = m_Des.CreateEncryptor();
}
public string Encrypt(string input)
{
byte[] bytes = Encoding.ASCII.GetBytes(input);
return Convert.ToBase64String(m_encryptor.TransformFinalBlock(bytes, 0, bytes.Length));
}
public string Decrypt(string input)
{
byte[] array = Convert.FromBase64String(input);
byte[] bytes = m_decryptor.TransformFinalBlock(array, 0, array.Length);
return Encoding.ASCII.GetString(bytes);
}
public void Dispose()
{
Dispose(isDisposing: true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool isDisposing)
{
if (m_Des != null)
{
m_Des.Dispose();
}
if (m_decryptor != null)
{
m_decryptor.Dispose();
}
if (m_encryptor != null)
{
m_encryptor.Dispose();
}
}
~DES()
{
Dispose(isDisposing: false);
}
}
}