.net常用加密解密方法

ContractedBlock.gif ExpandedBlockStart.gif UnicodeEncoding 加密
 
   
private byte [] _power;
/// <summary>
/// 用户权限
/// </summary>
public byte [] Power
{
set { _power = value; }
get { return _power; }
}
sql:
Power binary
4000
解密:
Encoding.Unicode.GetString(model.Power);
加密:
byte [] Power = new UnicodeEncoding().GetBytes( this .HidPowers.Value);
model.Power
= Power;

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
using System;
using System.Security.Cryptography;
using System.Text;
namespace BeidouWY.Common.DEncrypt
{
/// <summary>
/// Encrypt 的摘要说明。
/// LiTianPing
/// </summary>
public class DEncrypt
{
/// <summary>
/// 构造方法
/// </summary>
public DEncrypt()
{
}

#region 使用 缺省密钥字符串 加密/解密string

/// <summary>
/// 使用缺省密钥字符串加密string
/// </summary>
/// <param name="original"> 明文 </param>
/// <returns> 密文 </returns>
public static string Encrypt( string original)
{
return Encrypt(original, " MATICSOFT " );
}
/// <summary>
/// 使用缺省密钥字符串解密string
/// </summary>
/// <param name="original"> 密文 </param>
/// <returns> 明文 </returns>
public static string Decrypt( string original)
{
return Decrypt(original, " MATICSOFT " ,System.Text.Encoding.Default);
}

#endregion

#region 使用 给定密钥字符串 加密/解密string
/// <summary>
/// 使用给定密钥字符串加密string
/// </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>
/// 使用给定密钥字符串解密string
/// </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>
/// 使用给定密钥字符串解密string,返回指定编码方式明文
/// </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));
}
#endregion

#region 使用 缺省密钥字符串 加密/解密/byte[]
/// <summary>
/// 使用缺省密钥字符串解密byte[]
/// </summary>
/// <param name="encrypted"> 密文 </param>
/// <param name="key"> 密钥 </param>
/// <returns> 明文 </returns>
public static byte [] Decrypt( byte [] encrypted)
{
byte [] key = System.Text.Encoding.Default.GetBytes( " MATICSOFT " );
return Decrypt(encrypted,key);
}
/// <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( " MATICSOFT " );
return Encrypt(original,key);
}
#endregion

#region 使用 给定密钥 加密/解密/byte[]

/// <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);
}

#endregion




}
}

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
using System;
using System.Security.Cryptography;
using System.Text;
namespace BeidouWY.Common.DEncrypt
{
/// <summary>
/// DES加密/解密类。
/// LiTianPing
/// </summary>
public class DESEncrypt
{
public DESEncrypt()
{
}

#region ========加密========

/// <summary>
/// 加密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Encrypt( string Text)
{
return Encrypt(Text, " MATICSOFT " );
}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Encrypt( string Text, string sKey)
{
DESCryptoServiceProvider des
= new DESCryptoServiceProvider();
byte [] inputByteArray;
inputByteArray
= Encoding.Default.GetBytes(Text);
des.Key
= ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, " md5 " ).Substring( 0 , 8 ));
des.IV
= ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, " md5 " ).Substring( 0 , 8 ));
System.IO.MemoryStream ms
= new System.IO.MemoryStream();
CryptoStream cs
= new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,
0 ,inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret
= new StringBuilder();
foreach ( byte b in ms.ToArray())
{
ret.AppendFormat(
" {0:X2} " ,b);
}
return ret.ToString();
}

#endregion

#region ========解密========


/// <summary>
/// 解密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Decrypt( string Text)
{
return Decrypt(Text, " MATICSOFT " );
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt( string Text, string sKey)
{
DESCryptoServiceProvider des
= new DESCryptoServiceProvider();
int len;
len
= Text.Length / 2 ;
byte [] inputByteArray = new byte [len];
int x,i;
for (x = 0 ;x < len;x ++ )
{
i
= Convert.ToInt32(Text.Substring(x * 2 , 2 ), 16 );
inputByteArray[x]
= ( byte )i;
}
des.Key
= ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, " md5 " ).Substring( 0 , 8 ));
des.IV
= ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, " md5 " ).Substring( 0 , 8 ));
System.IO.MemoryStream ms
= new System.IO.MemoryStream();
CryptoStream cs
= new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,
0 ,inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}

