知识大全 C#对称加密解密算法
Posted 知
篇首语:苦心人天不负,卧薪尝胆,三千越甲可吞吴。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 C#对称加密解密算法相关的知识,希望对你有一定的参考价值。
C#对称加密解密算法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
/// <summary> /// 对称加密算法类 使用系统自带的函数 /// </summary> public class SymmetricMethod
private SymmetricAlgorithm mobjCryptoService; private string Key; /// <summary> /// 对称加密类的构造函数 /// </summary> public SymmetricMethod() mobjCryptoService = new RijndaelManaged(); Key = Guz(%&hj x H$yuBI FtmaT &fvHUFCy *h%(HilJ$lhj!y &(*jkP jH ; /// <summary> /// 获得密钥 /// </summary> /// <returns>密钥</returns> private byte[] GetLegalKey() string sTemp = Key; mobjCryptoService GenerateKey(); byte[] bytTemp = mobjCryptoService Key; int KeyLength = bytTemp Length; if (sTemp Length > KeyLength) sTemp = sTemp Substring( KeyLength); else if (sTemp Length < KeyLength) sTemp = sTemp PadRight(KeyLength ); return ASCIIEncoding ASCII GetBytes(sTemp); /// <summary> /// 获得初始向量IV /// </summary> /// <returns>初试向量IV</returns> private byte[] GetLegalIV() string sTemp = E ghj*Ghg !rNIfb& GUY GfghUb#er HBh(u%g HJ($jhWk &!hg ui%$hjk ; mobjCryptoService GenerateIV(); byte[] bytTemp = mobjCryptoService IV; int IVLength = bytTemp Length; if (sTemp Length > IVLength) sTemp = sTemp Substring( IVLength); else if (sTemp Length < IVLength) sTemp = sTemp PadRight(IVLength ); return ASCIIEncoding ASCII GetBytes(sTemp); /// <summary> /// 加密方法 /// </summary> /// <param name= Source >待加密的串</param> /// <returns>经过加密的串</returns> public string Encrypto(string Source) byte[] bytIn = UTF Encoding UTF GetBytes(Source); MemoryStream ms = new MemoryStream(); mobjCryptoService Key = GetLegalKey(); mobjCryptoService IV = GetLegalIV(); ICryptoTransform encrypto = mobjCryptoService CreateEncryptor(); CryptoStream cs = new CryptoStream(ms encrypto CryptoStreamMode Write); cs Write(bytIn bytIn Length); cs FlushFinalBlock(); ms Close(); byte[] bytOut = ms ToArray(); return Convert ToBase String(bytOut); /// <summary> /// 解密方法 /// </summary> /// <param name= Source >待解密的串</param> /// <returns>经过解密的串</returns> public string Decrypto(string Source) byte[] bytIn = Convert FromBase String(Source); MemoryStream ms = new MemoryStream(bytIn bytIn Length); mobjCryptoService Key = GetLegalKey(); mobjCryptoService IV = GetLegalIV(); ICryptoTransform encrypto = mobjCryptoService CreateDecryptor(); CryptoStream cs = new CryptoStream(ms encrypto CryptoStreamMode Read); StreamReader sr = new StreamReader(cs); return sr ReadToEnd();
///////////手动自己写的对称加密方法
enum TDesMode dmEncry dmDecry ;
public class Des static readonly byte[] BitIP = ;
static readonly byte[] BitCP = ;
static readonly int[] BitExp = ;
static readonly byte[] BitPM = ;
static readonly byte[ ] sBox = ;
static readonly byte[] BitPMC = ;
static readonly byte[] BitPMC = ;
byte[][] SubKey;
string Key;
public Des() Key = Guz(%&hj x H$yuBI FtmaT &fvHUFCy *h%(HilJ$lhj!y &(*jkP jH ; SubKey = new byte[ ][]; for (int i = ; i < SubKey Length; i++) SubKey[i] = new byte[ ];
void initPermutation(byte[] inData) byte[] newData = new byte[ ]; for (int i = ; i < ; i++) if ((inData[BitIP[i] >> ] & ( << ( (BitIP[i] & x )))) != ) newData[i >> ] |= (byte)( << ( (i & x ))); Array Copy(newData inData );
void conversePermutation(byte[] inData) byte[] newData = new byte[ ]; for (int i = ; i < ; i++) if ((inData[BitCP[i] >> ] & ( << ( (BitCP[i] & x )))) != ) newData[i >> ] |= (byte)( << ( (i & x ))); Array Copy(newData inData );
void expand(byte[] inData byte[] outData) Array Clear(outData ); for (int i = ; i < ; i++) if ((inData[BitExp[i] >> ] & ( << ( (BitExp[i] & x )))) != ) outData[i >> ] |= (byte)( << ( (i & x )));
void permutation(byte[] inData) byte[] newData = new byte[ ]; for (int i = ; i < ; i++) if ((inData[BitPM[i] >> ] & ( << ( (BitPM[i] & x )))) != ) newData[i >> ] |= (byte)( << ( (i & x ))); Array Copy(newData inData );
byte si(byte s byte inByte) int c = (inByte & x ) | ((inByte & x e) >> ) | ((inByte & x ) << ); return (byte)(sBox[s c] & x f);
void permutationChoose (byte[] inData byte[] outData) Array Clear(outData ); for (int i = ; i < ; i++) if ((inData[BitPMC [i] >> ] & ( << ( (BitPMC [i] & x )))) != ) outData[i >> ] |= (byte)( << ( (i & x )));
void permutationChoose (byte[] inData byte[] outData) Array Clear(outData ); for (int i = ; i < ; i++) if ((inData[BitPMC [i] >> ] & ( << ( (BitPMC [i] & x )))) != ) outData[i >> ] |= (byte)( << ( (i & x )));
void cycleMove(byte[] inData byte bitMove) for (int i = ; i < bitMove; i++) inData[ ] = (byte)((inData[ ] << ) | (inData[ ] >> )); inData[ ] = (byte)((inData[ ] << ) | (inData[ ] >> )); inData[ ] = (byte)((inData[ ] << ) | (inData[ ] >> )); inData[ ] = (byte)((inData[ ] << ) | ((inData[ ] & x ) >> )); inData[ ] = (byte)(inData[ ] & x f);
static readonly byte[] bitDisplace = ;
void makeKey(byte[] inKey byte[][] outKey) byte[] outData = new byte[ ]; byte[] key l = new byte[ ]; byte[] key r = new byte[ ]; byte[] key o = new byte[ ];
permutationChoose (inKey outData ); key l[ ] = (byte)(outData [ ] >> ); key l[ ] = (byte)((outData [ ] << ) | (outData [ ] >> )); key l[ ] = (byte)((outData [ ] << ) | (outData [ ] >> )); key l[ ] = (byte)((outData [ ] << ) | (outData [ ] >> )); key r[ ] = (byte)(outData [ ] & x f); key r[ ] = (byte)(outData [ ]); key r[ ] = (byte)(outData [ ]); key r[ ] = (byte)(outData [ ]);
for (int i = ; i < ; i++) cycleMove(key l bitDisplace[i]); cycleMove(key r bitDisplace[i]); key o[ ] = (byte)((key l[ ] << ) | (key l[ ] >> )); key o[ ] = (byte)((key l[ ] << ) | (key l[ ] >> )); key o[ ] = (byte)((key l[ ] << ) | (key l[ ] >> )); key o[ ] = (byte)((key l[ ] << ) | (key r[ ])); key o[ ] = (byte)(key r[ ]); key o[ ] = (byte)(key r[ ]); key o[ ] = (byte)(key r[ ]); permutationChoose (key o outKey[i]); ;
void encry(byte[] inData byte[] subKey byte[] outData) byte[] outBuf = new byte[ ]; byte[] buf = new byte[ ];
expand(inData outBuf); for (int i = ; i < ; i++) outBuf[i] = (byte)(outBuf[i] ^ subKey[i]); // outBuf xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx buf[ ] = (byte)(outBuf[ ] >> ); //xxxxxx > buf[ ] = (byte)(((outBuf[ ] & x ) << ) | (outBuf[ ] >> )); // < xx xxxx > buf[ ] = (byte)(((outBuf[ ] & x f) << ) | (outBuf[ ] >> )); // < xxxx xx > buf[ ] = (byte)(outBuf[ ] & x f); // xxxxxx buf[ ] = (byte)(outBuf[ ] >> ); // xxxxxx buf[ ] = (byte)(((outBuf[ ] & x ) << ) | (outBuf[ ] >> )); // xx xxxx buf[ ] = (byte)(((outBuf[ ] & x f) << ) | (outBuf[ ] >> )); // xxxx xx buf[ ] = (byte)(outBuf[ ] & x f); // xxxxxx for (int i = ; i < ; i++) buf[i] = si((byte)i buf[i]); for (int i = ; i < ; i++) outBuf[i] = (byte)((buf[i * ] << ) | buf[i * + ]); permutation(outBuf); for (int i = ; i < ; i++) outData[i] = outBuf[i];
// inData outData 都为 Bytes 否则出错 void desData(TDesMode desMode byte[] inData byte[] outData) int i j; byte[] temp = new byte[ ]; byte[] buf = new byte[ ];
for (i = ; i < ; i++) outData[i] = inData[i]; initPermutation(outData); if (desMode == TDesMode dmEncry) for (i = ; i < ; i++) for (j = ; j < ; j++) temp[j] = outData[j]; //temp = Ln for (j = ; j < ; j++) outData[j] = outData[j + ]; //Ln+ = Rn encry(outData SubKey[i] buf); //Rn ==Kn==> buf for (j = ; j < ; j++) outData[j + ] = (byte)(temp[j] ^ buf[j]); //Rn+ = Ln^buf ; for (j = ; j < ; j++) temp[j] = outData[j + ]; for (j = ; j < ; j++) outData[j + ] = outData[j]; for (j = ; j < ; j++) outData[j] = temp[j]; else if (desMode == TDesMode dmDecry) for (i = ; i >= ; i ) for (j = ; j < ; j++) temp[j] = outData[j]; for (j = ; j < ; j++) outData[j] = outData[j + ]; encry(outData SubKey[i] buf); for (j = ; j < ; j++) outData[j + ] = (byte)(temp[j] ^ buf[j]); ; for (j = ; j < ; j++) temp[j] = outData[j + ]; for (j = ; j < ; j++) outData[j + ] = outData[j]; for (j = ; j < ; j++) outData[j] = temp[j]; ; conversePermutation(outData);
byte[] Redim(byte[] arr int newSize) if (newSize == arr Length) return arr; byte[] newArr = new byte[newSize]; Array Copy(arr newArr Math Min(arr Length newSize)); return newArr;
/**/ //////////////////////////////////////////////////////////////
public byte[] EncryBytes(byte[] inData byte[] keyByte) byte[] tmpByte = new byte[ ]; byte[] outByte = new byte[ ];
if ((inData Length > ) && (inData[inData Length ] == )) throw new ArgumentException( The last byte is inData ); if (inData Length % != ) inData = Redim(inData (inData Length + ) / * ); if (keyByte Length != ) keyByte = Redim(keyByte ); makeKey(keyByte SubKey);
byte[] outData = new byte[inData Length]; for (int i = ; i < inData Length / ; i++) for (int j = ; j < ; j++) tmpByte[j] = inData[i * + j]; desData(TDesMode dmEncry tmpByte outByte); for (int j = ; j < ; j++) outData[i * + j] = outByte[j]; ;
return outData;
public byte[] DecryBytes(byte[] inData byte[] keyByte) byte[] tmpByte = new byte[ ]; byte[] outByte = new byte[ ];
if (keyByte Length != ) keyByte = Redim(keyByte ); makeKey(keyByte SubKey);
byte[] outData = new byte[(inData Length + ) / * ]; for (int i = ; i < inData Length / ; i++) for (int j = ; j < ; j++) tmpByte[j] = inData[i * + j]; desData(TDesMode dmDecry tmpByte outByte); for (int j = ; j < ; j++) outData[i * + j] = outByte[j]; ;
int n = outData Length ; while (n >= && outData[n] == ) n; return Redim(outData n + );
public string EncryStr(string Str) byte[] inData = Encoding UTF GetBytes(Str); byte[] keyByte = Encoding UTF GetBytes(Key); byte[] tmpByte = EncryBytes(inData keyByte); StringBuilder tmpStr = new StringBuilder(); foreach (byte b in tmpByte) tmpStr Append((char)b); return tmpStr ToString();
public string DecryStr(string Str) byte[] inData = new byte[Str Length]; for (int i = ; i < Str Length; i++) inData[i] = (byte)Str[i]; byte[] keyByte = Encoding UTF GetBytes(Key); byte[] tmpByte = DecryBytes(inData keyByte); return Encoding UTF GetString(tmpByte);
public string EncryStrHex(string Str) byte[] inData = Encoding UTF GetBytes(Str); byte[] keyByte = Encoding UTF GetBytes(Key); byte[] tmpByte = EncryBytes(inData keyByte); StringBuilder tmpStr = new StringBuilder(); foreach (byte b in tmpByte) tmpStr AppendFormat( :X b); return tmpStr ToString();
cha138/Article/program/net/201311/12328相关参考
使用ASP加密算法加密你的数据 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 简介 首先简
.NET中的DES对称加密 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! DES是一种对称加密(
java中使用MD5加密算法进行加密 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 在各种应用系
AES对称加密例子 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 什么是AES AES是一种对
Oracle数据库替代加密算法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 替代密码算法的原理
.NET对称加密实践(新手教程) 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 在使用NET框架
使用DES对称加密代码,支持中文 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!//名称空间usin
VB.NET关于加密算法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 前几天由于需要去研究了一
.NET中的密码学--对称加密 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 介绍 在net之
JAVA里面RSA加密算法的使用 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 打算写这个类用于