zoukankan      html  css  js  c++  java
  • Rsa 加密解密 java

    前言

    RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的。1987年7月首次在美国公布,当时他们三人都在麻省理工学院工作实习。RSA就是他们三人姓氏开头字母拼在一起组成的 [ 百度百科 ]

    开始

    1 .Java中RSA加密解密和.Net 、Php 有点不同,公钥私钥必须使用PKCS8格式,而.Net、Php却不需要。

    2 .加密代码如下,注意其中publicCertificate 是PKCS8格式:

        @Override
        public String rsaEncode(String publicCertificate, String text) {
            try {
                 byte[] publicBytes =baseStrToByte(publicCertificate);
                X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
                KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
                PublicKey pubKey = keyFactory.generatePublic(keySpec);
                try {
                    // get an RSA cipher object and print the provider
                    final Cipher cipher = Cipher.getInstance(ALGORITHM);
                    // encrypt the plain text using the public key
                    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
                    byte[] cipherBytes = cipher.doFinal(text.getBytes(CharSet));
                    String encodestr = baseByteToStr(cipherBytes);
                    return encodestr;
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    3 .解密代码,注意其中privateCertificate是PKCS8格式

       @Override
        public String rsaDecode(String privateCertificate, String text) {
            try {
                byte[] privateBytes = baseStrToByte(privateCertificate);
                PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateBytes);
                KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
                PrivateKey priKey = keyFactory.generatePrivate(keySpec);
                byte[] cipherText = null;
                try {
                    // get an RSA cipher object and print the provider
                    final Cipher cipher = Cipher.getInstance(ALGORITHM);
                    // encrypt the plain text using the public key
                    cipher.init(Cipher.DECRYPT_MODE, priKey);
                    byte[] textbyte = baseStrToByte(text);
                    cipherText = cipher.doFinal(textbyte);
                    String decodestr = new String(cipherText, CharSet);
                    return decodestr;
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    4 .辅助方法

     /**
         * @Description (TODO)
         * @param str
         * @return
         */
        private byte[] baseStrToByte(String str) {
            return Base64.getDecoder().decode(str);
        }
    
        /** 
         * @Description (TODO)
         * @param bytes
         * @return
         */
        private String baseByteToStr(byte[] bytes) {
            return Base64.getEncoder().encodeToString(bytes);
        }

    结束

    附上单元测试图片一张:
    单元测试

  • 相关阅读:
    WeChat-SmallProgram:组件 scroll-view 横向和纵向 案例
    Codeforces Round #277 (Div. 2) D. Valid Sets 树形DP
    Codeforces Round #243 (Div. 2) E. Sereja and Two Sequences DP
    Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp
    Codeforces Round #259 (Div. 2) D. Little Pony and Harmony Chest 状压DP
    Codeforces Round #274 (Div. 2) E. Riding in a Lift DP
    HDOJ 6069 素数筛
    在平面中,一个点绕任意点旋转θ度后的点的坐标
    HDOJ 5724 博弈SG函数
    Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序+贪心
  • 原文地址:https://www.cnblogs.com/alvis/p/9438848.html
Copyright © 2011-2022 走看看