zoukankan      html  css  js  c++  java
  • java 非对称加密RSA

    package com.aarony.test;
    
    import java.io.IOException;
    import java.security.KeyFactory;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    
    import javax.crypto.Cipher;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    public class EncryptionRSADemo2 {
    
        /**
         * 
         * 此方法描述的是:解密密匙
         * 
         * @author: Aarony
         * @version: 2018年6月20日 下午9:44:38
         */
        public static byte[] privateEncrypt(byte[] bytes, PrivateKey privateKey) throws Exception {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            return cipher.doFinal(bytes);
        }
    
        /**
         * 
         * 此方法描述的是:加密
         * 
         * @author: Aarony
         * @version: 2018年6月20日 下午9:44:47
         */
        public static byte[] publicEncrypt(byte[] bytes, PublicKey publicKey) throws Exception {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            return cipher.doFinal(bytes);
        }
    
        /**
         * 
         * 此方法描述的是:解密base64 位的密钥
         * 
         * @author: Aarony
         * @version: 2018年6月20日 下午9:40:51
         */
        public static PrivateKey string2PrivateKey(String privateStr) throws Exception {
            byte[] bytes = base642byte(privateStr);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            return keyFactory.generatePrivate(keySpec);
        }
    
        /**
         * 
         * 此方法描述的是:解密base64 位的公钥
         * 
         * @author: Aarony
         * @version: 2018年6月20日 下午9:40:51
         */
        public static PublicKey string2PublicKey(String pubStr) throws Exception {
            byte[] bytes = base642byte(pubStr);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            return keyFactory.generatePublic(keySpec);
        }
    
        /**
         * 
         * 此方法描述的是:生成keypair
         * 
         * @author: Aarony
         * @version: 2018年6月20日 下午9:35:43
         * @throws NoSuchAlgorithmException
         */
        public static KeyPair getKeyPair() throws NoSuchAlgorithmException {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(512);
            return keyPairGenerator.generateKeyPair();
        }
    
        /**
         * 
         * 此方法描述的是:获取公钥
         * 
         * @author: Aarony
         * @version: 2018年6月20日 下午9:37:13
         */
        public static String getPublicKey(KeyPair keyPair) {
            PublicKey key = keyPair.getPublic();
            return byte2base64(key.getEncoded());
        }
    
        /**
         * 
         * 此方法描述的是:获取公钥
         * 
         * @author: Aarony
         * @version: 2018年6月20日 下午9:37:13
         */
        public static String getPrivateKey(KeyPair keyPair) {
            PrivateKey key = keyPair.getPrivate();
            return byte2base64(key.getEncoded());
        }
    
        /**
         * 
         * 此方法描述的是:base64 解码
         * 
         * @author: Aarony
         * @version: 2018年6月20日 下午9:16:57
         */
        public static byte[] base642byte(String base64) throws IOException {
            BASE64Decoder decoder = new BASE64Decoder();
            return decoder.decodeBuffer(base64);
        }
    
        /**
         * 
         * 此方法描述的是: base 64编码
         * 
         * @author: Aarony
         * @version: 2018年6月20日 下午9:15:14
         */
        public static String byte2base64(byte[] bytes) {
            BASE64Encoder base = new BASE64Encoder();
            return base.encode(bytes);
        }
    }
  • 相关阅读:
    libevent源码学习之event
    游戏寻路A*算法
    游戏地图动态生成
    一个基于protocol buffer的RPC实现
    TCMalloc源码学习(四)(小内存块释放)
    TCMalloc源码学习(三)(小块内存分配)
    TCMalloc源码学习(二)
    第五十四篇 Linux相关——远程连接SSH
    第五十三篇 Linux相关——Web服务器
    第五十二篇 Linux相关——数据库服务MySQL
  • 原文地址:https://www.cnblogs.com/wucaifang/p/9206261.html
Copyright © 2011-2022 走看看