美文网首页
java与C#的AES加密、解密

java与C#的AES加密、解密

作者: 蚁丶点 | 来源:发表于2019-03-06 19:43 被阅读0次

    java代码:

      package my;
     
    import javax.crypto.*;
    import javax.crypto.spec.SecretKeySpec;
    import sun.misc.*;
     
    public class Xianchengmingming {   
        public static void main(String[] args) throws Exception {   
            String key = "ABCDEFGHIJKLMNRS";   
            String value="hsb:123:1489048530930:60001";   
            System.out.println("加密:"+aesEncrypt(value,key)); 
            System.out.println("解密:"+aesDecrypt(aesEncrypt(value,key),key));      
        }   
        /**
         * AES的加密函数
         * @param str 传入需要加密的字符
         * @param key 传入一个16位长度的密钥。否则报错
         * @return 执行成功返回加密结果,否则报错
         * @throws Exception 抛出一个加密异常
         */
        public static String aesEncrypt(String str, String key) throws Exception {
            if (str == null || key == null) return null;
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
            byte[] bytes = cipher.doFinal(str.getBytes("utf-8"));
            return new BASE64Encoder().encode(bytes);
        }
        /**
         * AES的解密函数
         * @param str 传入需要解密的字符
         * @param key 传入一个16位长度的密钥。否则报错
         * @return 执行成功返回加密结果,否则报错
         * @throws Exception 抛出一个解密异常
         */
        public static String aesDecrypt(String str, String key) throws Exception {
            if (str == null || key == null) return null;
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes("utf-8"), "AES"));
            byte[] bytes = new BASE64Decoder().decodeBuffer(str);
            bytes = cipher.doFinal(bytes);
            return new String(bytes, "utf-8");
        } 
    } 
    

    C#代码:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO; 
     
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                String key = "ABCDEFGHIJKLMNRS";
                Console.WriteLine("解密" + Decrypt("/4+UJG55uVHN6rninZ6Z8nUe2OJHGGAYqcdmdDeQhiM=", key));
                Console.ReadLine();
            }
     
            /// <summary>
            /// 有密码的AES加密 
            /// </summary>
            /// <param name="text">加密字符</param>
            /// <param name="password">加密的密码</param>
            /// <param name="iv">密钥</param>
            /// <returns></returns>
            public static string Encrypt(string toEncrypt,string key)
            {
     
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
     
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;
     
                ICryptoTransform cTransform = rDel.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
     
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
            }
     
            /// <summary>
            /// AES解密
            /// </summary>
            /// <param name="text"></param>
            /// <param name="password"></param>
            /// <param name="iv"></param>
            /// <returns></returns>
            public static string Decrypt(string toDecrypt,string key)
            {
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
     
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;
     
                ICryptoTransform cTransform = rDel.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
     
                return UTF8Encoding.UTF8.GetString(resultArray);
            }
     
        }
     
    }
    

    相关文章

      网友评论

          本文标题:java与C#的AES加密、解密

          本文链接:https://www.haomeiwen.com/subject/djrcqqtx.html