一、JavaCrypto編碼
JavaCrypto是Java平台自帶的加密解密API,可以進行各種加密算法的實現,包括對稱加密和非對稱加密。對稱加密的特點是加密解密使用的是相同的密鑰,常見的有DES、AES等算法;非對稱加密的特點是加密解密使用的是不同的密鑰,常見的有RSA、DSA等算法。
JavaCrypto為我們提供了進行加密解密的工具。下面給出一個DES加密的示例代碼:
public static String encrypt(String plaintext, String password) { try { SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "DES"); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedData = cipher.doFinal(plaintext.getBytes()); return new String(Base64.getEncoder().encode(encryptedData)); } catch(Exception e) { e.printStackTrace(); } return null; }
以上代碼實現了對一個字符串進行DES加密,並返回了加密結果的Base64編碼。
二、JavaCrypto解密
JavaCrypto還提供了各種解密算法的實現,包括對稱解密和非對稱解密。對稱解密的特點是和加密使用相同的密鑰進行解密,常見的有DES、AES等算法;非對稱解密的特點是使用私鑰進行解密,常見的有RSA、DSA等算法。
下面給出一個DES解密的示例代碼:
public static String decrypt(String ciphertext, String password) { try { SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "DES"); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(ciphertext.getBytes())); return new String(decryptedData); } catch(Exception e) { e.printStackTrace(); } return null; }
以上代碼實現了對一個DES加密後的結果進行解密,並返回了解密得到的原文。
三、JavaCrypto應用案例
JavaCrypto的應用場景非常廣泛,下面給出一個簡單的應用案例——對一個文件進行AES加密。
首先需要生成一個AES密鑰:
public static byte[] generateAesKey() { try { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } catch(Exception e) { e.printStackTrace(); } return null; }
以上代碼通過AES算法生成了一個128位的密鑰。
接下來對文件進行AES加密:
public static void encryptFile(String inputFilePath, String outputFilePath, byte[] aesKey) { try { SecretKeySpec secretKey = new SecretKeySpec(aesKey, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); FileInputStream inputStream = new FileInputStream(inputFilePath); FileOutputStream outputStream = new FileOutputStream(outputFilePath); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { byte[] encryptedData = cipher.update(buffer, 0, length); outputStream.write(encryptedData); } byte[] encryptedData = cipher.doFinal(); outputStream.write(encryptedData); inputStream.close(); outputStream.flush(); outputStream.close(); } catch(Exception e) { e.printStackTrace(); } }
以上代碼實現了對一個文件進行AES加密,並將結果輸出到另一個文件中。
最後,對加密後的文件進行解密:
public static void decryptFile(String inputFilePath, String outputFilePath, byte[] aesKey) { try { SecretKeySpec secretKey = new SecretKeySpec(aesKey, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); FileInputStream inputStream = new FileInputStream(inputFilePath); FileOutputStream outputStream = new FileOutputStream(outputFilePath); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { byte[] decryptedData = cipher.update(buffer, 0, length); outputStream.write(decryptedData); } byte[] decryptedData = cipher.doFinal(); outputStream.write(decryptedData); inputStream.close(); outputStream.flush(); outputStream.close(); } catch(Exception e) { e.printStackTrace(); } }
以上代碼實現了對加密後的文件進行解密,並將結果輸出到另一個文件中。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/156757.html