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(" ","+")
    
  • 相关阅读:
    第一次JAVA课,第一次课堂考,课后感受【代码部分】
    第一次JAVA课,第一次课堂考,课后感受
    小学期的开始【9.2进度报告】
    暑假的最后一周【8.26进度报告】
    一周质量报告【8.19进度报告】
    人月神话读后感(一)
    Web版记账本开发记录(二)开发过程遇到的问题小结1 对数据库的区间查询
    Web版记账本开发记录(一)代码和功能展示
    tomcat错误The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    库存物资管理系统
  • 原文地址:https://www.cnblogs.com/caibingxu/p/14253429.html
Copyright © 2011-2022 走看看