java和c#的AES256加密解密方法

java

CyptoClient.java中代码

package ***.security.crypto;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.zhengtoon.bjtoon.uia.util.HexUtil;

import org.apache.commons.codec.binary.Base64;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class CyptoClient
{

  private static final Logger logger = LogManager.getLogger(CyptoClient.class);

  public static final String CHARSET = "UTF-8";
 

  public static String encryptWithAES(String info, String aeskey) {
        try {
            byte[] key = HexUtil.hexStr2ByteArray(aeskey);
            byte[] bytes = info.getBytes();
            byte[] encrypt = AES256Coder.encrypt(bytes, key);
            return HexUtil.byteArray2HexStr(encrypt);
        } catch (Exception e) {
            logger.error("encrypt执行失败: " + e.getMessage(), e);
        }
        return null;
    }

    public static String decryptWithAES(String info, String aeskey) {
        try {
            byte[] key = HexUtil.hexStr2ByteArray(aeskey);
            byte[] bytes = HexUtil.hexStr2ByteArray(info);
            byte[] decrypt = AES256Coder.decrypt(bytes, key);
            return new String(decrypt);
        } catch (Exception e) {
            logger.error("decrypt执行失败: " + e.getMessage(), e);
        }
        return null;
    }

}

HexUtil .java中代码

package ***.security.crypto;

import org.springframework.util.StringUtils;

public class HexUtil {

    /**
     * 把hex转数组
     * @param hexString
     * @return
     */
    public static byte[] hexStr2ByteArray(String hexString) {
        if (StringUtils.isEmpty(hexString))
            throw new IllegalArgumentException("this hexString must not be empty");

        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {
            //因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
            //将hex 转换成byte   "&" 操作为了防止负数的自动扩展
            // hex转换成byte 其实只占用了4位,然后把高位进行右移四位
            // 然后“|”操作  低四位 就能得到 两个 16进制数转换成一个byte.
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

    public static String byteArray2HexStr(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1) {
            throw new IllegalArgumentException("this byteArray must not be null or empty");
        }
        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if ((byteArray[i] & 0xff) < 0x10)// 0~F前面不零
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();

    }

}

AES256Coder.java中代码

package ***.security.crypto;

import com.zhengtoon.bjtoon.uia.support.converter.MessageConverter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;

/**
 * ${DESCRIPTION}
 */
public class AES256Coder {
    private static final Logger logger = LogManager.getLogger(MessageConverter.class);
    public static final String KEY_ALGORITHM = "AES";
    public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    public static final Integer KEY_LENGTH = 256;
    public static byte[] initkey() throws Exception {
        Security.addProvider(new BouncyCastleProvider());
        KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM, "BC");
        SecretKey secretKey = kg.generateKey();
        return secretKey.getEncoded();
    }
    public static Key toKey(byte[] key) throws Exception {
        SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
        return secretKey;
    }
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        Key k = toKey(key);
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
        cipher.init(Cipher.ENCRYPT_MODE, k);
        return cipher.doFinal(data);
    }
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        Key k = toKey(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, k);
        return cipher.doFinal(data);
    }
    
}

上面的是java的AES256加密解密方法,引用了很多包,我需要用c#接收加密数据并解析,同时我这边也要对数据加密传回去,所以我需要对上面的方法进行修改为c#的方法,下面贴c#的方法,代码都简单放在一个文件中了,不和java那样封装多次

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;

namespace YMS.Command.SecurityHelper
{
   public class AES256Provider
    {
        private AES256Provider()
        {
        }
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="encryptStr">明文</param>
        /// <param name="key">密钥</param>

        /// 16位密钥对应128位编码,24位密钥对应192位,32对应256位编码
        /// <returns></returns>
        public static string Encrypt(string encryptStr, string key)

        {
            try {
                byte[] keyArray = hexStr2ByteArray(key);
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);
                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 byteArray2HexStr(resultArray);
            }
            catch (Exception ex)
            {

            }
            return null;
        }
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="decryptStr">密文</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string Decrypt(string decryptStr, string key)

        {
            try
            {
                byte[] keyArray = hexStr2ByteArray(key);
                byte[] toEncryptArray = hexStr2ByteArray(decryptStr);
                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);
            }
            catch (Exception ex)
            {

            }
            return null;
        }
        public static byte[] hexStr2ByteArray(String hexString)
        {
            try
            { 
                if (string.IsNullOrEmpty(hexString))
                    throw new Exception("this hexString must not be empty");

                hexString = hexString.ToLower();
                byte[] byteArray = new byte[hexString.Length / 2];
                int k = 0;
                for (int i = 0; i < byteArray.Length; i++)
                {

                    //把hexString中的两个16进制数转化为字节,一个16进制数是4位,两个就是8位即一个字节
                    //Substring(k, 2)的作用是从k开始截取2个数据
                    byteArray[i]= Convert.ToByte(hexString.Substring(k, 2), 16);
                    k += 2;
                }
                return byteArray;
            }
            catch (Exception ex)
            {

            }
            return null;
        }
        public static String byteArray2HexStr(byte[] byteArray)
        {
            try
            {
                StringBuilder hexString = new StringBuilder();
                foreach (byte b in byteArray)
                {

                    //X2表示十六进制格式(大写),域宽2位,不足的左边填0。
                    hexString.AppendFormat("{0:x2}", b);
                }
                return hexString.ToString().ToLower();
            }
            catch (Exception ex)
            {

            }
            return null;
        }
    }
}

java代码是甲方给的工具类,c#是我自己进行修改的,不当之处望指正

猜你喜欢

转载自blog.csdn.net/qq_33380252/article/details/83505420