欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net TripleDES加密、解密算法

程序员文章站 2022-06-06 21:07:52
using system;    using system.collections.generic;  ...
using system;   

using system.collections.generic;   

using system.linq;   

using system.text;   

using system.security.cryptography;   

using system.io;   

namespace windowsformsapplication1   

{  

    #region tripledes算法   

  

    public class classtripledes   

    {   

        public classtripledes()   

        {   

        }   

  

        //加密,使用密码产生加密算法的公钥,并使用tripledes对密码进行加密。   

        public static string encrypt(string pass)   

        {   

            try  

            {   

                byte[] bt = (new system.text.unicodeencoding()).getbytes(pass);   

                passwordderivebytes pdb = new passwordderivebytes(pass, null);   

                byte[] key = pdb.getbytes(24);   

                byte[] iv = pdb.getbytes(8);   

                memorystream ms = new memorystream();   

                tripledescryptoserviceprovider tdesc = new tripledescryptoserviceprovider();   

                cryptostream cs = new cryptostream(ms, tdesc.createencryptor(key, iv), cryptostreammode.write);   

                cs.write(bt, 0, bt.length);   

                cs.flushfinalblock();   

                return convert.tobase64string(ms.toarray());   

            }   

            catch (exception ex)   

            {   

                throw ex;   

            }   

        }   

  

        //解密,使用密码产生加密算法的公钥,并使用tripledes对加密数据进行解密。   

        public static string decrypt(string str, string pass)   

        {   

            try  

            {   

                byte[] bt = convert.frombase64string(str);   

                passwordderivebytes pdb = new passwordderivebytes(pass, null);   

                byte[] key = pdb.getbytes(24);   

                byte[] iv = pdb.getbytes(8);   

                memorystream ms = new memorystream();   

                tripledescryptoserviceprovider tdesc = new tripledescryptoserviceprovider();   

                cryptostream cs = new cryptostream(ms, tdesc.createdecryptor(key, iv), cryptostreammode.write);   

                cs.write(bt, 0, bt.length);   

                cs.flushfinalblock();   

                return (new system.text.unicodeencoding()).getstring(ms.toarray());   

            }   

            catch (exception ex)   

            {   

                throw ex;   

            }   

        }   

        //使用:   

        //string str = encrypt("bbb");   

        //console.writeline(decrypt(str, "bbb"));   

  

        //加密,使用密码产生加密算法的公钥,并使用tripledes对密码进行加密。   

        public static string encryptwithkey(string pass, string p_key)   

        {   

            try  

            {   

                byte[] bt = (new system.text.unicodeencoding()).getbytes(pass);   

                passwordderivebytes pdb = new passwordderivebytes(p_key, null);   

                byte[] key = pdb.getbytes(24);   

                byte[] iv = pdb.getbytes(8);   

                memorystream ms = new memorystream();   

                tripledescryptoserviceprovider tdesc = new tripledescryptoserviceprovider();   

                cryptostream cs = new cryptostream(ms, tdesc.createencryptor(key, iv), cryptostreammode.write);   

                cs.write(bt, 0, bt.length);   

                cs.flushfinalblock();   

                return convert.tobase64string(ms.toarray());   

            }   

            catch (exception ex)   

            {   

                throw ex;   

            }   

        }   

  

        //解密,使用密码产生加密算法的公钥,并使用tripledes对加密数据进行解密。   

        public static string decryptwithkey(string str, string p_key)   

        {   

            try  

            {   

                byte[] bt = convert.frombase64string(str);   

                passwordderivebytes pdb = new passwordderivebytes(p_key, null);   

                byte[] key = pdb.getbytes(24);   

                byte[] iv = pdb.getbytes(8);   

                memorystream ms = new memorystream();   

                tripledescryptoserviceprovider tdesc = new tripledescryptoserviceprovider();   

                cryptostream cs = new cryptostream(ms, tdesc.createdecryptor(key, iv), cryptostreammode.write);   

                cs.write(bt, 0, bt.length);   

                cs.flushfinalblock();   

                return (new system.text.unicodeencoding()).getstring(ms.toarray());   

            }   

            catch (exception ex)   

            {   

                throw ex;   

            }   

        }   

  

    }  

 

    #endregion   

}