加密解密AES(证件号、手机号)

本文详细介绍了AES(AdvancedEncryptionStandard)加密算法,包括其原理、特点、应用场景,以及Java示例中如何使用AES进行加密和解密操作,展示了AES在网络安全、数据存储和文件加密中的重要性。
摘要由CSDN通过智能技术生成

目录

AES

特点:

算法原理:

应用场景:

算法举例

第一种:指定的规则

第二种:随机生成的规则


AES

AES(Advanced Encryption Standard)是一种对称加密算法,用于保护敏感数据的机密性。它是目前最常用的加密算法之一,被广泛应用于各种领域,包括网络通信、数据存储和传输等。

特点:

  1. 安全性高:AES采用了高级的加密技术,具有较高的安全性,被广泛认可为安全可靠的加密算法。
  2. 高效性:AES算法的加密和解密速度快,适用于大规模数据的加密和解密操作。
  3. 灵活性:AES算法支持多种密钥长度,包括128位、192位和256位,可以根据需求选择合适的密钥长度。

算法原理:


AES算法基于分组密码的思想,将明文数据分成固定长度的数据块(128位),然后对每个数据块进行加密和解密操作。AES算法使用了一系列的轮函数,包括字节替代、行移位、列混淆和轮密钥加等步骤,通过多轮迭代来完成加密和解密过程。

应用场景:

  1. 网络通信:AES算法可以用于保护网络通信中的数据传输安全,例如加密敏感信息的传输,防止数据被窃取或篡改。
  2. 数据存储:AES算法可以用于加密存储在本地设备或云端的敏感数据,确保数据在存储过程中的安全性。
  3. 身份验证:AES算法可以用于加密用户的身份信息,确保用户身份的安全性和隐私性。
  4. 加密文件:AES算法可以用于加密文件,保护文件的机密性,防止未经授权的访问。

算法举例

第一种:指定的规则

package com.yun.greedy.testJava.AES;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class AESUtils {
    private static final String ENCODE_CHARSET = "UTF-8";
    private static final String ENCODE_ALGORITHM = "AES";
    private static final String ENCODE_TRANSFORMATION = "AES/ECB/PKCS5Padding";

    public AESUtils() {
    }

    /**
     * 过时 不推荐
     */
    @Deprecated
    public static String AESEncodeWithHex(String rule, String content) throws Exception {
        byte[] result = cipher(buildSecretKey(rule), content.getBytes(ENCODE_CHARSET), 1);
        return bytes2Hex(result);
    }

    /**
     * 过时 不推荐
     */
    @Deprecated
    public static String AESDecodeWithHex(String rule, String content) throws Exception {
        byte[] result = cipher(buildSecretKey(rule), hex2Bytes(content), 2);
        return new String(result, ENCODE_CHARSET);
    }

    /**
     * 解密
     *
     * @param rule    加密规则  必须为16, 24, 32位字符
     * @param content 加密字段
     * @return
     * @throws Exception
     */
    public static String AESEncode(String rule, String content) throws Exception {
        byte[] result = cipher(buildSecretKey(rule), content.getBytes(ENCODE_CHARSET), 1);
        return Base64.getEncoder().encodeToString(result);
    }

    /**
     * 加密
     *
     * @param rule    加密规则  必须为16, 24, 32位字符
     * @param content 密文(加密后的字段)
     * @return
     * @throws Exception
     */
    public static String AESDecode(String rule, String content) throws Exception {
        byte[] contentBytes = Base64.getDecoder().decode(content);
        byte[] result = cipher(buildSecretKey(rule), contentBytes, 2);
        return new String(result, ENCODE_CHARSET);
    }

    /**
     * 过时 不推荐
     */
    @Deprecated
    private static String bytes2Hex(byte[] bytes) {
        StringBuffer sb = new StringBuffer(bytes.length);
        byte[] var3 = bytes;
        int var4 = bytes.length;

        for (int var5 = 0; var5 < var4; ++var5) {
            byte b = var3[var5];
            String hex = Integer.toHexString(255 & b);
            if (hex.length() < 2) {
                sb.append(0);
            }

            sb.append(hex.toUpperCase());
        }

        return sb.toString();
    }

    /**
     * 过时 不推荐
     */
    @Deprecated
    private static byte[] hex2Bytes(String hex) {
        String str = "0123456789ABCDEF";
        char[] hexs = hex.toCharArray();
        byte[] bytes = new byte[hex.length() / 2];

        for (int i = 0; i < bytes.length; ++i) {
            int n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 255);
        }

        return bytes;
    }

    private static SecretKeySpec buildSecretKey(String rule) throws UnsupportedEncodingException {
        return new SecretKeySpec(rule.getBytes(ENCODE_CHARSET), ENCODE_ALGORITHM);
    }

    public static byte[] cipher(SecretKeySpec secretKey, byte[] input, int mode) throws Exception {
        Cipher cipher = Cipher.getInstance(ENCODE_TRANSFORMATION);
        cipher.init(mode, secretKey);
        return cipher.doFinal(input);
    }
}

