/// <summary>
/// 對稱加密算法類
/// </summary>
public class SymmetricMethod
{
private SymmetricAlgorithm mobjCryptoService;
private string Key;
/// <summary>
/// 對稱加密類的構造函數
/// </summary>
public SymmetricMethod()
{
mobjCryptoService = new RijndaelManaged();
Key =
}
/// <summary>
/// 獲得密鑰
/// </summary>
/// <returns>密鑰</returns>
private byte[] GetLegalKey()
{
string sTemp = Key;
mobjCryptoService
byte[] bytTemp = mobjCryptoService
int KeyLength = bytTemp
if (sTemp
sTemp = sTemp
else if (sTemp
sTemp = sTemp
return ASCIIEncoding
}
/// <summary>
/// 獲得初始向量IV
/// </summary>
/// <returns>初試向量IV</returns>
private byte[] GetLegalIV()
{
string sTemp =
mobjCryptoService
byte[] bytTemp = mobjCryptoService
int IVLength = bytTemp
if (sTemp
sTemp = sTemp
else if (sTemp
sTemp = sTemp
return ASCIIEncoding
}
/// <summary>
/// 加密方法
/// </summary>
/// <param name=
/// <returns>經過加密的串</returns>
public string Encrypto(string Source)
{
byte[] bytIn = UTF
MemoryStream ms = new MemoryStream();
mobjCryptoService
mobjCryptoService
ICryptoTransform encrypto = mobjCryptoService
CryptoStream cs = new CryptoStream(ms
cs
cs
ms
byte[] bytOut = ms
return Convert
}
/// <summary>
/// 解密方法
/// </summary>
/// <param name=
/// <returns>經過解密的串</returns>
public string Decrypto(string Source)
{
byte[] bytIn = Convert
MemoryStream ms = new MemoryStream(bytIn
mobjCryptoService
mobjCryptoService
ICryptoTransform encrypto = mobjCryptoService
CryptoStream cs = new CryptoStream(ms
StreamReader sr = new StreamReader(cs);
return sr
}
}
///////////手動自己寫的對稱加密方法
enum TDesMode { dmEncry
public class Des
{
static readonly byte[] BitIP =
{
};
static readonly byte[] BitCP =
{
};
static readonly int[] BitExp =
{
};
static readonly byte[] BitPM =
{
};
static readonly byte[
{
{
}
{
}
{
}
{
}
{
}
{
}
{
}
{
}
};
static readonly byte[] BitPMC
{
};
static readonly byte[] BitPMC
{
};
byte[][] SubKey;
string Key;
public Des()
{
Key =
SubKey = new byte[
for (int i =
{
SubKey[i] = new byte[
}
}
void initPermutation(byte[] inData)
{
byte[] newData = new byte[
for (int i =
{
if ((inData[BitIP[i] >>
{
newData[i >>
}
}
Array
}
void conversePermutation(byte[] inData)
{
byte[] newData = new byte[
for (int i =
{
if ((inData[BitCP[i] >>
{
newData[i >>
}
}
Array
}
void expand(byte[] inData
{
Array
for (int i =
{
if ((inData[BitExp[i] >>
{
outData[i >>
}
}
}
void permutation(byte[] inData)
{
byte[] newData = new byte[
for (int i =
{
if ((inData[BitPM[i] >>
{
newData[i >>
}
}
Array
}
byte si(byte s
{
int c = (inByte &
return (byte)(sBox[s
}
void permutationChoose
{
Array
for (int i =
{
if ((inData[BitPMC
{
outData[i >>
}
}
}
void permutationChoose
{
Array
for (int i =
{
if ((inData[BitPMC
{
outData[i >>
}
}
}
void cycleMove(byte[] inData
{
for (int i =
{
inData[
inData[
inData[
inData[
inData[
}
}
static readonly byte[] bitDisplace = {
void makeKey(byte[] inKey
{
byte[] outData
byte[] key
byte[] key
byte[] key
permutationChoose
key
key
key
key
key
key
key
key
for (int i =
{
cycleMove(key
cycleMove(key
key
key
key
key
key
key
key
permutationChoose
};
}
void encry(byte[] inData
{
byte[] outBuf = new byte[
byte[] buf = new byte[
expand(inData
for (int i =
// outBuf xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
buf[
buf[
buf[
buf[
buf[
buf[
buf[
buf[
for (int i =
for (int i =
permutation(outBuf);
for (int i =
}
// inData
void desData(TDesMode desMode
{
int i
byte[] temp = new byte[
byte[] buf = new byte[
for (i =
initPermutation(outData);
if (desMode == TDesMode
{
for (i =
{
for (j =
for (j =
encry(outData
for (j =
};
for (j =
for (j =
for (j =
}
else if (desMode == TDesMode
{
for (i =
{
for (j =
for (j =
encry(outData
for (j =
};
for (j =
for (j =
for (j =
};
conversePermutation(outData);
}
byte[] Redim(byte[] arr
{
if (newSize == arr
byte[] newArr = new byte[newSize];
Array
return newArr;
}
/**/
//////////////////////////////////////////////////////////////
public byte[] EncryBytes(byte[] inData
{
byte[] tmpByte = new byte[
byte[] outByte = new byte[
if ((inData
{
throw new ArgumentException(
}
if (inData
if (keyByte
makeKey(keyByte
byte[] outData = new byte[inData
for (int i =
{
for (int j =
{
tmpByte[j] = inData[i *
}
desData(TDesMode
for (int j =
{
outData[i *
}
};
return outData;
}
public byte[] DecryBytes(byte[] inData
{
byte[] tmpByte = new byte[
byte[] outByte = new byte[
if (keyByte
makeKey(keyByte
byte[] outData = new byte[(inData
for (int i =
{
for (int j =
{
tmpByte[j] = inData[i *
}
desData(TDesMode
for (int j =
{
outData[i *
}
};
int n = outData
while (n >=
return Redim(outData
}
public string EncryStr(string Str)
{
byte[] inData = Encoding
byte[] keyByte = Encoding
byte[] tmpByte = EncryBytes(inData
StringBuilder tmpStr = new StringBuilder();
foreach (byte b in tmpByte)
{
tmpStr
}
return tmpStr
}
public string DecryStr(string Str)
{
byte[] inData = new byte[Str
for (int i =
{
inData[i] = (byte)Str[i];
}
byte[] keyByte = Encoding
byte[] tmpByte = DecryBytes(inData
return Encoding
}
public string EncryStrHex(string Str)
{
byte[] inData = Encoding
byte[] keyByte = Encoding
byte[] tmpByte = EncryBytes(inData
StringBuilder tmpStr = new StringBuilder();
foreach (byte b in tmpByte)
{
tmpStr
}
return tmpStr
}
public string DecryStrHex(string StrHex)
{
if (StrHex
{
throw new ArgumentException(
}
byte[] inData = new byte[StrHex
for (int i =
{
inData[i /
}
byte[] keyByte = Encoding
byte[] tmpByte = DecryBytes(inData
return Encoding
}
}// End of class Des
From:http://tw.wingwit.com/Article/program/net/201311/12328.html