c#

位置:IT落伍者 >> c# >> 浏览文章

C#对称加密解密算法


发布日期:2018年06月24日
 
C#对称加密解密算法

/// <summary>

/// 对称加密算法类使用系统自带的函数

/// </summary>

public class SymmetricMethod

{

private SymmetricAlgorithm mobjCryptoService;

private string Key;

/// <summary>

/// 对称加密类的构造函数

/// </summary>

public SymmetricMethod()

{

mobjCryptoService = new RijndaelManaged();

Key = Guz(%&hjxH$yuBIFtmaT&fvHUFCy*h%(HilJ$lhj!y&(*jkPjH;

}

/// <summary>

/// 获得密钥

/// </summary>

/// <returns>密钥</returns>

private byte[] GetLegalKey()

{

string sTemp = Key;

mobjCryptoServiceGenerateKey();

byte[] bytTemp = mobjCryptoServiceKey;

int KeyLength = bytTempLength;

if (sTempLength > KeyLength)

sTemp = sTempSubstring( KeyLength);

else if (sTempLength < KeyLength)

sTemp = sTempPadRight(KeyLength );

return ASCIIEncodingASCIIGetBytes(sTemp);

}

/// <summary>

/// 获得初始向量IV

/// </summary>

/// <returns>初试向量IV</returns>

private byte[] GetLegalIV()

{

string sTemp = Eghj*Ghg!rNIfb&GUYGfghUb#erHBh(u%gHJ($jhWk&!hgui%$hjk;

mobjCryptoServiceGenerateIV();

byte[] bytTemp = mobjCryptoServiceIV;

int IVLength = bytTempLength;

if (sTempLength > IVLength)

sTemp = sTempSubstring( IVLength);

else if (sTempLength < IVLength)

sTemp = sTempPadRight(IVLength );

return ASCIIEncodingASCIIGetBytes(sTemp);

}

/// <summary>

/// 加密方法

/// </summary>

/// <param name=Source>待加密的串</param>

/// <returns>经过加密的串</returns>

public string Encrypto(string Source)

{

byte[] bytIn = UTFEncodingUTFGetBytes(Source);

MemoryStream ms = new MemoryStream();

mobjCryptoServiceKey = GetLegalKey();

mobjCryptoServiceIV = GetLegalIV();

ICryptoTransform encrypto = mobjCryptoServiceCreateEncryptor();

CryptoStream cs = new CryptoStream(ms encrypto CryptoStreamModeWrite);

csWrite(bytIn bytInLength);

csFlushFinalBlock();

msClose();

byte[] bytOut = msToArray();

return ConvertToBaseString(bytOut);

}

/// <summary>

/// 解密方法

/// </summary>

/// <param name=Source>待解密的串</param>

/// <returns>经过解密的串</returns>

public string Decrypto(string Source)

{

byte[] bytIn = ConvertFromBaseString(Source);

MemoryStream ms = new MemoryStream(bytIn bytInLength);

mobjCryptoServiceKey = GetLegalKey();

mobjCryptoServiceIV = GetLegalIV();

ICryptoTransform encrypto = mobjCryptoServiceCreateDecryptor();

CryptoStream cs = new CryptoStream(ms encrypto CryptoStreamModeRead);

StreamReader sr = new StreamReader(cs);

return srReadToEnd();

}

}

///////////手动自己写的对称加密方法

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(%&hjxH$yuBIFtmaT&fvHUFCy*h%(HilJ$lhj!y&(*jkPjH;

SubKey = new byte[][];

for (int i = ; i < SubKeyLength; 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)));

}

}

ArrayCopy(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)));

}

}

ArrayCopy(newData inData );

}

void expand(byte[] inData byte[] outData)

{

ArrayClear(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)));

}

}

ArrayCopy(newData inData );

}

byte si(byte s byte inByte)

{

int c = (inByte & x) | ((inByte & xe) >> ) | ((inByte & x) << );

return (byte)(sBox[s c] & xf);

}

void permutationChoose(byte[] inData byte[] outData)

{

ArrayClear(outData );

for (int i = ; i < ; i++)

{

if ((inData[BitPMC[i] >> ] & ( << ( (BitPMC[i] & x)))) != )

{

outData[i >> ] |= (byte)( << ( (i & x)));

}

}

}

void permutationChoose(byte[] inData byte[] outData)

{

ArrayClear(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[] & xf);

}

}

static readonly byte[] bitDisplace = { };

void makeKey(byte[] inKey byte[][] outKey)

