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/private key让putty(ssh)自动登录”指的是通过SSH(Secure Shell)协议,使用公钥/私钥对实现Putty的无密码自动登录。这是一项提高远程服务器访问安全性和便利性的技术。 在SSH连接中,通常有...
可以生成public key和private key
Console.WriteLine("Private Key: " + privateKey); // 加密数据 byte[] dataToEncrypt = File.ReadAllBytes("plaintext.txt"); byte[] encryptedData = rsa.Encrypt(dataToEncrypt, false); // 保存加密数据...
在这个过程中,private key(私钥)扮演着至关重要的角色,它用于解密由public key(公钥)加密的数据,确保只有预期的接收者才能读取信息。以下是对"模拟private key加密算法的SSL通信"这一主题的详细解释: 1. **...
- `saveKeys(PublicKey publicKey, PrivateKey privateKey, String fileName)`: 保存公钥和私钥到文件。 - `loadKeys(String publicKeyFile, String privateKeyFile)`: 从文件加载公钥和私钥。 7. **安全性考虑**...
5. **加载和使用密钥**:在需要时,可以反编码并解析回PublicKey和PrivateKey对象。 ```java byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyString); byte[] privateKeyBytes = Base64.getDecoder()...
A系统构建密钥:构建一对公私密钥Private Key1和Public Key1; A系统向B系统公布自己的公钥(Public Key1); B系统使用A公布的公钥(Public Key1)建立一对密钥:Private Key2和Public Key2; B系统向A系统公布...
publicKey := &privateKey.PublicKey publicPEM := pem.EncodeToMemory(&pem.Block{ Type: "RSA PUBLIC KEY", Bytes: x509.MarshalPKCS1PublicKey(publicKey), }) // 保存到文件 err = ioutil.WriteFile(...
在使用支付宝进行支付接口开发时,开发者需要生成一对密钥——私钥(Private Key)和公钥(Public Key),这就是“支付宝生成私钥公钥工具”所涉及的核心概念。 私钥是用于解密和签名的密钥,必须严格保密,只有...
用OpenSSL 生成了公钥和私钥文件,格式为PEM的,现在用C#想要从文件中读取公钥和私钥。 生成公钥和私钥方式如下: DSA公私钥可以使用OpenSSL ...openssl dsa -in dsa_private_key.pem -pubout -out dsa_public_key.pem
publicKey = privateKey.GetPublic(); ``` 3. **序列号生成**:序列号可以是任何与产品相关的唯一标识,如产品ID加上时间戳。将其转换为字节数组,然后使用私钥对其进行签名。 ```cpp std::string serialNumber = ...
本文将深入探讨如何模仿JSON,根据对象生成JSON字符串,以及如何根据JSON字符串反向生成对象。 首先,我们要理解JSON的基本结构。一个JSON对象以大括号`{}`包围,其内部由键值对组成,键用双引号`""`包裹,值可以是...
这是一个低级库,用于生成和序列化X25519(Curve25519)密钥对(使用nacl.box )。 另请参阅(相关规格):安装需要Node.js 12+ 要在本地安装(用于开发): git clone ...
方法生成公钥和私钥,公钥文件为publicKey,私钥文件为privateKey。 加密:String miwen = ElGamalCoder.decrypt("这里传入明文", "publicKey"); //publicKey是公钥文件所在的路径(包括文件名) 解密:String ...
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); System.out.println("RSA Public Key (Base64 encoded): " + Base64.get...
public void saveKeys(PublicKey publicKey, PrivateKey privateKey, String publicKeyPath, String privateKeyPath) { try { FileOutputStream publicKeyOut = new FileOutputStream(publicKeyPath); ...
上述代码会生成一个2048位的RSA密钥对,私钥存储在$privateKey变量中,公钥存储在$publicKey变量中。 数字签名和验证: 数字签名是一种用于验证数据完整性和发送者身份的技术。在PHP中,我们可以使用私钥对数据进行...
PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // 存储到keystore KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null, ...
本地生成RSA密钥对工具包,运行直接生成,1024和2048位都可以,修改配置文件config.properties中的参数即可,运行后本目录下生成4个文件:privateKey.txt,publicKey.txt,privateKey.pem,publicKey.pem
请勿用于商业用途!请勿用作非法用途!官方版本有30天的免费试用期,请大家支持正版,此版本仅供个人学习交流,对于造成的问题,本作者概不负责! ...privateKey.bytes publicKey.bytes readme.txt