java如何实现加密
Java实现加密的方法
Java提供了多种加密方式,包括对称加密、非对称加密和哈希加密。以下是一些常见的加密实现方法:

对称加密(AES)
对称加密使用相同的密钥进行加密和解密。AES(Advanced Encryption Standard)是一种常用的对称加密算法。

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
public class AESEncryption {
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256); // 可以使用128或192位
return keyGenerator.generateKey();
}
public static String encrypt(String plainText, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decryptedBytes);
}
}
非对称加密(RSA)
非对称加密使用公钥和私钥进行加密和解密。RSA是一种常见的非对称加密算法。
import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.util.Base64;
public class RSAEncryption {
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); // 密钥长度
return keyPairGenerator.generateKeyPair();
}
public static String encrypt(String plainText, java.security.PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, java.security.PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decryptedBytes);
}
}
哈希加密(SHA-256)
哈希加密是一种单向加密,通常用于密码存储。SHA-256是一种常用的哈希算法。
import java.security.MessageDigest;
import java.util.Base64;
public class SHA256Encryption {
public static String hash(String input) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = digest.digest(input.getBytes());
return Base64.getEncoder().encodeToString(hashBytes);
}
}
使用示例
public class Main {
public static void main(String[] args) throws Exception {
// AES示例
SecretKey aesKey = AESEncryption.generateKey();
String aesEncrypted = AESEncryption.encrypt("Hello World", aesKey);
String aesDecrypted = AESEncryption.decrypt(aesEncrypted, aesKey);
System.out.println("AES Decrypted: " + aesDecrypted);
// RSA示例
KeyPair rsaKeyPair = RSAEncryption.generateKeyPair();
String rsaEncrypted = RSAEncryption.encrypt("Hello World", rsaKeyPair.getPublic());
String rsaDecrypted = RSAEncryption.decrypt(rsaEncrypted, rsaKeyPair.getPrivate());
System.out.println("RSA Decrypted: " + rsaDecrypted);
// SHA-256示例
String shaHash = SHA256Encryption.hash("Hello World");
System.out.println("SHA-256 Hash: " + shaHash);
}
}
以上代码展示了如何在Java中实现常见的加密算法。根据具体需求选择合适的加密方式。






