Java实现AES加密解密

您所在的位置:网站首页 obs加密文件解密 Java实现AES加密解密

Java实现AES加密解密

2023-06-13 22:11| 来源: 网络整理| 查看: 265

AES简介

AES(Advanced Encryption Standard)高级加密标准,是一种被广泛使用的对称加密算法,用于加密和解密数据。它曾经是美国政府的一个机密标准,但现在已成为公开的加密算法,并被广泛使用于商业、政府及军事领域。

AES算法有三种不同的密钥长度:128位、192位和256位,每种长度有不同数量的轮数,其中128位密钥需要进行10轮加密,192位密钥需要进行12轮加密,256位密钥需要进行14轮加密。其中,轮数指的是加密算法中处理数据的重复次数,每轮中会对数据进行代换、置换、线性变换等操作,以增强加密强度。

AES算法的加密和解密过程使用的是相同的密钥,因此被称为对称密钥加密算法。它通过将明文按照固定大小分块(128比特),并对每个块进行相同的加密操作,从而实现加密。解密时则对密文进行反向操作即可。

总体来说,AES算法具有安全、高效等优势,已成为目前最常用的加密算法之一。

示例

 以下是使用Java实现AES加密和解密文件的示例代码:

import java.security.Key; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class AES { private static final String ALGORITHM = "AES"; /** * AES加密字符串 * @param key 密钥 * @param value 待加密字符串 */ public static String encrypt(String password, String value) throws Exception { Key key = generateKey(password); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptedByteValue = cipher.doFinal(value.getBytes("utf-8")); String encryptedValue64 = Base64.getEncoder().encodeToString(encryptedByteValue); return encryptedValue64; } /** * AES解密字符串 * @param key 密钥 * @param value 待解密字符串 * @return */ public static String decrypt(String password, String value) throws Exception { Key key = generateKey(password); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptedValue64 = Base64.getDecoder().decode(value); byte[] decryptedByteValue = cipher.doFinal(decryptedValue64); String decryptedValue = new String(decryptedByteValue,"utf-8"); return decryptedValue; } /** * AES加密文件 * @param password 密钥 * @param inputFile 待加密文件路径 * @param outputFile 输出文件路径 */ public static void encrypt(String password,String inputFile, String outputFile) throws Exception { Key key = generateKey(password); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); FileInputStream inputStream = new FileInputStream(inputFile); byte[] inputBytes = new byte[(int) inputFile.length()]; inputStream.read(inputBytes); byte[] outputBytes = cipher.doFinal(inputBytes); FileOutputStream outputStream = new FileOutputStream(outputFile); outputStream.write(outputBytes); inputStream.close(); outputStream.close(); } /** * AES解密文件 * @param password 密钥 * @param inputFile 待解密文件路径 * @param outputFile 输出文件路径 */ public static void decrypt(String password, String inputFile, String outputFile) throws Exception{ Key key = generateKey(password); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); FileInputStream inputStream = new FileInputStream(inputFile); byte[] inputBytes = new byte[(int) inputFile.length()]; inputStream.read(inputBytes); byte[] outputBytes = cipher.doFinal(inputBytes); FileOutputStream outputStream = new FileOutputStream(outputFile); outputStream.write(outputBytes); inputStream.close(); outputStream.close(); } /** * 生成key * @param password * @return * @throws Exception */ private static Key generateKey(String password) throws Exception { Key key = new SecretKeySpec(password.getBytes(),ALGORITHM); return key; } }

测试:

public static void main(String[] args) throws Exception { String originalString = "hello world"; String key = "12345678"; String encryptedString = AES.encrypt(key, originalString); String decryptedString = AES.decrypt(key, encryptedString); System.out.println(originalString); System.out.println(encryptedString); System.out.println(decryptedString); }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3