sTemp=sTemp.PadRight(IVLength,'');
returnASCIIEncoding.ASCII.GetBytes(sTemp);
}
///
///加密方法
///
///待加密的串
///经过加密的串
publicstringEncrypto(stringSource)
{
byte[]bytIn=UTF8Encoding.UTF8.GetBytes(Source);
MemoryStreamms=newMemoryStream();
mobjCryptoService.Key=GetLegalKey();
mobjCryptoService.IV=GetLegalIV();
ICryptoTransformencrypto=mobjCryptoService.CreateEncryptor();
CryptoStreamcs=newCryptoStream(ms,encrypto,CryptoStreamMode.Write);
cs.Write(bytIn,0,bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[]bytOut=ms.ToArray();
returnConvert.ToBase64String(bytOut);
}
///
///解密方法
///
///待解密的串
///经过解密的串
publicstringDecrypto(stringSource)
{
byte[]bytIn=Convert.FromBase64String(Source);
MemoryStreamms=newMemoryStream(bytIn,0,bytIn.Length);
mobjCryptoService.Key=GetLegalKey();
mobjCryptoService.IV=GetLegalIV();
ICryptoTransformencrypto=mobjCryptoService.CreateDecryptor();
CryptoStreamcs=newCryptoStream(ms,encrypto,CryptoStreamMode.Read);
StreamReadersr=newStreamReader(cs);
returnsr.ReadToEnd();
}
方法五:
usingSystem.IO;
usingSystem.Security.Cryptography;
usingSystem.Text;
...
//默认密钥向量
privatestaticbyte[]Keys={0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF};
///
///DES加密字符串
///
///待加密的字符串
///加密密钥,要求为8位
///加密成功返回加密后的字符串,失败返回源串
publicstaticstringEncryptDES(stringencryptString,stringencryptKey)
{
try
{
byte[]rgbKey=Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
byte[]rgbIV=Keys;
byte[]inputByteArray=Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProviderdCSP=newDESCryptoServiceProvider();
MemoryStreammStream=newMemoryStream();
CryptoStreamcStream=newCryptoStream(mStream,dCSP.CreateEncryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
cStream.Write(inputByteArray,0,inputByteArray.Length);
cStream.FlushFinalBlock();
returnConvert.ToBase64String(mStream.ToArray());
}
catch
{
returnencryptString;
}
}
///
///DES解密字符串
///
///待解密的字符串
///解密密钥,要求为8位,和加密密钥相同
///解密成功返回解密后的字符串,失败返源串
publicstaticstringDecryptDES(stringdecryptString,stringdecryptKey)
{
try
{
byte[]rgbKey=Encoding.UTF8.GetBytes(decryptKey);
byte[]rgbIV=Keys;
byte[]inputByteArray=Convert.FromBase64String(decryptString);
DESCryptoServiceProviderDCSP=newDESCryptoServiceProvider();
MemoryStreammStream=newMemoryStream();
CryptoStreamcStream=newCryptoStream(mStream,DCSP.CreateDecryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
cStream.Write(inputByteArray,0,inputByteArray.Length);
cStream.FlushFinalBlock();
returnEncoding.UTF8.GetString(mStream.ToArray());
}
catch
{
returndecryptString;
}
}
方法六(文件加密):
usingSystem.IO;
usingSystem.Security.Cryptography;
usingSystem.Text;
...
//加密文件
privatestaticvoidEncryptData(StringinName,StringoutName,byte[]desKey,byte[]desIV)
{
//Createthefilestreamstohandletheinputandoutputfiles.
FileStreamfin=newFileStream(inName,FileMode.Open,FileAccess.Read);
FileStreamfout=newFileStream(outName,FileMode.OpenOrCreate,FileAccess.Write);
fout.SetLength(0);
//Createvariablestohelpwithreadandwrite.
byte[]bin=newbyte[100];//Thisisintermediatestoragefortheencryption.
longrdlen=0;//Thisisthetotalnumberofbyteswritten.
longtotlen=fin.Length;//Thisisthetotallengthoftheinputfile.
intlen;//Thisisthenumberofbytestobewrittenatatime.
DESdes=newDESCryptoServiceProvider();
CryptoStreamencStream=newCryptoStream(fout,des.CreateEncryptor(desKey,desIV),CryptoStreamMode.Write);
//Readfromtheinputfile,thenencryptandwritetotheoutputfile.
while(rdlen{
len=fin.Read(bin,0,100);
encStream.Write(bin,0,len);
rdlen=rdlen+len;
}
encStream.Close();
fout.Close();
fin.Close();
}
//解密文件
privatestaticvoidDecryptData(StringinName,StringoutName,byte[]desKey,byte[]desIV)
{
//Createthefilestreamstohandletheinputandoutputfiles.
FileStreamfin=newFileStream(inName,FileMode.Open,FileAccess.Read);
FileStreamfout=newFileStream(outName,FileMode.OpenOrCreate,FileAccess.Write);
fout.SetLength(0);
//Createvariablestohelpwithreadandwrite.
byte[]bin=newbyte[100];//Thisisintermediatestoragefortheencryption.
longrdlen=0;//Thisisthetotalnumberofbyteswritten.
longtotlen=fin.Length;//Thisisthetotallengthoftheinputfile.
intlen;//Thisisthenumberofbytestobewrittenatatime.
DESdes=newDESCryptoServiceProvider();
CryptoStreamencStream=newCryptoStream(fout,des.CreateDecryptor(desKey,desIV),CryptoStreamMode.Write);
//Readfromtheinputfile,thenencryptandwritetotheoutputfile.
while(rdlen{
len=fin.Read(bin,0,100);
encStream.Write(bin,0,len);
rdlen=rdlen+len;
}
encStream.Close();
fout.Close();
fin.Close();
}
usingSystem;
usingSystem.Security.Cryptography;//这个是处理文字编码的前提
usingSystem.Text;
usingSystem.IO;
///
///DES加密方法
///
///明文
///密钥
///向量
///密文
publicstringDESEncrypt(stringstrPlain,stringstrDESKey,stringstrDESIV)
{
//把密钥转换成字节数组
byte[]bytesDESKey=ASCIIEncoding.ASCII.GetBytes(strDESKey