验证测试

package com.yun.greedy.testJava.AES;

public class AESTest {
    public static void main(String[] args) {
        String content = "加密字段";//原文
        String rule = "ASD3243ghuy56456@*!&D242";//加密规则必须为16, 24, 32位字符
        System.out.println("加密规则长度:" + rule.length());
        try {
            String encode = AESUtils.AESEncode(rule, content);
            System.out.println("加密:" + encode);
            String decode = AESUtils.AESDecode(rule, encode);
            System.out.println("原文:" + content);
            System.out.println("解密:" + decode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

第二种:随机生成的规则


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;

public class AESExample {
    /**
     * 选择AES算法和加密模式(例如,CBC模式)
     */
    public static final String AES_CBC = "AES/CBC/PKCS5Padding";
    /**
     * 创建密钥规范
     */
    public static final String AES_KEY = "AES";

    public static void main(String[] args) throws Exception {
        // 生成随机的128位密钥
        byte[] key = getRule(16);
        // 要加密的明文数据
        String plaintext = "Hello, World!";
        // 加密数据
        String encrypt = encrypt(plaintext, key);
        // 解密数据
        String decryptedPlaintext = decrypt(encrypt, key);
        System.out.println("明文数据: " + plaintext);
        System.out.println("加密后的数据: " + encrypt);
        System.out.println("解密后的数据: " + decryptedPlaintext);
    }

    /**
     * 获取规则key(随机生成的规则)
     *
     * @param i 秘钥16, 24, 32位数
     * @return
     */
    public static byte[] getRule(int i) throws Exception {
        if (i != 16 && i != 24 && i != 32) {
            throw new Exception("规则位数必须是16或24或32位数!");
        }
        StringBuffer sb = new StringBuffer();
        // 生成随机的密钥
        byte[] key = new byte[i];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(key);
        for (int in : key) {
            sb.append(in).append(",");
        }
        String substring = sb.substring(0, sb.length() - 1);
        System.out.println("秘钥:" + substring);
        return key;
    }

    /**
     * 创建密钥规范
     *
     * @param key 秘钥
     * @return
     */
    public static SecretKeySpec secretKeySpec(byte[] key) {
        return new SecretKeySpec(key, AES_KEY);
    }

    /**
     * 选择AES算法和加密模式
     *
     * @param key 秘钥
     * @param i   加密1 解密2
     * @return
     * @throws Exception
     */
    public static Cipher cipher(byte[] key, int i) throws Exception {
        SecretKeySpec secretKeySpec = secretKeySpec(key);
        // 创建初始化向量
        IvParameterSpec ivParameterSpec = new IvParameterSpec(key);
        // 选择AES算法和加密模式(例如,CBC模式)
        Cipher cipher = Cipher.getInstance(AES_CBC);
        // 初始化加密器
        cipher.init(i, secretKeySpec, ivParameterSpec);//加密
        return cipher;
    }

    /**
     * 加密
     *
     * @param plaintext 加密明文
     * @param key       秘钥
     * @return
     * @throws Exception
     */
    public static String encrypt(String plaintext, byte[] key) throws Exception {
        // 选择AES算法和加密模式(例如,CBC模式)
        Cipher cipher = cipher(key, Cipher.ENCRYPT_MODE);
        // 加密数据
        byte[] bytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
        String encode = Base64.getEncoder().encodeToString(bytes);
        return encode;
    }

    /**
     * 解密
     *
     * @param encrypt 加密的数据
     * @param key     秘钥
     * @return
     * @throws Exception
     */
    public static String decrypt(String encrypt, byte[] key) throws Exception {
        byte[] decode = Base64.getDecoder().decode(encrypt);
        // 选择AES算法和加密模式(例如,CBC模式)
        Cipher cipher = cipher(key, Cipher.DECRYPT_MODE);
        // 解密数据
        byte[] plaintextBytes = cipher.doFinal(decode);
        String plaintext = new String(plaintextBytes, StandardCharsets.UTF_8);
        return plaintext;
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一百减一是零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值