国产探花免费观看_亚洲丰满少妇自慰呻吟_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();
  }



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郓城县| 旬阳县| 区。| 涡阳县| 新河县| 高安市| 东乡| 恩施市| 隆化县| 区。| 社旗县| 两当县| 衡水市| 通化县| 华蓥市| 玛多县| 顺义区| 彭山县| 监利县| 华亭县| 博罗县| 青冈县| 师宗县| 鄂托克旗| 安多县| 东至县| 永福县| 疏勒县| 青河县| 梨树县| 图木舒克市| 禹州市| 宝丰县| 兴化市| 临安市| 都江堰市| 定州市| 邵阳县| 绿春县| 台前县| 陆良县|