KeyContext.java
import lombok.Builder; import lombok.Getter; import lombok.Setter; import java.security.PrivateKey; import java.security.PublicKey; @Getter @Setter @Builder public 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; @Component public 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); } }
相关推荐
可以生成public key和private key
A系统构建密钥:构建一对公私密钥Private Key1和Public Key1; A系统向B系统公布自己的公钥(Public Key1); B系统使用A公布的公钥(Public Key1)建立一对密钥:Private Key2和Public Key2; B系统向A系统公布...
用OpenSSL 生成了公钥和私钥文件,格式为PEM的,现在用C#想要从文件中读取公钥和私钥。 生成公钥和私钥方式如下: DSA公私钥可以使用OpenSSL ...openssl dsa -in dsa_private_key.pem -pubout -out dsa_public_key.pem
方法生成公钥和私钥,公钥文件为publicKey,私钥文件为privateKey。 加密:String miwen = ElGamalCoder.decrypt("这里传入明文", "publicKey"); //publicKey是公钥文件所在的路径(包括文件名) 解密:String ...
本地生成RSA密钥对工具包,运行直接生成,1024和2048位都可以,修改配置文件config.properties中的参数即可,运行后本目录下生成4个文件:privateKey.txt,publicKey.txt,privateKey.pem,publicKey.pem
请勿用于商业用途!请勿用作非法用途!官方版本有30天的免费试用期,请大家支持正版,此版本仅供个人学习交流,对于造成的问题,本作者概不负责! ...privateKey.bytes publicKey.bytes readme.txt
Public Key认证的基础在于一对密钥,public key和private key,public key对数据进行加密而且只能用于加密,private key 只能对所匹配的public key加密过的数据进行解密。
java –jar signapk.jar [-w] publickey.x509[.pem] privatekey.pk8 input.jar output.jar -w 是指对ROM签名时需使用的参数 publickey.x509[.pem] 是公钥文件 privatekey.pk8 是指 私钥文件 input.jar 要签名的...
let (privateKey, publicKey) = try! CC.RSA.generateKeyPair(2048) 将它们转换为PEM格式 let privateKeyPEM = try SwKeyConvert.PrivateKey.derToPKCS1PEM(privateKey) let publicKeyPEM = SwKeyConvert.PublicKey....
String privateKey = map.get("privateKey"); String publicKey= map.get("publicKey"); 生成KEY和ID String str = UUID.randomUUID().toString(); String appId = str.trim().replaceAll("-","").toUpperCase(); ...
ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream("privateKey.dat")); //保存私钥到文件privateKey.dat oout.writeObject(keyPair.getPrivate()); //序列化私钥 oout.close(); //...
PublicKey pubkey = keys.getPublic(); PrivateKey prikey = keys.getPrivate() pubKey = Base64.encodeToByte(pubkey.getEncoded()); priKey = Base64.encodeToByte(prikey.getEncoded()); System.out....
纹波密钥对 使用的XRP分类帐密钥对和钱包生成的实现,它... deriveKeypair(seed: string) -> {privateKey: string, publicKey: string} 从种子派生公钥和私钥。 密钥以33字节的十六进制字符串表示。 sign(messageH
docker run --rm -i masipcat/wireguard-go wg pubkey < privatekey> publickey 运行服务器 码头工人 docker-compose.yaml version : ' 3.3 ' services : wireguard : image : masipcat/wireguard-go:latest cap...
安装完毕配置一下环境变量。...C:\OpenSSL-Win32\bin>openssl.exe (进入 OpenSSL 程序) ...OpenSSL> rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem (生成公钥) OpenSSL> exit (退出 OpenSSL 程序)
1: 如何通过 openssl 命令获得 public key, private key并存入文件 2: 如何通过文件读取RSA的 public/private key。 3: 如何用public key加密,然后用private key解密。 4: 如何生成和运行示例代码。 网上关于RSA的...
本工具是用于golang编写的,用于rsa非对称加密技术实现的对字符串的加密解密工具,可以对文件进行加密解密(txt、docx、xls文档等)。...2.选择相应的publickey.pem进行加密 3.选择相应的privatekey.pem进行文档解密
IOS RSA公私钥生成
Generate the pair of public key and private key. The RSA implementation is included.
token 生成文件,用于前后端数据传输时发送的密钥(暗语)。 直接解压后引用该文件即可 -- 后端在收到第一次请求的时候调用 私有的(p开头的)...- openssl rsa -in private.key -pubout -outform PEM -out public.key