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

首頁 > 開發 > 綜合 > 正文

字符加密&文件加密的代碼

2024-07-21 02:24:34
字體:
來源:轉載
供稿:網友
 

///<summary>文件加密類 使用des加密文件流</summary>
///<param>deskey: des的密鑰;desiv: des向量</param>

class encrypfile{
        public byte[] deskey;
        public byte[] desiv;

        public encrypfile(byte[] inputkey,byte[] inputiv){
            deskey=inputkey;
            desiv=inputiv;

        }

        ///<summary>加密主方法</summary>
        ///<param>inname:被加密文件名;outname: 加密后文件名</param>
        public void begintoencry(string inname,string outname){
            filestream fin = new filestream(inname, filemode.open, fileaccess.read);
            filestream fout = new filestream(outname, filemode.openorcreate, fileaccess.write);
            fout.setlength(0);

            byte[] bin = new byte[100]; //this is intermediate storage for the encryption.
            long rdlen = 0;              //this is the total number of bytes written.
            long totlen = fin.length;    //this is the total length of the input file.
            int len;                     //this is the number of bytes to be written at a time.
            des des = new descryptoserviceprovider();
            cryptostream encstream = new cryptostream(fout, des.createencryptor(deskey, desiv), cryptostreammode.write);

            while(rdlen < totlen)
            {
                len = fin.read(bin, 0, 100);
                encstream.write(bin, 0, len);
                rdlen = rdlen + len;
            }

                encstream.close();
                fout.close();
                fin.close();
            }
    }


加密字符流
  //ptoencrypt為需要加密字符串,skey為密鑰
  public string encrypt(string ptoencrypt, string skey)
  {
   descryptoserviceprovider des = new descryptoserviceprovider();
   //把字符串放到byte數組中
   byte[] inputbytearray = encoding.default.getbytes(ptoencrypt);

   //建立加密對象的密鑰和向量
   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);

   cs.write(inputbytearray, 0, inputbytearray.length);
   cs.flushfinalblock();
   stringbuilder ret = new stringbuilder();
   foreach(byte b in ms.toarray())
   {
    ret.appendformat("{0:x2}", b);
   }
   return ret.tostring();
  }



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平乐县| 普陀区| 自贡市| 吉林省| 桦川县| 福建省| 郓城县| 松阳县| 崇州市| 微山县| 扎囊县| 新竹县| 遂溪县| 静海县| 郧西县| 衡阳市| 尼玛县| 乐昌市| 云梦县| 苏州市| 永寿县| 金平| 巴南区| 长宁区| 秦皇岛市| 宝鸡市| 宜良县| 和静县| 福建省| 新余市| 黎川县| 社旗县| 京山县| 建始县| 宁武县| 安多县| 昭觉县| 邢台市| 河北省| 博乐市| 微博|