Java实现AES-128-ECB加密解密

Java实现AES-128-ECB加密解密

AES加密标准又称为高级加密标准Rijndael加密法,是美国国家标准技术研究所NIST旨在取代DES的21世纪的加密标准。

下面就是我使用AES加密的java代码实现,可供大家参考:

package com.aas.fc.util;

import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * AES加密工具类
 * MD5值后16位为key,AES采用128位ECB模式,填充模式pkcs5,加密之后再base64转码
 */
public class AESUtils {

    /**
     * 加密
     * @param content
     * @param key
     * @return
     * @throws Exception
     */
    public static String Encrypt(String content, String key) throws Exception {
        // 判断Key是否为16位
        if (key.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(128, new SecureRandom(key.getBytes()));
        SecretKey secretKey = keyGenerator.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat , "AES");
        //"算法/模式/补码方式"
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
        //使用BASE64做转码功能,能起到2次加密的作用。
        return new Base64().encodeToString(encrypted);
    }

    /**
     * 解密
     * @param content
     * @param key
     * @return
     * @throws Exception
     */
    public static String Decrypt(String content, String key) throws Exception {
        try {
            // 判断Key是否为16位
            if (key.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(key.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat , "AES");
            //"算法/模式/补码方式"
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            //先base64解密
            byte[] encrypted1 = new Base64().decode(content);
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original,"utf-8");
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }


    public static void main(String[] args) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        //此处使用AES-128-ECB加密模式,key需要为16位。
        String key = "8749284927849201";
        // 原字符串
        String content = "hello world!";
        System.out.println(simpleDateFormat.format(new Date())+"原字符串是:"+content);
        // 加密
        String encryptContent = AESUtils.Encrypt(content, key);
        System.out.println(simpleDateFormat.format(new Date()) + "加密后的字符串是:" + encryptContent);
        // 解密
        String decryptContent = AESUtils.Decrypt(encryptContent, key);
        System.out.println(simpleDateFormat.format(new Date()) + "解密后的字符串是:" + decryptContent);
    }


}

执行效果如下:
在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
AES(Advanced Encryption Standard)是一种对称加密算法,可以用于加密和解密数据。在Java中,可以使用javax.crypto包中的类来实现AES加密算法。 下面是一个简单的示例代码,演示如何使用AES加密算法进行加密和解密: ```java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class AESExample { private static final String ALGORITHM = "AES"; private static final String KEY = "0123456789abcdef"; // 密钥,长度必须为16字节 public static String encrypt(String data) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] encryptedBytes = cipher.doFinal(data.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } public static String decrypt(String encryptedData) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData)); return new String(decryptedBytes); } public static void main(String[] args) { try { String originalData = "Hello, AES!"; System.out.println("原始数据: " + originalData); String encryptedData = encrypt(originalData); System.out.println("加密后的数据: " + encryptedData); String decryptedData = decrypt(encryptedData); System.out.println("解密后的数据: " + decryptedData); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例代码中,我们使用AES算法加密和解密数据。密钥长度必须为16字节(128位),可以根据需要修改。`encrypt`方法用于加密数据,`decrypt`方法用于解密数据。 请注意,这只是一个简单的示例,实际应用中需要注意密钥的安全性和其他加密参数的设置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值