欢迎光临
我们一直在努力

C#中的常用加密算法与其它语言的兼容性-.NET教程,C#语言

建站超值云服务器,限时71元/月

网上流行的asp版md5.perl版des算法在c#中的简单实现

1:md5

以前在asp时代常用的md5算法好象是从动网流出来的,后来大家都用它,基本上有两种 ,区别在md5.asp的结尾部分

md5 = lcase(wordtohex(a) & wordtohex(b) & wordtohex(c) & wordtohex(d))
 
md5=lcase(wordtohex(b) & wordtohex(c)) 

分别对应32位和16位加密方式

在c#中对应的实现为

/// <summary>
       /// 16位md5加密方法,以前的dvbbs所使用
       /// </summary>
       /// <param name=”strsource”>待加密字串</param>
        /// <returns>加密后的字串</returns>
        public string md5encrypt(string strsource)
        {
            return md5encrypt(strsource, 16);
        }
        /// <summary>
        /// md5加密,和动网上的16/32位md5加密结果相同
        /// </summary>
        /// <param name=”strsource”>待加密字串</param>
        /// <param name=”length”>16或32值之一,其它则采用.net默认md5加密算法</param>
        /// <returns>加密后的字串</returns>
        public string md5encrypt(string strsource, int length)
        {
            byte[] bytes = encoding.ascii.getbytes(strsource);
            byte[] hashvalue = ((system.security.cryptography.hashalgorithm)system.security.cryptography.cryptoconfig.createfromname(“md5”)).computehash(bytes);
            stringbuilder sb = new stringbuilder();
            switch (length)
            {
                case 16:
                    for (int i = 4; i < 12; i++)
                        sb.append(hashvalue[i].tostring(“x2”));
                    break;
                case 32:
                    for (int i = 0; i < 16; i++)
                    {
                        sb.append(hashvalue[i].tostring(“x2”));
                    }
                    break;
                default:
                    for (int i = 0; i < hashvalue.length; i++)
                    {
                        sb.append(hashvalue[i].tostring(“x2”));
                    }
                    break;
            }

同样的其它语言都实现了des加密与.net framework的des基础实现也不一样,比较郁闷的是我刚开始使用.net framework时还真的改写过perl版的des,后面才发现其实有更简单的办法,因为网上流传的perl/c/java版的des算法都是块加密的,设置ciphermode为ecb就好了,郁闷ing.

源代码如下

        public static byte[] deskey = new byte[] {0x82, 0xbc, 0xa1, 0x6a, 0xf5, 0x87, 0x3b, 0xe6, 0x59, 0x6a, 0x32, 0x64, 0x7f, 0x3a, 0x2a, 0xbb, 0x2b, 0x68, 0xe2, 0x5f, 0x06, 0xfb, 0xb8, 0x2d, 0x67, 0xb3, 0x55, 0x19, 0x4e, 0xb8, 0xbf, 0xdd };
        /// <summary>
        /// des加密
        /// </summary>
        /// <param name=”strsource”>待加密字串</param>
        /// <param name=”key”>32位key值</param>
        /// <returns>加密后的字符串</returns>
        public string desencrypt(string strsource) {
            return desencrypt(strsource, deskey);
        }
        public string desencrypt(string strsource,byte[] key)
        {
            symmetricalgorithm sa = rijndael.create();
            sa.key = key;
            sa.mode= ciphermode.ecb;
            sa.padding = paddingmode.zeros;
            memorystream ms = new memorystream();
            cryptostream cs = new cryptostream(ms, sa.createencryptor(), cryptostreammode.write);
            byte[] byt = encoding.unicode.getbytes(strsource);
            cs.write(byt, 0, byt.length);
            cs.flushfinalblock();
            cs.close();
            return convert.tobase64string(ms.toarray());
        }
        /// <summary>
        /// des解密
        /// </summary>
        /// <param name=”strsource”>待解密的字串</param>
        /// <param name=”key”>32位key值</param>
        /// <returns>解密后的字符串</returns>
        public string desdecrypt(string strsource) {
            return desdecrypt(strsource, deskey);
        }
        public string desdecrypt(string strsource,byte[] key)
        {
            symmetricalgorithm sa = rijndael.create();
            sa.key = key;
            sa.mode = ciphermode.ecb;
            sa.padding = paddingmode.zeros;
            icryptotransform ct = sa.createdecryptor();
            byte[] byt = convert.frombase64string(strsource);
            memorystream ms = new memorystream(byt);
            cryptostream cs = new cryptostream(ms, ct, cryptostreammode.read);
            streamreader sr = new streamreader(cs, encoding.unicode);
            return sr.readtoend();
        }

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#中的常用加密算法与其它语言的兼容性-.NET教程,C#语言
分享到: 更多 (0)