#endregion


}
}

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
using System;
using System.Text;
using System.Security.Cryptography;
namespace BeidouWY.Common.DEncrypt
{
/// <summary>
/// 得到随机安全码(哈希加密)。
/// </summary>
public class HashEncode
{
public HashEncode()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 得到随机哈希加密字符串
/// </summary>
/// <returns></returns>
public static string GetSecurity()
{
string Security = HashEncoding(GetRandomValue());
return Security;
}
/// <summary>
/// 得到一个随机数值
/// </summary>
/// <returns></returns>
public static string GetRandomValue()
{
Random Seed
= new Random();
string RandomVaule = Seed.Next( 1 , int .MaxValue).ToString();
return RandomVaule;
}
/// <summary>
/// 哈希加密一个字符串
/// </summary>
/// <param name="Security"></param>
/// <returns></returns>
public static string HashEncoding( string Security)
{
byte [] Value;
UnicodeEncoding Code
= new UnicodeEncoding();
byte [] Message = Code.GetBytes(Security);
SHA512Managed Arithmetic
= new SHA512Managed();
Value
= Arithmetic.ComputeHash(Message);
Security
= "" ;
foreach ( byte o in Value)
{
Security
+= ( int ) o + " O " ;
}
return Security;
}
}
}

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
using System;
using System.Security;
using System.Security.Cryptography;
using System.Text;

