首页 > 开发 > 综合 > 正文

C#一个封装的加密解密类

2024-07-21 02:18:03
字体:
来源:转载
供稿:网友
using system;
using system.text;
using system.globalization;
using system.security.cryptography;
using system.windows.forms;

namespace jh.encrypts
{
public class jhencrypt
{
/// <summary>
/// 构造方法
/// </summary>
public jhencrypt()
{
}
/// <summary>
/// 使用缺省密钥字符串加密
/// </summary>
/// <param name="original">明文</param>
/// <returns>密文</returns>
public static string encrypt(string original)
{
return encrypt(original,"jasonheung");
}
/// <summary>
/// 使用缺省密钥解密
/// </summary>
/// <param name="original">密文</param>
/// <returns>明文</returns>
public static string decrypt(string original)
{
return decrypt(original,"jasonheung",system.text.encoding.default);
}
/// <summary>
/// 使用给定密钥解密
/// </summary>
/// <param name="original">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static string decrypt(string original, string key)
{
return decrypt(original,key,system.text.encoding.default);
}
/// <summary>
/// 使用缺省密钥解密,返回指定编码方式明文
/// </summary>
/// <param name="original">密文</param>
/// <param name="encoding">编码方式</param>
/// <returns>明文</returns>
public static string decrypt(string original,encoding encoding)
{
return decrypt(original,"jasonheung",encoding);
}
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="original">原始文字</param>
/// <param name="key">密钥</param>
/// <param name="encoding">字符编码方案</param>
/// <returns>密文</returns>
public static string encrypt(string original, string key)
{
byte[] buff = system.text.encoding.default.getbytes(original);
byte[] kb = system.text.encoding.default.getbytes(key);
return convert.tobase64string(encrypt(buff,kb));
}

/// <summary>
/// 使用给定密钥解密
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <param name="encoding">字符编码方案</param>
/// <returns>明文</returns>
public static string decrypt(string encrypted, string key,encoding encoding)
{
byte[] buff = convert.frombase64string(encrypted);
byte[] kb = system.text.encoding.default.getbytes(key);
return encoding.getstring(decrypt(buff,kb));
}
/// <summary>
/// 生成md5摘要
/// </summary>
/// <param name="original">数据源</param>
/// <returns>摘要</returns>
public static byte[] makemd5(byte[] original)
{
md5cryptoserviceprovider hashmd5 = new md5cryptoserviceprovider();
byte[] keyhash = hashmd5.computehash(original);
hashmd5 = null;
return keyhash;
}

/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="original">明文</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
public static byte[] encrypt(byte[] original, byte[] key)
{
tripledescryptoserviceprovider des = new tripledescryptoserviceprovider();
des.key = makemd5(key);
des.mode = ciphermode.ecb;

return des.createencryptor().transformfinalblock(original, 0, original.length);
}

/// <summary>
/// 使用给定密钥解密数据
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static byte[] decrypt(byte[] encrypted, byte[] key)
{
tripledescryptoserviceprovider des = new tripledescryptoserviceprovider();
des.key = makemd5(key);
des.mode = ciphermode.ecb;

return des.createdecryptor().transformfinalblock(encrypted, 0, encrypted.length);
}

/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="original">原始数据</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
public static byte[] encrypt(byte[] original)
{
byte[] key = system.text.encoding.default.getbytes("jasonheung");
return encrypt(original,key);
}

/// <summary>
/// 使用缺省密钥解密数据
/// </summary>
/// <param name="encrypted">密文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static byte[] decrypt(byte[] encrypted)
{
byte[] key = system.text.encoding.default.getbytes("jasonheung");
return decrypt(encrypted,key);
}

}
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表