博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
根据PrivateKey生成 PublicKey
阅读量:5954 次
发布时间:2019-06-19

本文共 4920 字,大约阅读时间需要 16 分钟。

  hot3.png

KeyContext.java

 

import lombok.Builder;import lombok.Getter;import lombok.Setter;import java.security.PrivateKey;import java.security.PublicKey;@Getter@Setter@Builderpublic class KeyContext {    private String privateKeyStr;    private PrivateKey privateKey;    private String publicKeyStr;    private PublicKey publicKey;}

 

KeyUtil.java

 

import org.bouncycastle.jce.provider.BouncyCastleProvider;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import javax.crypto.Cipher;import java.security.KeyFactory;import java.security.PrivateKey;import java.security.PublicKey;import java.security.interfaces.RSAPrivateCrtKey;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.RSAPublicKeySpec;import java.util.Base64;@Componentpublic class KeyUtil {    private static final Logger logger = LoggerFactory.getLogger(KeyUtil.class);    private static BouncyCastleProvider bouncyCastleProvider = new BouncyCastleProvider();      public KeyContext genPublicKey(String privateKeyString) {        logger.info("Origin privateKey: {}", privateKeyString);        KeyContext.KeyContextBuilder builder = KeyContext.builder();        try {            byte[] decoded = Base64.getDecoder().decode(privateKeyString);            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decoded);            KeyFactory kf = KeyFactory.getInstance("RSA");            PrivateKey privateKey = kf.generatePrivate(keySpec);            RSAPrivateCrtKey privk = (RSAPrivateCrtKey) privateKey;            RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(privk.getModulus(), privk.getPublicExponent());            KeyFactory keyFactory = KeyFactory.getInstance("RSA");            PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);            String publicKeyStr = Base64.getEncoder().encodeToString(publicKey.getEncoded());            logger.info("Gen publicKey: {}", publicKeyStr);            builder.privateKeyStr(privateKeyString).privateKey(privateKey).publicKeyStr(publicKeyStr).publicKey(publicKey);        } catch (Exception e) {            logger.error("genPublicKey Error:", e);        }        return builder.build();    }    public String encrypt(PublicKey publicKey, String context) {        if (publicKey == null) {            logger.error("KeyUtil.encrypt--context:{}, publicKey is null", context);            return context;        }        try {            Cipher cipher = Cipher.getInstance("RSA", bouncyCastleProvider);            cipher.init(Cipher.ENCRYPT_MODE, publicKey);            byte[] output = cipher.doFinal(context.getBytes());            return Base64.getEncoder().encodeToString(output);        } catch (Exception e) {            logger.error("KeyUtil.encrypt Exception:", e);            return context;        }    }    public String decrypt(PrivateKey privateKey, String context) {        if (privateKey == null) {            logger.error("KeyUtil.decrypt--context:{}, privateKey is null", context);            return context;        }        try {            Cipher cipher = Cipher.getInstance("RSA", bouncyCastleProvider);            cipher.init(Cipher.DECRYPT_MODE, privateKey);            byte[] output = cipher.doFinal(Base64.getDecoder().decode(context));            return new String(output);        } catch (Exception e) {            logger.error("KeyUtil.decrypt Exception:", e);            return context;        }    }    public static void main(String[] args) {        KeyUtil keyUtil = new KeyUtil();        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKKUer0JPGmT6Q3KnpDS5nJO8eSMPBVNwrixd1s4bAYACGFxnzg1xT+TvhmLqxWfj0gX8w2dGKP1PGgwd3F69mTsC+Yn6pAqQOETgn7JqdFHp01YCV5beK0GC7Ev7QSHTVy7PeH3J5ppuOEEd+wOsoP9GztArjgMJfvBA01BL7DxAgMBAAECgYBjQRZ1lc/l/MDJBKwtajP6ESwoBV0g/Goma2GctSNtvlNfeghkPp9/IulpmxUFjHXi44wlAYVrg2oviXdCNnl5RiWUgNfKntoQ96MVZAaOY4HKD06sPZOM2jG8sAGcOk656FeL8KYlad7Kbk/a/Elbujdp3yHWuEgnpT9E7WX2gQJBANYqkYN4mmP5a7p0mMmfjEJmo/IHHFYO3vKcHhoHdOnOi5MOMoY5sE5hNGQAINRKDgfSZ8q+/C43paDYWZrYcPkCQQDCVlPfMYGJfWx0u8MSEja54NeP87k/UfY4m1V/BUHWN62gUDaF3Uc6oRz9DQwlOiYo9qT3Zs7gNAc8uXFplrW5AkEAznlFxrlsJ3xctusYLjIqmA26e2kNkY5OtRl8D94mgg8GEyV54lwVtMsUJmDVRbWLp1DbjeTo3Wn6vYI3iQioiQJAaLtLchJlBCrC41o5M6j7M0t4AI1RvU03i6Qy/ERiCcdx296+s3/gHjmrvLhmXj2rSRI7L1WJkgyYBeLOux/MiQJAUgk4Oml7T//xhhb+E3A2BBKkhnMuHHp18jHw31EqAzCQtzrcZg+cA4woISsH18sqo/iy8qeW00WCIVizVVdoRg==";        KeyContext keyContext = keyUtil.genPublicKey(privateKey);        String originStr = "thisisatest";        String encryptStr = keyUtil.encrypt(keyContext.getPublicKey(), originStr);        String decryptStr = keyUtil.decrypt(keyContext.getPrivateKey(), encryptStr);        System.out.println("~~~~~~~~~~originStr : " + originStr);        System.out.println("~~~~~~~~~~encryptStr : " + encryptStr);        System.out.println("~~~~~~~~~~decryptStr : " + decryptStr);    }}

转载于:https://my.oschina.net/buddie/blog/3019171

你可能感兴趣的文章
RHEL4.4安装YUM
查看>>
【几种UML关系】
查看>>
php开发工具 zend studio 12.5.1 中文版汉化
查看>>
Adobe Flash地图控件AnyMap
查看>>
selenium2 webdriver要点理解
查看>>
LAMP兄弟连打造免费视频教程
查看>>
Android 多模块项目入门
查看>>
使用iSCSI搭建IP SAN存储网络
查看>>
Bean的自动装配Autowiring
查看>>
ASP.NET Core 使用 Hangfire 定时任务
查看>>
mongoDB Error:not master and slaveOk=false
查看>>
python的服务
查看>>
Apache优化
查看>>
Linux入门之Linux与Windows常见对比
查看>>
如何创建支持不同屏幕尺寸的Android应用(转载)
查看>>
python学习之面向对象学习进阶
查看>>
python学习之网络编程
查看>>
64位Java开发平台的选择,如何区分JDK,Tomcat,eclipse的32位与64版本
查看>>
谈Win32汇编
查看>>
sqlserver_identity
查看>>