以下是一個使用Java編寫的AES算法實例,使用AES(CBC模式)對數(shù)據(jù)進(jìn)行加密和解密。代碼中包括了生成隨機(jī)密鑰和密鑰偏移量的方法。
java
Copy code
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
public class AESExample {
? ? private static final String AES_ALGORITHM = "AES";
? ? private static final String AES_TRANSFORMATION = "AES/CBC/PKCS5Padding";
? ? private static final int AES_KEY_SIZE = 256;
? ? public static void main(String[] args) {
? ? ? ? try {
? ? ? ? ? ? // 生成隨機(jī)密鑰
? ? ? ? ? ? SecretKey secretKey = generateRandomKey();
? ? ? ? ? ? // 生成隨機(jī)密鑰偏移量
? ? ? ? ? ? byte[] iv = generateRandomIV();
? ? ? ? ? ? String plaintext = "Hello, World!";
? ? ? ? ? ? // 加密
? ? ? ? ? ? String ciphertext = encrypt(plaintext, secretKey, iv);
? ? ? ? ? ? System.out.println("加密后的文本:" + ciphertext);
? ? ? ? ? ? // 解密
? ? ? ? ? ? String decryptedText = decrypt(ciphertext, secretKey, iv);
? ? ? ? ? ? System.out.println("解密后的文本:" + decryptedText);
? ? ? ? } catch (NoSuchAlgorithmException | NoSuchPaddingException
? ? ? ? ? ? ? ? | InvalidKeyException | InvalidAlgorithmParameterException
? ? ? ? ? ? ? ? | IllegalBlockSizeException | BadPaddingException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? // 生成隨機(jī)密鑰
? ? public static SecretKey generateRandomKey() throws NoSuchAlgorithmException {
? ? ? ? KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_ALGORITHM);
? ? ? ? keyGenerator.init(AES_KEY_SIZE);
? ? ? ? return keyGenerator.generateKey();
? ? }
? ? // 生成隨機(jī)密鑰偏移量
? ? public static byte[] generateRandomIV() {
? ? ? ? SecureRandom secureRandom = new SecureRandom();
? ? ? ? byte[] iv = new byte[16];
? ? ? ? secureRandom.nextBytes(iv);
? ? ? ? return iv;
? ? }
? ? // AES加密
? ? public static String encrypt(String plaintext, SecretKey secretKey, byte[] iv)
? ? ? ? ? ? throws NoSuchPaddingException, NoSuchAlgorithmException,
? ? ? ? ? ? InvalidAlgorithmParameterException, InvalidKeyException,
? ? ? ? ? ? BadPaddingException, IllegalBlockSizeException {
? ? ? ? Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION);
? ? ? ? IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
? ? ? ? cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
? ? ? ? byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
? ? ? ? return Base64.getEncoder().encodeToString(encryptedBytes);
? ? }
? ? // AES解密
? ? public static String decrypt(String ciphertext, SecretKey secretKey, byte[] iv)
? ? ? ? ? ? throws NoSuchPaddingException, NoSuchAlgorithmException,
? ? ? ? ? ? InvalidAlgorithmParameterException, InvalidKeyException,
? ? ? ? ? ? BadPaddingException, IllegalBlockSizeException {
? ? ? ? Cipher cipher = Cipher.getInstance(AES_TRANSFORMATION);
? ? ? ? IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
? ? ? ? cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
? ? ? ? byte[] encryptedBytes = Base64.getDecoder().decode(ciphertext);
? ? ? ? byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
? ? ? ? return new String(decryptedBytes);
? ? }
}
上述代碼中,generateRandomKey方法用于生成隨機(jī)密鑰,generateRandomIV方法用于生成隨機(jī)密鑰偏移量。encrypt方法使用提供的密鑰和密鑰偏移量對給定的明文進(jìn)行加密,返回Base64編碼后的密文。decrypt方法使用相同的密鑰和密鑰偏移量對密文進(jìn)行解密,并返回解密后的明文。文章來源:http://www.zghlxwxcb.cn/news/detail-723862.html
請注意,上述代碼僅供示例目的,實際使用時需要妥善管理密鑰和密鑰偏移量以確保安全性。文章來源地址http://www.zghlxwxcb.cn/news/detail-723862.html
到了這里,關(guān)于用java語言寫一個AES算法,使用AES(CBC模式)對數(shù)據(jù)進(jìn)行加密或解密。加解密用到的密鑰(Key)和密鑰偏移量(IV),代碼實例類編寫。的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!