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

CryptoJS与C#AES加解密互转教程

程序员文章站 2022-03-28 19:12:41
cryptojs与c#aes加解密互转 页面js引用: js端aes解密: /*********************************************************...

    cryptojs与c#aes加解密互转

    页面js引用:

    js端aes解密:

    /**************************************************************
        *字符串加密
        *   str:需要加密的字符串
        ****************************************************************/
        function encrypt(str) {
            var key = "12345678900000001234567890000000";//32位
            var iv = "1234567890000000";//16位
            var key = cryptojs.enc.utf8.parse(key);
            var iv = cryptojs.enc.utf8.parse(iv);
    
            var encrypted = '';
    
            var srcs = cryptojs.enc.utf8.parse(str);
            encrypted = cryptojs.aes.encrypt(srcs, key, {
                iv: iv,
                mode: cryptojs.mode.cbc,
                padding: cryptojs.pad.pkcs7
            });
    
            return encrypted.ciphertext.tostring();
        }
        /**************************************************************
        *字符串解密
        *   str:需要解密的字符串
        ****************************************************************/
        function decrypt(str) {
            var key = "12345678900000001234567890000000";//32位
            var iv = "1234567890000000";//16位
            var key = cryptojs.enc.utf8.parse(key);
            var iv = cryptojs.enc.utf8.parse(iv);
            var encryptedhexstr = cryptojs.enc.hex.parse(str);
            var srcs = cryptojs.enc.base64.stringify(encryptedhexstr);
            var decrypt = cryptojs.aes.decrypt(srcs, key, {
                iv: iv,
                mode: cryptojs.mode.cbc,
                padding: cryptojs.pad.pkcs7
            });
            var decryptedstr = decrypt.tostring(cryptojs.enc.utf8);
            return decryptedstr.tostring();
        }

    c# aes加密解密:

    const string aes_iv = "1234567890000000";//16位    
    
        ///   
        /// aes加密算法  
        ///   
        ///明文字符串  
        ///密钥(32位)  
        /// 字符串  
        public static string encryptbyaes(string input, string key)
        {
            byte[] keybytes = encoding.utf8.getbytes(key.substring(0, 32));
            using (aescryptoserviceprovider aesalg = new aescryptoserviceprovider())
            {
                aesalg.key = keybytes;
                aesalg.iv = encoding.utf8.getbytes(aes_iv.substring(0, 16));
    
                icryptotransform encryptor = aesalg.createencryptor(aesalg.key, aesalg.iv);
                using (memorystream msencrypt = new memorystream())
                {
                    using (cryptostream csencrypt = new cryptostream(msencrypt, encryptor, cryptostreammode.write))
                    {
                        using (streamwriter swencrypt = new streamwriter(csencrypt))
                        {
                            swencrypt.write(input);
                        }
                        byte[] bytes = msencrypt.toarray();
                        return bytearraytohexstring(bytes);
                    }
                }
            }
        }
    
        ///   
        /// aes解密  
        ///   
        ///密文字节数组  
        ///密钥(32位)  
        /// 返回解密后的字符串  
        public static string decryptbyaes(string input, string key)
        {
            byte[] inputbytes = hexstringtobytearray(input); 
            byte[] keybytes = encoding.utf8.getbytes(key.substring(0, 32));
            using (aescryptoserviceprovider aesalg = new aescryptoserviceprovider())
            {
                aesalg.key = keybytes;
                aesalg.iv = encoding.utf8.getbytes(aes_iv.substring(0, 16));
    
                icryptotransform decryptor = aesalg.createdecryptor(aesalg.key, aesalg.iv);
                using (memorystream msencrypt = new memorystream(inputbytes))
                {
                    using (cryptostream csencrypt = new cryptostream(msencrypt, decryptor, cryptostreammode.read))
                    {
                        using (streamreader srencrypt = new streamreader(csencrypt))
                        {
                            return srencrypt.readtoend();
                        }
                    }
                }
            }
        }
    
        /// 
        /// 将指定的16进制字符串转换为byte数组
        /// 
        ///16进制字符串(如:“7f 2c 4a”或“7f2c4a”都可以)
        /// 16进制字符串对应的byte数组
        public static byte[] hexstringtobytearray(string s)
        {
            s = s.replace(" ", "");
            byte[] buffer = new byte[s.length / 2];
            for (int i = 0; i < s.length; i += 2)
                buffer[i / 2] = (byte)convert.tobyte(s.substring(i, 2), 16);
            return buffer;
        }
    
        /// 
        /// 将一个byte数组转换成一个格式化的16进制字符串
        /// 
        ///byte数组
        /// 格式化的16进制字符串
        public static string bytearraytohexstring(byte[] data)
        {
            stringbuilder sb = new stringbuilder(data.length * 3);
            foreach (byte b in data)
            {
                //16进制数字
                sb.append(convert.tostring(b, 16).padleft(2, '0'));
                //16进制数字之间以空格隔开
                //sb.append(convert.tostring(b, 16).padleft(2, '0').padright(3, ' '));
            }
            return sb.tostring().toupper();
        }

    用法:

    encrypt("2017-05")//结果:68f4a7903a9fe6085d2301ac68cc039c
    decrypt("68f4a7903a9fe6085d2301ac68cc039c")//结果:2017-05
    
    //加密
    string str1 = encrypt.encryptbyaes("2017-05", "12345678900000001234567890000000");
    //解密
    string str2 = encrypt.decryptbyaes("68f4a7903a9fe6085d2301ac68cc039c", "12345678900000001234567890000000");