zoukankan      html  css  js  c++  java
  • RSA加解密-Java

    import java.security.KeyFactory;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.SecureRandom;
    import java.security.interfaces.RSAPrivateKey;
    import java.security.interfaces.RSAPublicKey;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    import javax.crypto.Cipher;
    import org.apache.commons.codec.binary.Base64;
    
    /**
     * @Author: kylin
     * @Date: 2019/3/27 下午3:25
     */
    public class RSAUtil {
    
      /**
       * 随机生成密钥对
       */
      public static void genKeyPair() throws NoSuchAlgorithmException {
        // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        keyPairGen.initialize(1024, new SecureRandom());
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
        String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
      }
    
      /**
       * 公钥加密
       */
      public static String encrypt(String str, String publicKey) throws Exception {
        //base64编码的公钥
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA")
            .generatePublic(new X509EncodedKeySpec(decoded));
        //RSA加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
        return outStr;
      }
    
      /**
       * 私钥解密
       */
      public static String decrypt(String str, String privateKey) throws Exception {
        //64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
        //base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA")
            .generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outStr = new String(cipher.doFinal(inputByte));
        return outStr;
      }
    
    }

    分别是生成密钥对、加密以及解密

  • 相关阅读:
    Android MP3录音实现
    Android应用中MVP开发模式
    ExpandableListView getChildView 不执行,不显示子列表
    Javascript中apply、call、bind
    baidu地图:实现多点连线渲染
    经纬度纠偏的一些经验
    Spark:spark df插入hive表后小文件数量多,如何合并?
    Java-Maven(七):Eclipse中Maven依赖、聚合、继承特性
    Java-Maven(六):Eclipse中Maven插件的命令操作
    Java-Maven(五):Eclipse&Maven下创建java工程&web工程
  • 原文地址:https://www.cnblogs.com/yishilin/p/10608363.html
Copyright © 2011-2022 走看看