Java加密源代码科普
引言
在现代社会中,信息安全是一个非常重要的话题。随着互联网的普及和数据传输的频繁,保护敏感信息的安全性变得尤为关键。为了实现这个目标,加密技术应运而生。在本文中,我们将探讨Java中的加密源代码,并提供一些示例帮助读者理解这些概念。
加密基础
加密是指将原始数据(称为明文)通过某种算法转换为不可读的数据(称为密文),以保护数据的机密性。只有掌握正确的密钥才能解密密文并还原为原始数据。在Java中,有多种加密算法可供选择,包括对称加密和非对称加密。
对称加密
对称加密是指使用同一个密钥进行加密和解密的加密算法。常见的对称加密算法有DES,AES等。下面是一个使用AES对称加密算法的Java示例代码:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class SymmetricEncryption {
private static final String ALGORITHM = "AES";
public static String encrypt(String plainText, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
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 {
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecretKey secretKey = keyGenerator.generateKey();
String plainText = "Hello, World!";
String encryptedText = encrypt(plainText, secretKey);
String decryptedText = decrypt(encryptedText, secretKey);
System.out.println("Plain Text: " + plainText);
System.out.println("Encrypted Text: " + encryptedText);
System.out.println("Decrypted Text: " + decryptedText);
}
}
在这个示例中,我们使用AES算法进行加密和解密。我们首先生成一个随机密钥,然后使用该密钥对明文进行加密。加密后的密文通过Base64编码转换为字符串。然后我们使用同样的密钥对密文进行解密,并将解密后的字节数组转换为字符串。
非对称加密
非对称加密是指使用不同的密钥进行加密和解密的加密算法。常见的非对称加密算法有RSA,DSA等。下面是一个使用RSA非对称加密算法的Java示例代码:
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
public class AsymmetricEncryption {
private static final String ALGORITHM = "RSA";
public static String encrypt(String plainText, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, PrivateKey privateKey) throws Exception {
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String plainText = "Hello, World!";
String encryptedText = encrypt(plainText, publicKey);
String decryptedText = decrypt(encryptedText, privateKey);
System.out