国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

使用DES加密解密代碼(C# & vb.Net),已經(jīng)調(diào)試成功,支持中文加解密,公布!

2024-07-10 13:04:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
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)試成功!
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 辉南县| 嘉定区| 呼伦贝尔市| 襄垣县| 阿拉善盟| 定远县| 龙江县| 大洼县| 赣榆县| 出国| 新邵县| 舟曲县| 永靖县| 阜康市| 江源县| 明水县| 克山县| 汾西县| 夏邑县| 岑溪市| 旌德县| 海林市| 贡觉县| 昭平县| 澄城县| 巴东县| 明水县| 江北区| 道孚县| 克东县| 平潭县| 通道| 鄂尔多斯市| 康马县| 黄山市| 德安县| 凉城县| 西藏| 霸州市| 平和县| 衡阳县|