zoukankan      html  css  js  c++  java
  • RSA加密、解密、签名、验签的原理及方法

    一、RSA加密简介

      RSA加密是一种非对称加密。可以在不直接传递密钥的情况下,完成解密。这能够确保信息的安全性,避免了直接传递密钥所造成的被破解的风险。是由一对密钥来进行加解密的过程,分别称为公钥和私钥。两者之间有数学相关,该加密算法的原理就是对一极大整数做因数分解的困难性来保证安全性。通常个人保存私钥,公钥是公开的(可能同时多人持有)。

    二、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回复的。

      在这个过程中,只有2次传递过程,第一次是A传递加签的消息和消息本身给B,第二次是B获取A的公钥,即使都被敌方截获,也没有危险性,因为只有A的私钥才能对消息进行签名,即使知道了消息内容,也无法伪造带签名的回复给B,防止了消息内容的篡改。

      但是,综合两个场景你会发现,第一个场景虽然被截获的消息没有泄露,但是可以利用截获的公钥,将假指令进行加密,然后传递给A。第二个场景虽然截获的消息不能被篡改,但是消息的内容可以利用公钥验签来获得,并不能防止泄露。所以在实际应用中,要根据情况使用,也可以同时使用加密和签名,比如A和B都有一套自己的公钥和私钥,当A要给B发送消息时,先用B的公钥对消息加密,再对加密的消息使用A的私钥加签名,达到既不泄露也不被篡改,更能保证消息的安全性。

      总结:公钥加密、私钥解密;私钥签名、公钥验签。

    三、RSA加密、签名的方法,代码例子如下:

    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 {
    
        /**
         * RSA最大加密明文大小
         */
        private static final int MAX_ENCRYPT_BLOCK = 117;
    
        /**
         * RSA最大解密密文大小
         */
        private static final int MAX_DECRYPT_BLOCK = 128;
    
        /**
         * 获取密钥对
         *
         * @return 密钥对
         */
        public static KeyPair getKeyPair() throws Exception {
            KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
            generator.initialize(1024);
            return generator.generateKeyPair();
        }
    
        /**
         * 获取私钥
         *
         * @param privateKey 私钥字符串
         * @return
         */
        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);
        }
    
        /**
         * 获取公钥
         *
         * @param publicKey 公钥字符串
         * @return
         */
        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);
        }
    
        /**
         * RSA加密
         *
         * @param data 待加密数据
         * @param publicKey 公钥
         * @return
         */
        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();
            // 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
            // 加密后的字符串
            return new String(Base64.encodeBase64String(encryptedData));
        }
    
        /**
         * RSA解密
         *
         * @param data 待解密数据
         * @param privateKey 私钥
         * @return
         */
        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");
        }
    
        /**
         * 私钥加密(未考虑明文长度超过限制的情况)
         * @param plain_text 明文
         * @param privateStr 私钥
         * @return
         */
        public static String enWithRSAPrivateKey(String plain_text,String privateStr) {
            try {
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.ENCRYPT_MODE, getPrivateKey(privateStr));
                return byte2hex(cipher.doFinal(plain_text.getBytes()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 公钥解密(未考虑明文长度超过限制的情况)
         * @param plain_text 密文
         * @param publicStr 公钥
         * @return
         */
        public static String deWithRSAPublicKey(String plain_text,String publicStr) {
            try {
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.DECRYPT_MODE, getPublicKey(publicStr));
                byte[] plainText = cipher.doFinal(hex2byte(plain_text));
                return new String(plainText);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * MD5withRSA签名
         *
         * @param data 待签名数据
         * @param privateKey 私钥
         * @return 签名
         */
        public static String signMD5withRSA(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()));
        }
    
        /**
         * signVerifyMD5withRSA数字签名验证.
         *
         * @param srcData 原始字符串
         * @param publicKey 公钥
         * @param sign 签名
         * @return 是否验签通过
         */
        public static boolean signVerifyMD5withRSA(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()));
        }
    
    
        /**
         * SHA256withRSA签名
         *
         * @param privateKeyStr
         *            私钥
         * @param plain_text
         *            明文
         * @return
         */
        public static String signSha256withRSA(String plain_text,String privateKeyStr) {
            try {
                Signature Sign = Signature.getInstance("SHA256withRSA");
                PrivateKey privateKey = getPrivateKey(privateKeyStr);
                Sign.initSign(privateKey);
                Sign.update(plain_text.getBytes());
                byte[] signed = Sign.sign();
                return byte2hex(signed) ;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
    
        /**
         * SHA1WithRSA数字签名验证.
         *
         * @param data   数据
         * @param strHexPublicKey    公钥
         * @param sign   签名
         * @return true, if successful
         * @throws Exception the exception
         */
        public static boolean signVerifySHA256WithRSA(String data, String strHexPublicKey, String sign)
                throws Exception {
            try {
                PublicKey rsaPublicKey = getPublicKey(strHexPublicKey);
                //公钥解签
                Signature sig = Signature.getInstance("SHA256withRSA");
                sig.initVerify(rsaPublicKey);
                sig.update(data.getBytes());
                return sig.verify(hex2byte(sign));
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * Description:将二进制转换成16进制字符串
         *
         * @param b
         * @return
         * @return String
         * @author name:
         */
        public static String byte2hex(byte[] b) {
            String hs = "";
            String stmp = "";
            for (int n = 0; n < b.length; n++) {
                stmp = (Integer.toHexString(b[n] & 0XFF));
                if (stmp.length() == 1) {
                    hs = hs + "0" + stmp;
                } else {
                    hs = hs + stmp;
                }
            }
            return hs.toUpperCase();
        }
    
        /**
         * Description:将16进制字符串转换成二进制
         * @return
         * @return String
         * @author name:
         */
        public static byte[] hex2byte(String hexString) {
            if (hexString == null || "".equals(hexString)) {
                return null;
            }
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];
            for (int i = 0; i < length; i++) {
                int pos = i * 2;
                d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
            }
            return d;
        }
    
        /**
         * Convert char to byte
         * @param c char
         * @return byte
         */
        private static byte charToByte(char c) {
            return (byte) "0123456789ABCDEF".indexOf(c);
        }
    
        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 = "张三丰";
    
                // RSA公钥加密
                String encryptData = encrypt(data, getPublicKey(publicKey));
                System.out.println("公钥加密后内容:" + encryptData);
                // RSA私钥解密
                String decryptData = decrypt(encryptData, getPrivateKey(privateKey));
                System.out.println("私钥解密后内容:" + decryptData);
    
                // RSA私钥加密
                String encryptData1 = enWithRSAPrivateKey(data, privateKey);
                System.out.println("私钥加密后内容:" + encryptData1);
                // RSA公钥解密
                String decryptData1 = deWithRSAPublicKey(encryptData1, publicKey);
                System.out.println("公钥解密后内容:" + decryptData1);
    
                // RSA签名(MD5withRSA)
                String sign = signMD5withRSA(data, getPrivateKey(privateKey));
                // RSA验签(MD5withRSA)
                boolean result = signVerifyMD5withRSA(data, getPublicKey(publicKey), sign);
                System.out.println("MD5withRSA验签结果:" + result);
    
                // RSA签名(SHA256withRSA)
                String sign1 = signSha256withRSA(data, privateKey);
                // RSA验签(SHA256withRSA)
                boolean result1 = signVerifySHA256WithRSA(data, publicKey, sign1);
                System.out.println("SHA256withRSA验签结果:" + result);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.print("加解密异常");
            }
        }
    }
    

    PS:RSA加密对明文的长度有所限制,规定需加密的明文最大长度=密钥长度-11(单位是字节,即byte),所以在加密和解密的过程中需要分块进行。而密钥默认是1024位,即1024位/8位-11=128-11=117字节。所以默认加密前的明文最大长度117字节,解密密文最大长度为128字。那么为啥两者相差11字节呢?是因为RSA加密使用到了填充模式(padding),即内容不足117字节时会自动填满,用到填充模式自然会占用一定的字节,而且这部分字节也是参与加密的。

      密钥长度的设置就是上面例子的第32行。可自行调整,当然非对称加密随着密钥变长,安全性上升的同时性能也会有所下降。

    本文转载自:https://www.cnblogs.com/pcheng/p/9629621.html

  • 相关阅读:
    POJ1486 Sorting Slides 二分图or贪心
    POJ2060 Taxi Cab Scheme 最小路径覆盖
    POJ3083 Children of the Candy Corn 解题报告
    以前的文章
    POJ2449 Remmarguts' Date K短路经典题
    这一年的acm路
    POJ3014 Asteroids 最小点覆盖
    POJ2594 Treasure Exploration 最小路径覆盖
    POJ3009 Curling 2.0 解题报告
    POJ2226 Muddy Fields 最小点集覆盖
  • 原文地址:https://www.cnblogs.com/zhangww/p/10846216.html
Copyright © 2011-2022 走看看