- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在编写一小段代码来读取存储在 .pem 文件中的公钥和私钥。我正在使用以下命令来生成 key 。
下面的命令生成一对 key 。
$openssl genrsa -out mykey.pem 2048
生成私钥的命令
$openssl pkcs8 -topk8 -inform PEM -outform PEM -in mykey.pem \
-out private_key.pem -nocrypt
和这个命令来获取公钥。
$ openssl rsa -in mykey.pem -pubout -outform DER -out public_key.der
我写了两个方法,分别读取私钥和公钥。
public PrivateKey getPemPrivateKey(String filename, String algorithm) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
String temp = new String(keyBytes);
String privKeyPEM = temp.replace("-----BEGIN PRIVATE KEY-----\n", "");
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", "");
//System.out.println("Private key\n"+privKeyPEM);
Base64 b64 = new Base64();
byte [] decoded = b64.decode(privKeyPEM);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePrivate(spec);
}
public PublicKey getPemPublicKey(String filename, String algorithm) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
String temp = new String(keyBytes);
String publicKeyPEM = temp.replace("-----BEGIN PUBLIC KEY-----\n", "");
publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", "");
Base64 b64 = new Base64();
byte [] decoded = b64.decode(publicKeyPEM);
X509EncodedKeySpec spec =
new X509EncodedKeySpec(decoded);
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePublic(spec);
}
我觉得这是一种幼稚的做法。我无法通过互联网获得更好的方法。谁能建议我编写相同代码来处理通用案例的最佳方法是什么。我不想使用任何类型的第三方库。
我对唱歌/加密有非常基本的了解,几乎不使用任何 Java 安全 API。因此,如果我在某处没有意义,请指出。
最佳答案
试试这个类(class)。
package groovy;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
public class RSA {
private static String getKey(String filename) throws IOException {
// Read key from file
String strKeyPEM = "";
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ((line = br.readLine()) != null) {
strKeyPEM += line + "\n";
}
br.close();
return strKeyPEM;
}
public static RSAPrivateKey getPrivateKey(String filename) throws IOException, GeneralSecurityException {
String privateKeyPEM = getKey(filename);
return getPrivateKeyFromString(privateKeyPEM);
}
public static RSAPrivateKey getPrivateKeyFromString(String key) throws IOException, GeneralSecurityException {
String privateKeyPEM = key;
privateKeyPEM = privateKeyPEM.replace("-----BEGIN PRIVATE KEY-----\n", "");
privateKeyPEM = privateKeyPEM.replace("-----END PRIVATE KEY-----", "");
byte[] encoded = Base64.decodeBase64(privateKeyPEM);
KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
RSAPrivateKey privKey = (RSAPrivateKey) kf.generatePrivate(keySpec);
return privKey;
}
public static RSAPublicKey getPublicKey(String filename) throws IOException, GeneralSecurityException {
String publicKeyPEM = getKey(filename);
return getPublicKeyFromString(publicKeyPEM);
}
public static RSAPublicKey getPublicKeyFromString(String key) throws IOException, GeneralSecurityException {
String publicKeyPEM = key;
publicKeyPEM = publicKeyPEM.replace("-----BEGIN PUBLIC KEY-----\n", "");
publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", "");
byte[] encoded = Base64.decodeBase64(publicKeyPEM);
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(new X509EncodedKeySpec(encoded));
return pubKey;
}
public static String sign(PrivateKey privateKey, String message) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException {
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initSign(privateKey);
sign.update(message.getBytes("UTF-8"));
return new String(Base64.encodeBase64(sign.sign()), "UTF-8");
}
public static boolean verify(PublicKey publicKey, String message, String signature) throws SignatureException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initVerify(publicKey);
sign.update(message.getBytes("UTF-8"));
return sign.verify(Base64.decodeBase64(signature.getBytes("UTF-8")));
}
public static String encrypt(String rawText, PublicKey publicKey) throws IOException, GeneralSecurityException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return Base64.encodeBase64String(cipher.doFinal(rawText.getBytes("UTF-8")));
}
public static String decrypt(String cipherText, PrivateKey privateKey) throws IOException, GeneralSecurityException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(cipher.doFinal(Base64.decodeBase64(cipherText)), "UTF-8");
}
}
Required jar library "common-codec-1.6"
关于java - 如何读取 .pem 文件以获取私钥和公钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11787571/
如何从一个 PEM 中确定 SSL 证书到期日期,该 PEM 在一个文件 (pem) 上串联了很多证书? 例子: # cat cert.pem -----BEGIN CERTIFICATE-----
我想让运行测试更容易。为了实现这一点,我想提供项目内部测试中使用的 secret 。 我使用以下命令创建了“fullchain.pem”、“privkey.pem”和“dhparams.pem”: o
注意图片中证书的图标图像上写有标准或个人。有什么不同?最近一次我在处理它,我只制作了带有“个人”的图标。我不知道为什么。有什么不同?两者是如何产生的? 最佳答案 我会这样说,因为您的 .p12 文件包
我正在尝试安装这个项目:https://github.com/versatica/mediasoup-demo 它需要 fullchain.pem和 privkey.pem文件。 如何在 Ubuntu
我正在使用 curl 使用公共(public)证书文件从 https 站点下载数据。 系统信息: 操作系统:软呢帽 14 curl : curl 7.30.0 openssl: OpenSSL 1.0
我正在尝试使用 openssl 加密短文本,但我不知道如何继续。更具体地说:我发现一段代码似乎可以工作,但必须从终端执行指令。是的,我知道,存在 system() 函数,但我想避免它,因为它需要文件夹
我正在使用 OpenSSL 将“me.p12”转换为 PEM。当我生成“me.p12”时,我为其设置了密码。 “me.p12”包含私钥和证书。 当我将其转换为 PEM 时,我运行命令: openssl
我正在使用 Amazon 的 EC2 集群来运行 GraphLab。他们想要我的 .pem 文件的位置,这是我的私钥。 搜索后我仍然在ubuntu中找不到该文件。我正在使用 PuTTY。 最佳答案 .
我正在尝试了解 .pem 文件的布局方式。密码短语是如何验证的? .pem 文件如何检查您尝试打开它时使用的密码? 最佳答案 多种不同类型的数据存储在扩展名为.pem 的文件中。所以你的问题不准确。但
我需要生成一个 cert.pem 和 key.pem 文件来使用 Nginx 加密 http 请求。 在 Linux 上,我会执行以下 OpenSSL 命令: openssl req -x509 -n
我必须以 pem 格式提交 CSR。 我已经使用 OpenSSL 生成了 CSR,但在将其转换为 PEM 格式时遇到了困难。 我必须从 example.csr 获取示例 - csr.pem。如何实现?
我知道这是一个与许多其他问题非常相似的问题,但他们都没有给出直接的答案或对我有用的答案...... 我从 Let's encrypt 获得了两个文件: 证书.pem key.pem 我需要让他们进入
首先,我看过this thread但它不相关并且有不同的问题。 我的 Kafka 属性文件中有以下设置片段: ssl.keystore.type=PEM ssl.keystore.key=/path/
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this q
我想下载 Cpanel 自动生成的 SSL 作为 key.pem 和 cert.pem,我在 Cpanel 中找不到任何下载链接,有什么建议吗? 我曾尝试将证书文本复制到文本文件中并使用它,但没有成功
这个问题在这里已经有了答案: Converting pfx to pem using openssl (5 个答案) 关闭 6 年前。
我想通过 AWS Lambda 使用 pysftp 库连接 EC2。我使用以下代码进行连接。 mysftp = pysftp.Connection( host=Constants.MY_HOS
我有这两个文件(certificate.pem 和private_key.pem),它们由我使用的 API 提供。我需要使用这些文件在 Swift 项目中签署我的 http 请求。 我可以用 Post
我的 Pem 文件就是这种格式。 Bag Attributes localKeyID: friendlyName: test subject=/C=GB/ST=London/L=Soho/O=Righ
我从证书颁发机构获得了这些文件: domain.com.p7b domain.com.crt domain.com.ca-bundle 我尝试了这个小代码: var express = require
我是一名优秀的程序员,十分优秀!