{

byte[] outData = new byte[];

byte[] keyl = new byte[];

byte[] keyr = new byte[];

byte[] keyo = new byte[];

permutationChoose(inKey outData);

keyl[] = (byte)(outData[] >> );

keyl[] = (byte)((outData[] << ) | (outData[] >> ));

keyl[] = (byte)((outData[] << ) | (outData[] >> ));

keyl[] = (byte)((outData[] << ) | (outData[] >> ));

keyr[] = (byte)(outData[] & xf);

keyr[] = (byte)(outData[]);

keyr[] = (byte)(outData[]);

keyr[] = (byte)(outData[]);

for (int i = ; i < ; i++)

{

cycleMove(keyl bitDisplace[i]);

cycleMove(keyr bitDisplace[i]);

keyo[] = (byte)((keyl[] << ) | (keyl[] >> ));

keyo[] = (byte)((keyl[] << ) | (keyl[] >> ));

keyo[] = (byte)((keyl[] << ) | (keyl[] >> ));

keyo[] = (byte)((keyl[] << ) | (keyr[]));

keyo[] = (byte)(keyr[]);

keyo[] = (byte)(keyr[]);

keyo[] = (byte)(keyr[]);

permutationChoose(keyo 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[] & xf) << ) | (outBuf[] >> )); // < xxxx xx >

buf[] = (byte)(outBuf[] & xf); // xxxxxx

buf[] = (byte)(outBuf[] >> ); // xxxxxx

buf[] = (byte)(((outBuf[] & x) << ) | (outBuf[] >> )); // xx xxxx

buf[] = (byte)(((outBuf[] & xf) << ) | (outBuf[] >> )); // xxxx xx

buf[] = (byte)(outBuf[] & xf); // 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 == TDesModedmEncry)

{

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 == TDesModedmDecry)

{

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 == arrLength) return arr;

byte[] newArr = new byte[newSize];

ArrayCopy(arr newArr MathMin(arrLength newSize));

return newArr;

}

/**/

//////////////////////////////////////////////////////////////

public byte[] EncryBytes(byte[] inData byte[] keyByte)

{

byte[] tmpByte = new byte[];

byte[] outByte = new byte[];

if ((inDataLength > ) && (inData[inDataLength ] == ))

{

throw new ArgumentException(The last byte is inData);

}

if (inDataLength % != ) inData = Redim(inData (inDataLength + ) / * );

if (keyByteLength != ) keyByte = Redim(keyByte );

makeKey(keyByte SubKey);

byte[] outData = new byte[inDataLength];

for (int i = ; i < inDataLength / ; i++)

{

for (int j = ; j < ; j++)

{

tmpByte[j] = inData[i * + j];

}

desData(TDesModedmEncry 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 (keyByteLength != ) keyByte = Redim(keyByte );

makeKey(keyByte SubKey);

byte[] outData = new byte[(inDataLength + ) / * ];

for (int i = ; i < inDataLength / ; i++)

{

for (int j = ; j < ; j++)

{

tmpByte[j] = inData[i * + j];

}

desData(TDesModedmDecry tmpByte outByte);

for (int j = ; j < ; j++)

{

outData[i * + j] = outByte[j];

}

};

int n = outDataLength ;

while (n >= && outData[n] == ) n;

return Redim(outData n + );

}

public string EncryStr(string Str)

{

byte[] inData = EncodingUTFGetBytes(Str);

byte[] keyByte = EncodingUTFGetBytes(Key);

byte[] tmpByte = EncryBytes(inData keyByte);

StringBuilder tmpStr = new StringBuilder();

foreach (byte b in tmpByte)

{

tmpStrAppend((char)b);

}

return tmpStrToString();

}

public string DecryStr(string Str)

{

byte[] inData = new byte[StrLength];

for (int i = ; i < StrLength; i++)

{

inData[i] = (byte)Str[i];

}

byte[] keyByte = EncodingUTFGetBytes(Key);

byte[] tmpByte = DecryBytes(inData keyByte);

return EncodingUTFGetString(tmpByte);

}

public string EncryStrHex(string Str)

{

byte[] inData = EncodingUTFGetBytes(Str);

byte[] keyByte = EncodingUTFGetBytes(Key);

byte[] tmpByte = EncryBytes(inData keyByte);

StringBuilder tmpStr = new StringBuilder();

foreach (byte b in tmpByte)

{

tmpStrAppendFormat({:X} b);

}

return tmpStrToString();

}

public string DecryStrHex(string StrHex)

{

if (StrHexLength % != )

{

throw new ArgumentException(String length must be even StrHex);

}

byte[] inData = new byte[StrHexLength / ];

for (int i = ; i < StrHexLength; i += )

{

inData[i / ] = (byte)(UriFromHex(StrHex[i]) * + UriFromHex(StrHex[i + ]));

}

byte[] keyByte = EncodingUTFGetBytes(Key);

byte[] tmpByte = DecryBytes(inData keyByte);

return EncodingUTFGetString(tmpByte);

}

}// End of class Des

               

上一篇:.Net FrameWork SDK文档例子演示

下一篇:.net方法参数注释编写实例