|

楼主 |
发表于 2022-6-2 10:10:43
|
显示全部楼层
RSA加密、签名区别
加密和签名都是为了安全性考虑,但略有不同。常有人问加密和签名是用私钥还是公钥?其实都是对加密和签名的作用有所混淆。简单的说,加密是为了防止信息被泄露,而签名是为了防止信息被篡改。这里举2个例子说明。
第一个场景:战场上,B要给A传递一条消息,内容为某一指令。
RSA的加密过程如下:
(1)A生成一对密钥(公钥和私钥),私钥不公开,A自己保留。公钥为公开的,任何人可以获取。
(2)A传递自己的公钥给B,B用A的公钥对消息进行加密。
(3)A接收到B加密的消息,利用A自己的私钥对消息进行解密。
在这个过程中,只有2次传递过程,第一次是A传递公钥给B,第二次是B传递加密消息给A,即使都被敌方截获,也没有危险性,因为只有A的私钥才能对消息进行解密,防止了消息内容的泄露。
第二个场景:A收到B发的消息后,需要进行回复“收到”。
RSA签名的过程如下:
(1)A生成一对密钥(公钥和私钥),私钥不公开,A自己保留。公钥为公开的,任何人可以获取。
(2)A用自己的私钥对消息加签,形成签名,并将加签的消息和消息本身一起传递给B。
(3)B收到消息后,在获取A的公钥进行验签,如果验签出来的内容与消息本身一致,证明消息是A回复的。
- import java.io.ByteArrayOutputStream;
- import java.security.KeyFactory;
- import java.security.KeyPair;
- import java.security.KeyPairGenerator;
- import java.security.PrivateKey;
- import java.security.PublicKey;
- import java.security.Signature;
- import java.security.spec.PKCS8EncodedKeySpec;
- import java.security.spec.X509EncodedKeySpec;
- import javax.crypto.Cipher;
- import org.apache.commons.codec.binary.Base64;
- public class TestRSA {
- private static final int MAX_ENCRYPT_BLOCK = 117;
- private static final int MAX_DECRYPT_BLOCK = 128;
- public static KeyPair getKeyPair() throws Exception {
- KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
- generator.initialize(1024);
- return generator.generateKeyPair();
- }
- public static PrivateKey getPrivateKey(String privateKey) throws Exception {
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());
- PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
- return keyFactory.generatePrivate(keySpec);
- }
- public static PublicKey getPublicKey(String publicKey) throws Exception {
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());
- X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
- return keyFactory.generatePublic(keySpec);
- }
- public static String encrypt(String data, PublicKey publicKey) throws Exception {
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
- int inputLen = data.getBytes().length;
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- int offset = 0;
- byte[] cache;
- int i = 0;
- while (inputLen - offset > 0) {
- if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
- cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
- } else {
- cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
- }
- out.write(cache, 0, cache.length);
- i++;
- offset = i * MAX_ENCRYPT_BLOCK;
- }
- byte[] encryptedData = out.toByteArray();
- out.close();
- return new String(Base64.encodeBase64String(encryptedData));
- }
- public static String decrypt(String data, PrivateKey privateKey) throws Exception {
- Cipher cipher = Cipher.getInstance("RSA");
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
- byte[] dataBytes = Base64.decodeBase64(data);
- int inputLen = dataBytes.length;
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- int offset = 0;
- byte[] cache;
- int i = 0;
- while (inputLen - offset > 0) {
- if (inputLen - offset > MAX_DECRYPT_BLOCK) {
- cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
- } else {
- cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
- }
- out.write(cache, 0, cache.length);
- i++;
- offset = i * MAX_DECRYPT_BLOCK;
- }
- byte[] decryptedData = out.toByteArray();
- out.close();
- return new String(decryptedData, "UTF-8");
- }
- public static String sign(String data, PrivateKey privateKey) throws Exception {
- byte[] keyBytes = privateKey.getEncoded();
- PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- PrivateKey key = keyFactory.generatePrivate(keySpec);
- Signature signature = Signature.getInstance("MD5withRSA");
- signature.initSign(key);
- signature.update(data.getBytes());
- return new String(Base64.encodeBase64(signature.sign()));
- }
- public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception {
- byte[] keyBytes = publicKey.getEncoded();
- X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- PublicKey key = keyFactory.generatePublic(keySpec);
- Signature signature = Signature.getInstance("MD5withRSA");
- signature.initVerify(key);
- signature.update(srcData.getBytes());
- return signature.verify(Base64.decodeBase64(sign.getBytes()));
- }
- public static void main(String[] args) {
- try {
- KeyPair keyPair = getKeyPair();
- String privateKey = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
- String publicKey = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
- System.out.println("私钥:" + privateKey);
- System.out.println("公钥:" + publicKey);
- String data = "待加密的文字内容";
- String encryptData = encrypt(data, getPublicKey(publicKey));
- System.out.println("加密后内容:" + encryptData);
- String decryptData = decrypt(encryptData, getPrivateKey(privateKey));
- System.out.println("解密后内容:" + decryptData);
- String sign = sign(data, getPrivateKey(privateKey));
- boolean result = verify(data, getPublicKey(publicKey), sign);
- System.out.print("验签结果:" + result);
- } catch (Exception e) {
- e.printStackTrace();
- System.out.print("加解密异常");
- }
- }
- }
复制代码
|
|