阅读量:0
在Java中,使用PKCS#1和PKCS#8进行加密和解密操作,你可以使用java.security
包中的Cipher
类。以下是使用这两种标准进行加密和解密的示例代码:
- PKCS#1加密:
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class PKCS1Encryption { public static void main(String[] args) throws NoSuchAlgorithmException { // 生成RSA密钥对 KeyGenerator keyGenerator = KeyGenerator.getInstance("RSA"); keyGenerator.init(2048); SecretKey secretKey = keyGenerator.generateKey(); // 使用PKCS#1填充模式进行加密 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); String plainText = "Hello, World!"; byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); // 输出加密后的Base64编码字符串 System.out.println("Encrypted text: " + Base64.getEncoder().encodeToString(encryptedBytes)); } }
- PKCS#1解密:
import javax.crypto.Cipher; import javax.crypto.SecretKey; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class PKCS1Decryption { public static void main(String[] args) throws NoSuchAlgorithmException { // 生成RSA密钥对(与加密时使用相同的密钥) KeyGenerator keyGenerator = KeyGenerator.getInstance("RSA"); keyGenerator.init(2048); SecretKey secretKey = keyGenerator.generateKey(); // 使用PKCS#1填充模式进行解密 Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, secretKey); String encryptedText = "Base64EncodedEncryptedText"; // 替换为实际的加密后的Base64编码字符串 byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); // 输出解密后的文本 System.out.println("Decrypted text: " + new String(decryptedBytes)); } }
- PKCS#8加密:
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.PKCS8EncodedKeySpec; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class PKCS8Encryption { public static void main(String[] args) throws NoSuchAlgorithmException { // 生成RSA密钥对 KeyGenerator keyGenerator = KeyGenerator.getInstance("RSA"); keyGenerator.init(2048); SecretKey secretKey = keyGenerator.generateKey(); // 将密钥转换为PKCS#8编码格式 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(secretKey.getEncoded()); // 使用PKCS#8填充模式进行加密(注意:这里需要使用一个支持PKCS#8的加密算法,如AES) Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); String plainText = "Hello, World!"; byte[] encryptedBytes = cipher.doFinal(plainText.getBytes()); // 输出加密后的Base64编码字符串 System.out.println("Encrypted text: " + Base64.getEncoder().encodeToString(encryptedBytes)); } }
- PKCS#8解密:
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.PKCS8EncodedKeySpec; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class PKCS8Decryption { public static void main(String[] args) throws NoSuchAlgorithmException { // 生成RSA密钥对 KeyGenerator keyGenerator = KeyGenerator.getInstance("RSA"); keyGenerator.init(2048); SecretKey secretKey = keyGenerator.generateKey(); // 将密钥转换为PKCS#8编码格式 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(secretKey.getEncoded()); // 使用