Java对称加密算法原理详细讲解
什么是对称加密算法?
对称加密算法是指加密和解密使用的密钥相同的一类加密算法。在数据传输过程中,发送方使用密钥对数据进行加密,接收方使用同样的密钥对数据进行解密,因此只有知道密钥的人才能够对数据进行解密。对称加密算法具有加密速度快、加密强度高的优点,但其缺陷在于密钥需要被发送方和接收方共享,如果密钥被攻击者获取,那么数据就存在被解密的风险。
常见的对称加密算法有DES、3DES、AES等。
Java对称加密算法实现
Java提供了大量对称加密算法的实现,常用的有以下几种:
DES加密算法
DES(Data Encryption Standard),数据加密标准,是一种对称密钥加密算法。下面是一个DES加密的Java实例:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;
public class DES {
public static void main(String[] args) throws Exception {
String plainText = "Hello, World!";
String key = "testkey";
byte[] cipherText = desEncrypt(plainText.getBytes(), key.getBytes());
System.out.println(new String(cipherText));
byte[] result = desDecrypt(cipherText, key.getBytes());
System.out.println(new String(result));
}
public static byte[] desEncrypt(byte[] plainText, byte[] key) throws Exception {
SecureRandom random = new SecureRandom();
DESKeySpec keySpec = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
return cipher.doFinal(plainText);
}
public static byte[] desDecrypt(byte[] cipherText, byte[] key) throws Exception {
SecureRandom random = new SecureRandom();
DESKeySpec keySpec = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
return cipher.doFinal(cipherText);
}
}
AES加密算法
AES(Advanced Encryption Standard),高级加密标准,是一种对称密钥加密算法。下面是一个AES加密的Java实例:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
public class AES {
public static void main(String[] args) throws Exception {
String plainText = "Hello, World!";
String key = "testkey";
byte[] cipherText = aesEncrypt(plainText.getBytes(), key.getBytes());
System.out.println(new String(cipherText));
byte[] result = aesDecrypt(cipherText, key.getBytes());
System.out.println(new String(result));
}
public static byte[] aesEncrypt(byte[] plainText, byte[] key) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = new SecureRandom(key);
keyGenerator.init(256, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] keyBytes = secretKey.getEncoded();
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
return cipher.doFinal(plainText);
}
public static byte[] aesDecrypt(byte[] cipherText, byte[] key) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
return cipher.doFinal(cipherText);
}
}
什么是非对称加密算法?
非对称加密算法也叫公钥加密算法,它与对称加密算法不同的是加密和解密使用的是不同的密钥。一个密钥被称为公钥,另一个密钥被称为私钥。在数据传输过程中,发送方使用接收方的公钥对数据进行加密,接收方使用自己的私钥对数据进行解密。因此对于加密数据,只有具有接收方私钥的人才能够进行解密,非对称加密算法在数据传输过程中保证了数据的安全性,但加密和解密速度较慢。
常见的非对称加密算法有RSA、DSA等。
Java非对称加密算法实现
Java提供了大量非对称加密算法的实现,常用的有以下几种:
RSA加密算法
RSA(Rivest-Shamir-Adleman)是一种非对称加密算法,它基于一个数学难题:质因数分解。下面是一个RSA加密的Java实例:
import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
public class RSA {
public static void main(String[] args) throws Exception {
String plainText = "Hello, World!";
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
byte[] cipherText = rsaEncrypt(plainText.getBytes(), publicKey);
System.out.println(new String(cipherText));
byte[] result = rsaDecrypt(cipherText, privateKey);
System.out.println(new String(result));
}
public static byte[] rsaEncrypt(byte[] plainText, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(plainText);
}
public static byte[] rsaDecrypt(byte[] cipherText, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(cipherText);
}
}
DSA签名算法
DSA(Digital Signature Algorithm),数字签名算法,是一种公钥加密算法。它使用了一个哈希函数,将消息的摘要加密,并由发送方的私钥进行加密。接收方使用发送方的公钥对加密后的消息进行解密,然后使用相同的哈希函数对消息进行摘要验证。下面是一个DSA签名的Java实例:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
public class DSA {
public static void main(String[] args) throws Exception {
String plainText = "Hello, World!";
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
byte[] signText = dsaSign(plainText.getBytes(), privateKey);
System.out.println(new String(signText));
boolean result = dsaVerify(plainText.getBytes(), signText, publicKey);
System.out.println(result);
}
public static byte[] dsaSign(byte[] plainText, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance("DSA");
signature.initSign(privateKey);
signature.update(plainText);
return signature.sign();
}
public static boolean dsaVerify(byte[] plainText, byte[] signText, PublicKey publicKey) throws Exception {
Signature signature = Signature.getInstance("DSA");
signature.initVerify(publicKey);
signature.update(plainText);
return signature.verify(signText);
}
}
以上就是Java对称加密和非对称加密算法的实现,并包含了DES、AES、RSA、DSA等多种算法的使用示例。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java对称与非对称加密算法原理详细讲解 - Python技术站