zoukankan      html  css  js  c++  java
  • RSA加密 基于Base64

    gradle 添加依赖 compile group: 'commons-codec', name: 'commons-codec', version: '1.15'

    /**
     * RSA加解密工具类,实现公钥加密私钥解密和私钥解密公钥解密
     */
    public class RsaUtils {
        /**
         * 公钥解密
         *
         * @param publicKeyText
         * @param text
         * @return
         * @throws Exception
         */
        public static String decryptByPublicKey(String publicKeyText, String text) throws Exception {
            X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            byte[] result = cipher.doFinal(Base64.decodeBase64(text));
            return new String(result);
        }
    
        /**
         * 私钥加密
         *
         * @param privateKeyText
         * @param text
         * @return
         * @throws Exception
         */
        public static String encryptByPrivateKey(String privateKeyText, String text) throws Exception {
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            byte[] result = cipher.doFinal(text.getBytes());
            return Base64.encodeBase64String(result);
        }
    
        /**
         * 私钥解密
         *
         * @param privateKeyText
         * @param text
         * @return
         * @throws Exception
         */
        public static String decryptByPrivateKey(String privateKeyText, String text) throws Exception {
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] result = cipher.doFinal(Base64.decodeBase64(text));
            return new String(result);
        }
    
        /**
         * 公钥加密
         *
         * @param publicKeyText
         * @param text
         * @return
         */
        public static String encryptByPublicKey(String publicKeyText, String text) throws Exception {
            X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] result = cipher.doFinal(text.getBytes());
            return Base64.encodeBase64String(result);
        }
    
        /**
         * 构建RSA密钥对
         *
         * @return
         * @throws NoSuchAlgorithmException
         */
        public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(2048);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
            RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
            String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
            String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
            return new RsaKeyPair(publicKeyString, privateKeyString);
        }
    }
    
    public class RsaKeyPair {
        private String publicKey;
        private String privateKey;
    
        public RsaKeyPair(String publicKey, String privateKey) {
            this.publicKey = publicKey;
            this.privateKey = privateKey;
        }
    
        public String getPublicKey() {
            return publicKey;
        }
    
        public String getPrivateKey() {
            return privateKey;
        }
    }
    

    注意由于url传输过程中可能导致字符变换,从而导致出错,可以参考下 https://blog.csdn.net/q840202933/article/details/71627949

    replaceAll(" ","+")
    
  • 相关阅读:
    解决ssh: connect to host github.com port 22: Connection refused
    sudo/su命令免密执行
    Linux tar.gz、tar、bz2、zip 等解压缩、压缩命令详解
    项目实战——校验文件名和邮箱地址
    项目实战:判断闰年平年并输出某月的天数
    ubuntu软件更新时出现没有Release文件
    项目实战:根据出生日期判断星座
    项目实战--实现淡旺季飞机票打折
    xxx.java:1: 错误: 类xxx是公共的, 应在名为 xxx.java 的文件中声明 public class xxx
    32位int整数范围
  • 原文地址:https://www.cnblogs.com/caibingxu/p/14253429.html
Copyright © 2011-2022 走看看