AES加密在Linux操作系统上每次加密之后的值会不同

一 概述

本人在近期开发过程中发现一个有趣的现象,通过AES加密解密的方法,在Windows操作系统上执行都是唯一,但是转到Linux操作系统上就出现每次加密都会得到一个不一样的加密值。

二 本人AES加密代码

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;

/**
 * @Author zhoucheng
 * @ClassName AESUtils
 * @Date 17:59 2021/4/8
 * @Version 1.0
 * @Description 用于加密,解密
 **/
public class AESUtils {

    //加密密钥
    public static final String secret = "zhoucheng";

    /**
     * @Author zhoucheng
     * @MethodName AESJDKEncode
     * @Param [message, Key]
     * @Date 18:22 2021/4/8
     * @return: byte[]
     * @Version 1.0
     * @Description AES加密
     **/
    public static byte[] AESJDKEncode(String message, String Key) {

        try {
            KeyGenerator keyGeneratorEncode = KeyGenerator.getInstance("AES");

            keyGeneratorEncode.init(256, new SecureRandom(Key.getBytes(StandardCharsets.UTF_8)));
            SecretKey secretKey = keyGeneratorEncode.generateKey();
            byte[] encode = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(encode, "AES");

            //创建密码器
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Author zhoucheng
     * @MethodName AESJDKDecode
     * @Param [message, Key]
     * @Date 18:22 2021/4/8
     * @return: byte[]
     * @Version 1.0
     * @Description AES解密
     **/
    public static byte[] AESJDKDecode(byte[] message, String Key) {

        try {
            KeyGenerator keyGeneratorDecode = KeyGenerator.getInstance("AES");

            keyGeneratorDecode.init(256, new SecureRandom(Key.getBytes(StandardCharsets.UTF_8)));
            SecretKey secretKey = keyGeneratorDecode.generateKey();
            byte[] decode = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(decode, "AES");
            //创建密码器
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] result = cipher.doFinal(message);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @Author zhoucheng
     * @MethodName convertByteToHexString
     * @Param [bytes]
     * @Date 19:11 2021/4/8
     * @return: java.lang.String
     * @Version 1.0
     * @Description 将byte数组转化为16进制字符串
     **/
    public static String convertByteToHexString(byte[] bytes) {

        String result = "";

        for (int i = 0; i < bytes.length; i++) {
            int temp = bytes[i] & 0xff;
            String tempHex = Integer.toHexString(temp);
            if (tempHex.length() < 2) {
                result += "0" + tempHex;
            }else {
                result += tempHex;
            }
        }
        return result;
    }
}

三 分析原因

SecureRandom 实现完全隨操作系统本身的内部状态,除非调用方在调用 getInstance 方法之后又调用了 setSeed 方法;该实现在 windows 上每次生成的 key 都相同,但是在 solaris 或部分 linux 系统上则不同。

暂时分析理解至此,后续继续研究,待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值