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

简介: 加密解密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;
    }
}


相关文章
|
3月前
|
人工智能 数据安全/隐私保护
电话加密
【2月更文挑战第3天】电话加密。
21 3
|
12月前
|
存储 机器学习/深度学习 JSON
【安全】如果您的JWT被盗,会发生什么?
【安全】如果您的JWT被盗,会发生什么?
|
算法
支付宝接口的数字签名
通过阅读本篇文章,你可以了解到数字签名技术,了解支付宝接口的签名和验签的流程
183 0
|
算法 数据安全/隐私保护 Python
凯撒加密Caesar cipher
凯撒加密Caesar cipher
240 0
凯撒加密Caesar cipher
|
安全 测试技术 数据安全/隐私保护
认证、加密及数字签名的区别
认证、加密及数字签名的区别
268 0
|
开发工具
教程-使用支付宝公钥验签
阅读角色:技术同学 技术同学把1).APPID,2).应用私钥,3).支付宝公钥,配置在代码中,对请求内容进行签名,并对支付宝返回的内容进行验签。 支付宝开放平台SDK封装了签名和验签过程,只需配置账号及密钥参数即可,强烈建议使用。
1435 0
|
存储 Web App开发 编解码
加解密,加签、验签也就这肥事
加解密,加签、验签也就这肥事
1302 0
加解密,加签、验签也就这肥事
如何上传商户公钥RSA2,获取支付宝公钥
说明:   所有说的公钥上传都是指上传商户自己公钥一般命名:rsa_public_key   appid请求的请上传公钥到您的appid下面   pid请求的请上传到mapi网关密钥产品中  密钥上传(以上传到开放平台为例):   1.登录上传公钥地址【点击登录】   2.首先确认要上传到哪一个appid下(如下图)需要上传RSA2(SHA256)密钥(推荐),还是上传RSA(SHA1)密钥         3.打开》设置应用公钥》需要手机验证,验证完成跳转(如下图)         4.选择rsa_public_key.pem打开复制里面商户公钥字符串》粘贴到上图框中,选择保存。
1176 0
|
算法 Java 数据安全/隐私保护
登录注册之加密算法
通常不论是cms或者crm或者erp或者b2b等项目,对于登录注册全部都是加密的,注册对密码加密,登录比较加密后的密码。安全性在任何时候都是最重要的。 下面贴一下我个人比较常用的加密,加密又分可逆与不可逆,目前安全系数比较高的就是不可逆,当然通过技术还算可以破解得到明文的,但是有很多方式可以使破解的难度系数达到比较高的值,这样一来,部分黑客破解时,花费的时间周期就比较长,从而保证用户的账户一定程度是安全的。
1073 0
|
C# 数据安全/隐私保护
C#实现微信AES-128-CBC加密数据的解密
原文:C#实现微信AES-128-CBC加密数据的解密 1.微信小程序登录获取用户的openid信息 需要使用AES-128-CBC解密处理 /// /// ASE加解密 /// ...
1505 0