如何使用java實作AES加密演算法
導言:
在網路應用程式和資料傳輸過程中,資料的安全性是至關重要的。加密演算法是保護資料安全的重要手段之一。 AES(Advanced Encryption Standard)是目前最常用的對稱加密演算法之一,具有高度的安全性、效率和靈活性。本文將介紹如何使用Java程式語言實作AES加密演算法,以保護資料的安全。
匯入相關函式庫
在使用Java實作AES加密演算法之前,我們需要匯入相關的函式庫檔案。在Java中,可以使用javax.crypto函式庫來實作AES加密演算法。我們需要導入以下庫檔案:
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.SecretKeySpec;
實作AES加密和解密方法
public class AESUtil { private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; private static final String SECRET_KEY_ALGORITHM = "PBKDF2WithHmacSHA256"; private static final int ITERATION_COUNT = 65536; private static final int KEY_LENGTH = 128; private static final int IV_LENGTH = 16; public static byte[] encrypt(String plaintext, String password) throws Exception { byte[] salt = generateSalt(); PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(SECRET_KEY_ALGORITHM); SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec); byte[] iv = generateIV(); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv)); byte[] ciphertext = cipher.doFinal(plaintext.getBytes("UTF-8")); byte[] encrypted = new byte[iv.length + salt.length + ciphertext.length]; System.arraycopy(iv, 0, encrypted, 0, iv.length); System.arraycopy(salt, 0, encrypted, iv.length, salt.length); System.arraycopy(ciphertext, 0, encrypted, iv.length + salt.length, ciphertext.length); return encrypted; } public static String decrypt(byte[] encrypted, String password) throws Exception { byte[] iv = new byte[IV_LENGTH]; System.arraycopy(encrypted, 0, iv, 0, iv.length); byte[] salt = new byte[encrypted.length - iv.length - KEY_LENGTH / 8]; System.arraycopy(encrypted, iv.length, salt, 0, salt.length); byte[] ciphertext = new byte[encrypted.length - iv.length - salt.length]; System.arraycopy(encrypted, iv.length + salt.length, ciphertext, 0, ciphertext.length); PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATION_COUNT, KEY_LENGTH); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(SECRET_KEY_ALGORITHM); SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); byte[] plaintext = cipher.doFinal(ciphertext); return new String(plaintext, "UTF-8"); } private static byte[] generateSalt() { byte[] salt = new byte[KEY_LENGTH / 8]; SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(salt); return salt; } private static byte[] generateIV() { byte[] iv = new byte[IV_LENGTH]; SecureRandom secureRandom = new SecureRandom(); secureRandom.nextBytes(iv); return iv; } }
使用範例
現在我們可以使用上述實現的AES加密演算法來加密和解密資料了。
public class Main { public static void main(String[] args) { try { String plaintext = "Hello, World!"; String password = "MySecretPassword"; byte[] encrypted = AESUtil.encrypt(plaintext, password); System.out.println("Encrypted data: " + Arrays.toString(encrypted)); String decrypted = AESUtil.decrypt(encrypted, password); System.out.println("Decrypted data: " + decrypted); } catch (Exception e) { e.printStackTrace(); } } }
以上是如何使用java實作AES加密演算法的詳細內容。更多資訊請關注PHP中文網其他相關文章!