110,546
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace myHeFeng.frameWork.components.stringEncrypt
{
/// <summary>
/// 提供非对称加码解密字符串的方法和函数,只适用短字符串,效率低
/// </summary>
public sealed class RSACryption
{
/// <summary>
/// RSA 的密钥产生 产生私钥 和公钥
/// </summary>
/// <param name="xmlKeys"></param>
/// <param name="xmlPublicKey"></param>
public static void RSAKey(out string xmlKeys, out string xmlPublicKey)
{
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
xmlKeys = rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
rsa.Clear();
}
}
/// <summary>
/// RSA的加密函数 string
/// </summary>
/// <param name="xmlPublicKey"></param>
/// <param name="m_strEncryptString"></param>
/// <returns></returns>
public string RSAEncrypt(string xmlPublicKey, string input)
{
byte[] PlainTextBArray;
byte[] CypherTextBArray;
string Result;
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray = (new UnicodeEncoding()).GetBytes(input);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
}
}
/// <summary>
/// RSA的加密函数 byte[]
/// </summary>
/// <param name="xmlPublicKey"></param>
/// <param name="EncryptString"></param>
/// <returns></returns>
public string RSAEncrypt(string xmlPublicKey, byte[] inputByte)
{
byte[] CypherTextBArray;
string Result;
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray = rsa.Encrypt(inputByte, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
}
}
/// <summary>
/// RSA的解密函数 string
/// </summary>
/// <param name="xmlPrivateKey"></param>
/// <param name="m_strDecryptString"></param>
/// <returns></returns>
public string RSADecrypt(string xmlPrivateKey, string input)
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result;
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPrivateKey);
PlainTextBArray = Convert.FromBase64String(input);
DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
}
/// <summary>
/// RSA的解密函数 byte[]
/// </summary>
/// <param name="xmlPrivateKey"></param>
/// <param name="DecryptString"></param>
/// <returns></returns>
public string RSADecrypt(string xmlPrivateKey, byte[] inputByte)
{
byte[] DypherTextBArray;
string Result;
using (System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider())
{
rsa.FromXmlString(xmlPrivateKey);
DypherTextBArray = rsa.Decrypt(inputByte, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
}
#region RSA数字签名
#region 获取Hash描述表
//获取Hash描述表
public bool GetHash(string m_strSource, ref byte[] HashData)
{
//从字符串中取得Hash描述
byte[] Buffer;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
HashData = MD5.ComputeHash(Buffer);
return true;
}
namespace myHeFeng.frameWork.components.stringEncrypt
{
public class simple3Des : IDisposable
{
private TripleDESCryptoServiceProvider TripleDes = new TripleDESCryptoServiceProvider();
public simple3Des(string key)
{
TripleDes.Key = TruncateHash(key, TripleDes.KeySize / 8);
TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);
}
private byte[] TruncateHash(string key, int length)
{
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] keyBytes = System.Text.Encoding.Unicode.GetBytes(key);
byte[] hash = sha1.ComputeHash(keyBytes);
hash = (byte[])Microsoft.VisualBasic.CompilerServices.Utils.CopyArray((Array)hash, new byte[length - 1 + 1]);
return hash;
}
public string EncryptData(string plaintext)
{
byte[] plaintextBytes = System.Text.Encoding.Unicode.GetBytes(plaintext);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream encStream = new CryptoStream(ms, TripleDes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
encStream.Write(plaintextBytes, 0, plaintextBytes.Length);
encStream.FlushFinalBlock();
return System.Convert.ToBase64String(ms.ToArray());
}
public string DecryptData(string encryptedtext)
{
byte[] encryptedBytes = System.Convert.FromBase64String(encryptedtext);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream decStream = new CryptoStream(ms, TripleDes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
decStream.FlushFinalBlock();
return System.Text.Encoding.Unicode.GetString(ms.ToArray());
}
#region IDisposable 成员
public void Dispose()
{
TripleDes.Clear();
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace myHeFeng.frameWork.components.stringEncrypt
{
/// <summary>
/// 提供对称加码解密字符串的方法和函数
/// </summary>
public class DESEncrypt
{
/// <summary>
/// 系统固定字符串加密的钥匙
/// </summary>
private static readonly string DESEncryptKey = "a";
/// <summary>
/// 使用系统固定KEY的字符串加密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string encryptString(string input)
{
return __work(input, string.Empty, true);
}
/// <summary>
/// 使用自定义KEY的字符串加密
/// </summary>
/// <param name="input"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string encryptString(string input, string key)
{
return __work(input, key, true);
}
/// <summary>
/// 使用系统固定KEY的字符串解密
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string decryptionString(string input)
{
return __work(input, string.Empty, false);
}
/// <summary>
/// 使用自定义KEY的字符串解密
/// </summary>
/// <param name="input"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string decryptionString(string input, string key)
{
return __work(input, key, false);
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="key"></param>
/// <param name="isEncrypt"></param>
/// <returns></returns>
private static string __work(string input, string key, bool isEncrypt)
{
string _key;
if (string.IsNullOrEmpty(key))
{
_key = DESEncryptKey;
}
else
{
_key = key;
}
using (simple3Des sim = new simple3Des(_key))
{
if (isEncrypt)
{
return sim.EncryptData(input);
}
return sim.DecryptData(input);
}
}