您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在 Java 中,有多种方法可以实现数据加密。这里,我将向您介绍两种常用的加密算法:AES(高级加密标准)和 DES(数据加密标准)。
AES 是一种对称加密算法,它使用相同的密钥进行加密和解密。以下是一个使用 AES 加密和解密数据的示例:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class AesEncryption {
public static void main(String[] args) throws Exception {
String originalData = "Hello, World!";
String encryptedData = encrypt(originalData);
String decryptedData = decrypt(encryptedData);
System.out.println("Original Data: " + originalData);
System.out.println("Encrypted Data: " + encryptedData);
System.out.println("Decrypted Data: " + decryptedData);
}
public static String encrypt(String data) throws Exception {
SecretKey secretKey = generateSecretKey();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedData) throws Exception {
SecretKey secretKey = generateSecretKey();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedData);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
public static SecretKey generateSecretKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128); // 设置密钥长度,可以是 128、192 或 256 位
return keyGenerator.generateKey();
}
}
DES 是一种较旧的加密算法,它使用 56 位密钥进行加密和解密。以下是一个使用 DES 加密和解密数据的示例:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class DesEncryption {
public static void main(String[] args) throws Exception {
String originalData = "Hello, World!";
String encryptedData = encrypt(originalData);
String decryptedData = decrypt(encryptedData);
System.out.println("Original Data: " + originalData);
System.out.println("Encrypted Data: " + encryptedData);
System.out.println("Decrypted Data: " + decryptedData);
}
public static String encrypt(String data) throws Exception {
DESKeySpec desKeySpec = new DESKeySpec("12345678".getBytes(StandardCharsets.UTF_8)); // 设置密钥
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedData) throws Exception {
DESKeySpec desKeySpec = new DESKeySpec("12345678".getBytes(StandardCharsets.UTF_8)); // 设置密钥
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedData);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
}
请注意,这些示例仅用于演示目的。在实际应用中,您需要考虑更多的安全因素,例如密钥管理、加密模式(如 CBC、CFB 等)和填充方案(如 PKCS5Padding、NoPadding 等)。此外,对于更高级的加密需求,您可以考虑使用 Java 加密扩展(Java Cryptography Extension, JCE)或 Java Cryptography Architecture(JCA)。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。