当前位置:   article > 正文

Aes加/解密工具类 --Java_aes密钥生成工具

aes密钥生成工具


一、加/解密流程图

在这里插入图片描述

二、生成密钥

代码如下(示例):

	/**
     * 生成加密密钥
     * @param seed 密钥种子+salt盐值
     * @param keySize  key size
     * @return 密钥
     * @throws Exception 异常
     */
    public static String generateKey(String seed, int keySize) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom;
        if (seed != null && !"".equals(seed)) {
            secureRandom = new SecureRandom(seed.getBytes());
        } else {
            secureRandom = new SecureRandom();
        }

        keyGenerator.init(keySize, secureRandom);
        SecretKey secretKey = keyGenerator.generateKey();
        return Base64.encodeBase64String(secretKey.getEncoded());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

三、字符串加密解密

1.字符串加密

代码如下(示例):

	/**
     * 字符串加密
     *
     * @param data 需要加密的字符串
     * @param key 密钥
     * @return 结果
     * @throws Exception 异常
     */
    public static String encrypt(String data, String key) throws Exception {
        Key k = toKey(Base64.decodeBase64(key));
        byte[] raw = k.getEncoded();
        SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(1, secretKeySpec);
        byte[] encypted = cipher.doFinal(data.getBytes());
        return Base64.encodeBase64String(encypted);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2.字符串解密

代码如下(示例):

	/**
     * 字符串解密
     *
     * @param data 已加密的字符串
     * @param key 密钥
     * @return 结果
     * @throws Exception 异常
     */
    public static String decrypt(String data, String key) throws Exception {
        Key k = toKey(Base64.decodeBase64(key));
        byte[] raw = k.getEncoded();
        SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(2, secretKeySpec);
        byte[] encryptedBytes = Base64.decodeBase64(data);
        byte[] originalBytes = cipher.doFinal(encryptedBytes);
        return new String(originalBytes);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

四、文件加密解密

1.文件加密

代码如下(示例):

	/**
     * 文件加密
     * @param key 密钥
     * @param sourceFile 源文件
     * @param targetFile 目标文件
     * @return 结果
     * @throws Exception 异常
     */
    public static int encryptFile(String key, File sourceFile, File targetFile) throws Exception {
        if (sourceFile.exists() && sourceFile.isFile()) {
            if (!targetFile.getParentFile().exists()) {
                targetFile.getParentFile().mkdirs();
            }

            targetFile.createNewFile();
            InputStream in = new FileInputStream(sourceFile);
            OutputStream out = new FileOutputStream(targetFile);
            Key k = toKey(Base64.decodeBase64(key));
            byte[] raw = k.getEncoded();
            SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(1, secretKeySpec);
            CipherInputStream cin = new CipherInputStream(in, cipher);
            byte[] cache = new byte[1024];
            boolean var11 = false;

            int nRead;
            while ((nRead = cin.read(cache)) != -1) {
                out.write(cache, 0, nRead);
                out.flush();
            }

            out.close();
            cin.close();
            in.close();
            log.debug("Encrypted successfully!!");
            return 1;
        } else {
            log.debug("Encrypted Failed," + sourceFile.getAbsolutePath() + "文件不存在!!");
            return 1;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

2.文件解密

代码如下(示例):

	/**
     * 文件解密
     * @param key 密钥
     * @param sourceFile 源文件
     * @param targetFile 目标文件
     * @return 结果
     * @throws Exception 异常
     */
    public static int decryptFile(String key, File sourceFile, File targetFile) throws Exception {
        if (sourceFile.exists() && sourceFile.isFile()) {
            if (!targetFile.getParentFile().exists()) {
                targetFile.getParentFile().mkdirs();
            }

            targetFile.createNewFile();
            FileInputStream in = new FileInputStream(sourceFile);
            FileOutputStream out = new FileOutputStream(targetFile);
            Key k = toKey(Base64.decodeBase64(key));
            byte[] raw = k.getEncoded();
            SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(2, secretKeySpec);
            CipherOutputStream cout = new CipherOutputStream(out, cipher);
            byte[] cache = new byte[1024];
            boolean var11 = false;

            int nRead;
            while ((nRead = in.read(cache)) != -1) {
                cout.write(cache, 0, nRead);
                cout.flush();
            }

            cout.close();
            out.close();
            in.close();
            log.debug("Decrypted successfully!!");
            return 0;
        } else {
            log.debug("Decrypted Failed," + sourceFile.getAbsolutePath() + "文件不存在!!");
            return 1;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

五、测试

代码如下(示例):

 public static void main(String[] args) {
        //密钥种子
        String seed = "123456";
        //salt盐值
        String salt = "tomy";
        //key size
        int keySize = 128;
        //secret key
        String key = "";
        try {
           //生成secret key
            key = GenerateKeyUtil.generateKey(seed + salt, keySize);
            System.out.println(key);
            //执行字符串加密
            String encrypt = AESUtil.encrypt("7895455", key);
            System.out.println(encrypt);
            //执行字符串解密
            String decrypt = AESUtil.decrypt(encrypt, key);
            System.out.println(decrypt);
            File sourceFile = new File("C:\\Users\\Desktop\\1.png");
            File targetFile = new File("C:\\Users\\Desktop\\2.png");
            File targetFile1 = new File("C:\\Users\\Desktop\\3.png");
            //执行文件加密
            encryptFile(key, sourceFile, targetFile);
            //执行文件解密
            decryptFile(key, targetFile, targetFile1);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/511704
推荐阅读
相关标签
  

闽ICP备14008679号