zoukankan      html  css  js  c++  java
  • rsa加密

    pom:

    commons-codec
    commons-codec
    1.10

    import org.apache.commons.codec.binary.Base64;
    import javax.crypto.Cipher;
    import java.security.*;
    import java.security.interfaces.RSAPrivateKey;
    import java.security.interfaces.RSAPublicKey;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    import java.util.HashMap;
    import java.util.Map;

    public class RSAUtil {

    public static void main(String[] args) {
        Map<String, String> keyPair = RSAUtil.generateKeyPair();
        String publicKey = keyPair.get("publicKey");
        String privateKey = keyPair.get("privateKey");
        String string = "啦啦啦11";
        String encrypt = RSAUtil.encrypt(string, publicKey);
        String decryptedString = RSAUtil.decrypt(encrypt, privateKey);
        System.out.println("publicKey:"+publicKey);
        System.out.println("privateKey:"+privateKey);
        System.out.println("string:"+string);
        System.out.println("decryptedString:"+decryptedString);
    }
    
    /**
     * 生成秘钥对
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static Map<String, String> generateKeyPair(){
        KeyPairGenerator keyPairGen = null;
        try {
            keyPairGen = KeyPairGenerator.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        keyPairGen.initialize(1024,new SecureRandom());
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
        RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        String publicKey = new String(Base64.encodeBase64(rsaPublicKey.getEncoded()));
        String privateKey = new String(Base64.encodeBase64((rsaPrivateKey.getEncoded())));
        Map<String, String> keyMap = new HashMap<>();
        keyMap.put("publicKey", publicKey);
        keyMap.put("privateKey", privateKey);
        return keyMap;
    }
    
    /**
     * 加密
     * @param string
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static String encrypt(String string, String publicKey){
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = null;
        try {
            pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
            Cipher cipher = null;
            cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            return Base64.encodeBase64String(cipher.doFinal(string.getBytes("UTF-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    
    /**
     *
     * @param string
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static String decrypt(String string, String privateKey){
        try {
            RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)));
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey);
            return new String(cipher.doFinal(Base64.decodeBase64(string)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    

    }

  • 相关阅读:
    加解密的使用工具总结
    Java Base64编码解码实现
    Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数
    关于时区的时间的详解,比如UTCGMT等
    JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
    HTTP请求报文和HTTP响应报文
    一名全栈工程师Node.js之路-转
    使用zlib模块实现HTTP服务端与客户端实现传输数据压缩
    为什么要搭建自己的缓存管理模块?
    js 跨域问题常见的五种解决方式
  • 原文地址:https://www.cnblogs.com/xiaohan970121/p/11686234.html
Copyright © 2011-2022 走看看