namespace BeidouWY.Common.DEncrypt
{
/// <summary>
/// MD5Helper 的摘要说明。
/// </summary>
public class MD5Helper
{
/// <summary>
/// 得到一个加密的字符串
/// </summary>
/// <param name="strIn"> 原始字符串 </param>
/// <returns> 加密后字符串 </returns>
public static string GetMD5String( string strIn)
{
byte [] b = Encoding.Default.GetBytes(strIn);

b
= new MD5CryptoServiceProvider().ComputeHash(b);
string strOut = "" ;
for ( int i = 0 ;i < b.Length;i ++ )
{
strOut
+= b[i].ToString( " x " ).PadLeft( 2 , ' 2 ' );
}
return strOut;
}
}
}

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
using System;
using System.Text;
using System.Security.Cryptography;
namespace BeidouWY.Common.DEncrypt
{
/// <summary>
/// RSA加密解密及RSA签名和验证
/// </summary>
public class RSACryption
{
public RSACryption()
{
}


#region RSA 加密解密

#region RSA 的密钥产生

/// <summary>
/// RSA 的密钥产生 产生私钥 和公钥
/// </summary>
/// <param name="xmlKeys"></param>
/// <param name="xmlPublicKey"></param>
public void RSAKey( out string xmlKeys, out string xmlPublicKey)
{
System.Security.Cryptography.RSACryptoServiceProvider rsa
= new RSACryptoServiceProvider();
xmlKeys
= rsa.ToXmlString( true );
xmlPublicKey
= rsa.ToXmlString( false );
}
#endregion

#region RSA的加密函数
// ##############################################################################
// RSA 方式加密
// 说明KEY必须是XML的行式,返回的是字符串
// 在有一点需要说明!!该加密方式有 长度 限制的!!
// ##############################################################################

// RSA的加密函数 string
public string RSAEncrypt( string xmlPublicKey, string m_strEncryptString )
{

byte [] PlainTextBArray;
byte [] CypherTextBArray;
string Result;
RSACryptoServiceProvider rsa
= new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray
= ( new UnicodeEncoding()).GetBytes(m_strEncryptString);
CypherTextBArray
= rsa.Encrypt(PlainTextBArray, false );
Result
= Convert.ToBase64String(CypherTextBArray);
return Result;

}
// RSA的加密函数 byte[]
public string RSAEncrypt( string xmlPublicKey, byte [] EncryptString )
{

byte [] CypherTextBArray;
string Result;
RSACryptoServiceProvider rsa
= new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray
= rsa.Encrypt(EncryptString, false );
Result
= Convert.ToBase64String(CypherTextBArray);
return Result;

}
#endregion

#region RSA的解密函数
// RSA的解密函数 string
public string RSADecrypt( string xmlPrivateKey, string m_strDecryptString )
{
byte [] PlainTextBArray;
byte [] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa
= new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
PlainTextBArray
= Convert.FromBase64String(m_strDecryptString);
DypherTextBArray
= rsa.Decrypt(PlainTextBArray, false );
Result
= ( new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;

}

// RSA的解密函数 byte
public string RSADecrypt( string xmlPrivateKey, byte [] DecryptString )
{
byte [] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa
= new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
DypherTextBArray
= rsa.Decrypt(DecryptString, false );
Result
= ( new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;

}
#endregion

#endregion

#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 ;
}

// 获取Hash描述表
public bool GetHash( string m_strSource, ref string strHashData)
{

// 从字符串中取得Hash描述
byte [] Buffer;
byte [] HashData;
System.Security.Cryptography.HashAlgorithm MD5
= System.Security.Cryptography.HashAlgorithm.Create( " MD5 " );
Buffer
= System.Text.Encoding.GetEncoding( " GB2312 " ).GetBytes(m_strSource);
HashData
= MD5.ComputeHash(Buffer);

strHashData
= Convert.ToBase64String(HashData);
return true ;

}

// 获取Hash描述表
public bool GetHash(System.IO.FileStream objFile, ref byte [] HashData)
{

// 从文件中取得Hash描述
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create( " MD5 " );
HashData
= MD5.ComputeHash(objFile);
objFile.Close();

return true ;

}

// 获取Hash描述表
public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
{

// 从文件中取得Hash描述
byte [] HashData;
System.Security.Cryptography.HashAlgorithm MD5
= System.Security.Cryptography.HashAlgorithm.Create( " MD5 " );
HashData
= MD5.ComputeHash(objFile);
objFile.Close();

strHashData
= Convert.ToBase64String(HashData);

return true ;

}
#endregion

#region RSA签名
// RSA签名
public bool SignatureFormatter( string p_strKeyPrivate, byte [] HashbyteSignature, ref byte [] EncryptedSignatureData)
{

System.Security.Cryptography.RSACryptoServiceProvider RSA
= new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter
= new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
// 设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm( " MD5 " );
// 执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

return true ;

}

// RSA签名
public bool SignatureFormatter( string p_strKeyPrivate, byte [] HashbyteSignature, ref string m_strEncryptedSignatureData)
{

byte [] EncryptedSignatureData;

System.Security.Cryptography.RSACryptoServiceProvider RSA
= new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter
= new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
// 设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm( " MD5 " );
// 执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

m_strEncryptedSignatureData
= Convert.ToBase64String(EncryptedSignatureData);

return true ;

}

// RSA签名
public bool SignatureFormatter( string p_strKeyPrivate, string m_strHashbyteSignature, ref byte [] EncryptedSignatureData)
{

byte [] HashbyteSignature;

HashbyteSignature
= Convert.FromBase64String(m_strHashbyteSignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA
= new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter
= new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
// 设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm( " MD5 " );
// 执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

return true ;

}

// RSA签名
public bool SignatureFormatter( string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData)
{

byte [] HashbyteSignature;
byte [] EncryptedSignatureData;

HashbyteSignature
= Convert.FromBase64String(m_strHashbyteSignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA
= new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter
= new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
// 设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm( " MD5 " );
// 执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

m_strEncryptedSignatureData
= Convert.ToBase64String(EncryptedSignatureData);

return true ;

}
#endregion

#region RSA 签名验证

public bool SignatureDeformatter( string p_strKeyPublic, byte [] HashbyteDeformatter, byte [] DeformatterData)
{

System.Security.Cryptography.RSACryptoServiceProvider RSA
= new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter
= new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
// 指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm( " MD5 " );

if (RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true ;
}
else
{
return false ;
}

}

public bool SignatureDeformatter( string p_strKeyPublic, string p_strHashbyteDeformatter, byte [] DeformatterData)
{

byte [] HashbyteDeformatter;

HashbyteDeformatter
= Convert.FromBase64String(p_strHashbyteDeformatter);

System.Security.Cryptography.RSACryptoServiceProvider RSA
= new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter
= new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
// 指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm( " MD5 " );

if (RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true ;
}
else
{
return false ;
}

}

public bool SignatureDeformatter( string p_strKeyPublic, byte [] HashbyteDeformatter, string p_strDeformatterData)
{

byte [] DeformatterData;

System.Security.Cryptography.RSACryptoServiceProvider RSA
= new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter
= new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
// 指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm( " MD5 " );

DeformatterData
= Convert.FromBase64String(p_strDeformatterData);

if (RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true ;
}
else
{
return false ;
}

}

public bool SignatureDeformatter( string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
{

byte [] DeformatterData;
byte [] HashbyteDeformatter;

HashbyteDeformatter
= Convert.FromBase64String(p_strHashbyteDeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA
= new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter
= new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
// 指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm( " MD5 " );

DeformatterData
= Convert.FromBase64String(p_strDeformatterData);

if (RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true ;
}
else
{
return false ;
}

}


#endregion


#endregion

}
}

 

转载于:https://www.cnblogs.com/qinweilong/archive/2010/06/30/1768535.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值