c#
-----------------------------------------------
//名稱空間
using system;
using system.security.cryptography;
using system.io;
using system.text;
//方法
//加密方法
public string encrypt(string ptoencrypt, string skey)
{
descryptoserviceprovider des = new descryptoserviceprovider();
//把字符串放到byte數(shù)組中
//原來(lái)使用的utf8編碼,我改成unicode編碼了,不行
byte[] inputbytearray = encoding.default.getbytes(ptoencrypt);
//byte[] inputbytearray=encoding.unicode.getbytes(ptoencrypt);
//建立加密對(duì)象的密鑰和偏移量
//原文使用asciiencoding.ascii方法的getbytes方法
//使得輸入密碼必須輸入英文文本
des.key = asciiencoding.ascii.getbytes(skey);
des.iv = asciiencoding.ascii.getbytes(skey);
memorystream ms = new memorystream();
cryptostream cs = new cryptostream(ms, des.createencryptor(),cryptostreammode.write);
//write the byte array into the crypto stream
//(it will end up in the memory stream)
cs.write(inputbytearray, 0, inputbytearray.length);
cs.flushfinalblock();
//get the data back from the memory stream, and into a string
stringbuilder ret = new stringbuilder();
foreach(byte b in ms.toarray())
{
//format as hex
ret.appendformat("{0:x2}", b);
}
ret.tostring();
return ret.tostring();
}
//解密方法
public string decrypt(string ptodecrypt, string skey)
{
descryptoserviceprovider des = new descryptoserviceprovider();
//put the input string into the byte array
byte[] inputbytearray = new byte[ptodecrypt.length / 2];
for(int x = 0; x < ptodecrypt.length / 2; x++)
{
int i = (convert.toint32(ptodecrypt.substring(x * 2, 2), 16));
inputbytearray[x] = (byte)i;
}
//建立加密對(duì)象的密鑰和偏移量,此值重要,不能修改
des.key = asciiencoding.ascii.getbytes(skey);
des.iv = asciiencoding.ascii.getbytes(skey);
memorystream ms = new memorystream();
cryptostream cs = new cryptostream(ms, des.createdecryptor(),cryptostreammode.write);
//flush the data through the crypto stream into the memory stream
cs.write(inputbytearray, 0, inputbytearray.length);
cs.flushfinalblock();
//get the decrypted data back from the memory stream
//建立stringbuild對(duì)象,createdecrypt使用的是流對(duì)象,必須把解密后的文本變成流對(duì)象
stringbuilder ret = new stringbuilder();
return system.text.encoding.default.getstring(ms.toarray());
}
-------------------------------------------------------
vb.net :
-------------------------------------------------------
imports system.web.security
imports system.security
imports system.security.cryptography
imports system.text
public shared function encrypt(byval ptoencrypt as string, byval skey as string) as string
dim des as new descryptoserviceprovider()
dim inputbytearray() as byte
inputbytearray = encoding.default.getbytes(ptoencrypt)
'建立加密對(duì)象的密鑰和偏移量
'原文使用asciiencoding.ascii方法的getbytes方法
'使得輸入密碼必須輸入英文文本
des.key = asciiencoding.ascii.getbytes(skey)
des.iv = asciiencoding.ascii.getbytes(skey)
'寫二進(jìn)制數(shù)組到加密流
'(把內(nèi)存流中的內(nèi)容全部寫入)
dim ms as new system.io.memorystream()
dim cs as new cryptostream(ms, des.createencryptor, cryptostreammode.write)
'寫二進(jìn)制數(shù)組到加密流
'(把內(nèi)存流中的內(nèi)容全部寫入)
cs.write(inputbytearray, 0, inputbytearray.length)
cs.flushfinalblock()
'建立輸出字符串
dim ret as new stringbuilder()
dim b as byte
for each b in ms.toarray()
ret.appendformat("{0:x2}", b)
next
return ret.tostring()
end function
'解密方法
public shared function decrypt(byval ptodecrypt as string, byval skey as string) as string
dim des as new descryptoserviceprovider()
'把字符串放入byte數(shù)組
dim len as integer
len = ptodecrypt.length / 2 - 1
dim inputbytearray(len) as byte
dim x, i as integer
for x = 0 to len
i = convert.toint32(ptodecrypt.substring(x * 2, 2), 16)
inputbytearray(x) = ctype(i, byte)
next
'建立加密對(duì)象的密鑰和偏移量,此值重要,不能修改
des.key = asciiencoding.ascii.getbytes(skey)
des.iv = asciiencoding.ascii.getbytes(skey)
dim ms as new system.io.memorystream()
dim cs as new cryptostream(ms, des.createdecryptor, cryptostreammode.write)
cs.write(inputbytearray, 0, inputbytearray.length)
cs.flushfinalblock()
return encoding.default.getstring(ms.toarray)
end function
------------------------------------------------
備注:
1. skey輸入密碼的時(shí)候,必須使用英文字符,區(qū)分大小寫,且字符數(shù)量是8個(gè),不能多也不能少,否則出錯(cuò)。
2. 本人asp.net1.1,vs.net2003,windows2003 server環(huán)境下c#和vb.net分別調(diào)試成功!