46
sTemp=sTemp.PadRight(IVLength,'');
47
returnASCIIEncoding.ASCII.GetBytes(sTemp);
48
}
49
///
50
///加密方法
51
///
52
///待加密的串
53
///经过加密的串
54
publicstringEncrypto(stringSource)
55
{
56
byte[]bytIn=UTF8Encoding.UTF8.GetBytes(Source);
57
MemoryStreamms=newMemoryStream();
58
mobjCryptoService.Key=GetLegalKey();
59
mobjCryptoService.IV=GetLegalIV();
60
ICryptoTransformencrypto=mobjCryptoService.CreateEncryptor();
61
CryptoStreamcs=newCryptoStream(ms,encrypto,CryptoStreamMode.Write);
62
cs.Write(bytIn,0,bytIn.Length);
63
cs.FlushFinalBlock();
64
ms.Close();
65
byte[]bytOut=ms.ToArray();
66
returnConvert.ToBase64String(bytOut);
67
}
68
///
69
///解密方法
70
///
71
///待解密的串
72
///经过解密的串
73
publicstringDecrypto(stringSource)
74
{
75
byte[]bytIn=Convert.FromBase64String(Source);
76
MemoryStreamms=newMemoryStream(bytIn,0,bytIn.Length);
77
mobjCryptoService.Key=GetLegalKey();
78
mobjCryptoService.IV=GetLegalIV();
79
ICryptoTransformencrypto=mobjCryptoService.CreateDecryptor();
80
CryptoStreamcs=newCryptoStream(ms,encrypto,CryptoStreamMode.Read);
81
StreamReadersr=newStreamReader(cs);
82
returnsr.ReadToEnd();
83
}
viewsourceprint?
01
方法五:
02
usingSystem.IO;
03
usingSystem.Security.Cryptography;
04
usingSystem.Text;
05
06
...
07
08
//默认密钥向量
09
privatestaticbyte[]Keys={0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF};
10
///
11
///DES加密字符串
12
///
13
///待加密的字符串
14
///加密密钥,要求为8位
15
///加密成功返回加密后的字符串,失败返回源串
16
publicstaticstringEncryptDES(stringencryptString,stringencryptKey)
17
{
18
try
19
{
20
byte[]rgbKey=Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
21
byte[]rgbIV=Keys;
22
byte[]inputByteArray=Encoding.UTF8.GetBytes(encryptString);
23
DESCryptoServiceProviderdCSP=newDESCryptoServiceProvider();
24
MemoryStreammStream=newMemoryStream();
25
CryptoStreamcStream=newCryptoStream(mStream,dCSP.CreateEncryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
26
cStream.Write(inputByteArray,0,inputByteArray.Length);
27
cStream.FlushFinalBlock();
28
returnConvert.ToBase64String(mStream.ToArray());
29
}
30
catch
31
{
32
returnencryptString;
33
}
34
}
35
36
///
37
///DES解密字符串
38
///
39
///待解密的字符串
40
///解密密钥,要求为8位,和加密密钥相同
41
///解密成功返回解密后的字符串,失败返源串
42
publicstaticstringDecryptDES(stringdecryptString,stringdecryptKey)
43
{
44
try
45
{
46
byte[]rgbKey=Encoding.UTF8.GetBytes(decryptKey);
47
byte[]rgbIV=Keys;
48
byte[]inputByteArray=Convert.FromBase64String(decryptString);
49
DESCryptoServiceProviderDCSP=newDESCryptoServiceProvider();
50
MemoryStreammStream=newMemoryStream();
51
CryptoStreamcStream=newCryptoStream(mStream,DCSP.CreateDecryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
52
cStream.Write(inputByteArray,0,inputByteArray.Length);
53
cStream.FlushFinalBlock();
54
returnEncoding.UTF8.GetString(mStream.ToArray());
55
}
56
catch
57
{
58
returndecryptString;
59
}
60
}
viewsourceprint?
01
方法六(文件加密):
02
usingSystem.IO;
03
usingSystem.Security.Cryptography;
04
usingSystem.Text;
05
06
...
07
08
//加密文件
09
privatestaticvoidEncryptData(StringinName,StringoutName,byte[]desKey,byte[]desIV)
10
{
11
//Createthefilestreamstohandletheinputandoutputfiles.
12
FileStreamfin=newFileStream(inName,FileMode.Open,FileAccess.Read);
13
FileStreamfout=newFileStream(outName,FileMode.OpenOrCreate,FileAccess.Write);
14
fout.SetLength(0);
15
16
//Createvariablestohelpwithreadandwrite.
17
byte[]bin=newbyte[100];//Thisisintermediatestoragefortheencryption.
18
longrdlen=0;//Thisisthetotalnumberofbyteswritten.
19
longtotlen=fin.Length;//Thisisthetotallengthoftheinputfile.
20
intlen;//Thisisthenumberofbytestobewrittenatatime.
21
22
DESdes=newDESCryptoServiceProvider();
23
CryptoStreamencStream=newCryptoStream(fout,des.CreateEncryptor(desKey,desIV),CryptoStreamMode.Write);
24
25
//Readfromtheinputfile,thenencryptandwritetotheoutputfile.
26
while(rdlen27
{
28
len=fin.Read(bin,0,100);