zoukankan      html  css  js  c++  java
  • openssl生成rsa公私钥对并在java中使用

    rsa著名的非对称加密算法,具体实现我也不是很清楚,大概先要了解一下密码学,有一定基础才能去看的东东,这里就先介绍怎么使用rsa为我们服务。

    首先openssl这是个集成了众多加密算法的工具,它将这一系列的算法整理在一起,是一个伟大的项目。

    openssl genrsa -out private.key 1024

    首先生成私钥,1024是私钥大小,越大越难被破译,同样加密解密所需的时间越长。

    openssl rsa -in private.key -pubout -out public.key

    这个是根据私钥生成公钥。注意私钥在java中使用时,需要通过PCKS#8转换,PCKS是美国RSA数据安全公司还有一堆人指定的密码学标准,其中#8是描述私钥的信息格式。

    openssl pkcs8 -topk8 -nocrypt -in private.key -outform PEM -out java_private.key

    公钥和私钥均可被用来加密解密。

    公钥加密,私钥解密

    私钥加签,公钥验签(这章没讲这个)。

    上代码:

        public static String encryptWithRSA(String msg, String keyPath, boolean isPrivate) {
            try {
                Cipher cipher = Cipher.getInstance("RSA");
                if (isPrivate) {
                    PrivateKey privateKey = loadRSAPrivateKey(keyPath);
                    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
                } else {
                    PublicKey publicKey = loadRSAPublicKey(keyPath);
                    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
                }
                byte[] encryptByteMsg = cipher.doFinal(msg.getBytes("utf-8"));
                String encryptMsg = Base64.getEncoder().encodeToString(encryptByteMsg);
                logger.info("get encryptMsg:[{}]", encryptByteMsg);
                return encryptMsg;
            } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | UnsupportedEncodingException | IllegalBlockSizeException e) {
                logger.error("encrypt error", e);
                throw new FlyException(FrameworkExceptionCode.ENCRYPTERROR.getCode(), FrameworkExceptionCode.ENCRYPTERROR.getMsg());
            }
    
        }
        public static String decryptMsgWithRSA(String encryptMsg, String keyPath, boolean isPrivate) {
            try {
                Cipher cipher = Cipher.getInstance("RSA");
                if (isPrivate) {
                    PrivateKey privateKey = loadRSAPrivateKey(keyPath);
                    cipher.init(Cipher.DECRYPT_MODE, privateKey);
                } else {
                    PublicKey publicKey = loadRSAPublicKey(keyPath);
                    cipher.init(Cipher.DECRYPT_MODE, publicKey);
                }
                byte[] encryptByte = Base64.getDecoder().decode(encryptMsg.getBytes());
                byte[] plainByteMsg = cipher.doFinal(encryptByte);
                return new String(plainByteMsg);
            } catch (NoSuchAlgorithmException |NoSuchPaddingException |InvalidKeyException|BadPaddingException|IllegalBlockSizeException  e) {
                logger.error("decrypt error!",e);
                throw new FlyException(FrameworkExceptionCode.DECRYPTERROR.getCode(),FrameworkExceptionCode.DECRYPTERROR.getMsg());
            }
        }
  • 相关阅读:
    20169221 2016-2017-2 《移动平台应用开发实践》第十一周学习总结
    20169221 2016-2017-2 《移动平台应用与开发》第十周实验总结
    20169201 2016-2017-2 《网络攻防实践》实验三 缓冲区溢出实验
    20169218 2016-2017-2 《网络攻防实践》第九周作业
    2016-2017 《移动平台开发》实验三 敏捷开发与XP实践
    20169221 2016-2017-2 实验一 网络攻防环境的搭建与测试
    实验二《Java面向对象》实验报告
    20169221 2016-2017-2 《移动平台开发》第七周学习总结
    20169221 2016-2017-2 《网络攻防》第七周学习总结
    20169221 2016-2017-2 《移动平台开发》第六周学习总结
  • 原文地址:https://www.cnblogs.com/foreveravalon/p/7095247.html
Copyright © 2011-2022 走看看