我知道非对称加密是如何工作的。我知道有两把钥匙(一把私人钥匙和一把公共钥匙)。
当有人想要通信时,他们用这些公钥交换他们的公钥加密消息,然后相应的消息只能由拥有私钥的用户解密。
现在,我正在使用 Node.js,我需要做这样的事情......
我需要一个应用程序,每个小时读取数据库,提取数据并将其保存到我需要发送到另一台服务器的文件中。
我的问题是我不希望该文件对其他人可见,我使用 SSH 进行传输,因此没有问题,但我必须加密该文件,因为我不是该服务器的管理员,所以也许有人可以阅读它. 不幸的是,两台服务器的管理员都是一样的。所以我的想法是用公钥加密文件,然后只有拥有私钥的人(我)才能解密它。
我认为使用以下内容毫无意义:
var key = 'blablabla'
Run Code Online (Sandbox Code Playgroud)
如果我使用公钥,没有问题,所有人都可以阅读它......它确实是公共的。但是有了这个公钥,没有人可以解密消息,所以它有点像单向加密。
现在,有人可以告诉我是否需要签名者/验证者来完成这项工作,或者我可能必须使用 openssl 生成两个密钥(公共/私有)并将这些密钥传递给密码/解密器?
我在看加密模块,但没有例子......
除了使用浏览器之外,如何获取使用 https 协议的网站的公钥?还有其他方法吗?就像从终端一样。
更新 - 我想要所有的方法,以便我可以选择最适合我的方法
嗨,我需要从 postgresql 数据库中存储和读取公钥,我正在使用 java。
我有这个代码要存储
cbd.ejecutarSeguro("update persona set llave_publica = ? where cedula = 112345678", x509EncodedKeySpec.getEncoded());
public void ejecutarSeguro(String query, byte[] pkBytes) {
try {
Connection conn = DriverManager.getConnection(nombreConexion);
PreparedStatement pstat;
pstat = conn.prepareStatement(query);
pstat.setBytes(1, pkBytes);
pstat.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
阅读我用这个
String filePublicKey_s = cbd.consultarArray("Select llave_publica from persona where cedula = 112345678").get(0).get(0);
byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
//encodedPublicKey = (filePublicKey_s).substring(2).getBytes();
System.out.println("Public Key; "+filePublicKey_s);
System.out.println("rescato de la base; "+(filePublicKey_s).getBytes());// LA …Run Code Online (Sandbox Code Playgroud) 我这样做是为了通过写出 pem 文件来处理 pfx 证书。
我怎样才能取出public_key?我注意到 p12 对象没有 get_publickey() 方法。
import contextlib
import OpenSSL.crypto
import os
import requests
import ssl
import tempfile
pfx_password = 'thiscertpassword'
tpem = 'temppem.pem'
pfxfile = 'fts.pfx'
f_pem = open(tpem, 'wb')
pfx = open(pfxfile, 'rb').read()
p12 = OpenSSL.crypto.load_pkcs12(pfx, pfx_password)
f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM,
p12.get_privatekey()))
#f_pem.write(OpenSSL.crypto.dump_publickey(OpenSSL.crypto.FILETYPE_PEM,
p12.get_publickey())) # NO SUCH METHOD
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM,
p12.get_certificate()))
Run Code Online (Sandbox Code Playgroud) 一些库要求公钥由整数模数和指数表示。将证书或公钥快速转换为该格式非常困难。如何使用 bash 转换它?
我经历了许多类似的话题,但没有运气!!
我想使用 PEM 文件生成公钥和私钥。以下是我使用的代码:
String pemFileNme = "C:\\Users\\amitmm\\Desktop\\clean\\key.pem";
File pubKeyFile = new File(pemFileNme);
File privKeyFile = new File(pemFileNme);
// read public key DER file
DataInputStream dis = new DataInputStream(new
FileInputStream(pubKeyFile));
byte[] pubKeyBytes = new byte[(int)pubKeyFile.length()];
dis.readFully(pubKeyBytes);
dis.close();
// read private key DER file
dis = new DataInputStream(new FileInputStream(privKeyFile));
byte[] privKeyBytes = new byte[(int)privKeyFile.length()];
dis.read(privKeyBytes);
dis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
// decode public key
X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubKeyBytes);
RSAPublicKey pubKey = (RSAPublicKey)
keyFactory.generatePublic(pubSpec);
// decode private key
PKCS8EncodedKeySpec privSpec = …Run Code Online (Sandbox Code Playgroud) 用例:我有一个用例,其中客户端生成私钥和公钥,将 base 64 编码的公钥发送到服务器。
在服务器端,我将使用此公钥加密消息并将加密消息发送到客户端,客户端使用其私钥解密。商定的算法是“RSA”。
问题出在服务器端,我看到某些密钥正在X509EncodedKeySpec用作密钥规范
byte[] publicBytes = Base64.decodeBase64(base64EncodedPubKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pubKey = keyFactory.generatePublic(keySpec);
Run Code Online (Sandbox Code Playgroud)
虽然有些键会使用以下方法抛出异常 ( Caused by: java.security.InvalidKeyException: IOException: algid parse error, not a sequence)X509EncodedKeySpec但可以使用RSAPublicKeySpec:
byte[] publicBytes = Base64.decodeBase64(base64EncodedPubKey);
org.bouncycastle.asn1.pkcs.RSAPublicKey.RSAPublicKey pkcs1PublicKey = org.bouncycastle.asn1.pkcs.RSAPublicKey.RSAPublicKey.getInstance(publicBytes);
BigInteger modulus = pkcs1PublicKey.getModulus();
BigInteger publicExponent = pkcs1PublicKey.getPublicExponent();
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, publicExponent);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pubKey = keyFactory.generatePublic(keySpec);
Run Code Online (Sandbox Code Playgroud)
所以,我开始理解的是客户端和服务器需要同意是否使用:
PKCS #1或X.509对 key 进行编码。 …
我阅读了以下https://nodejs.org/api/crypto.html#crypto_class_sign并尝试复制代码:
签名.js
const crypto = require('crypto');
const { privateKey, publicKey } = crypto.generateKeyPairSync('ec', {
namedCurve: 'sect239k1'
});
const sign = crypto.createSign('SHA256');
sign.write('some data to sign');
sign.end();
const signature = sign.sign(privateKey, 'hex');
const verify = crypto.createVerify('SHA256');
verify.write('some data to sign');
verify.end();
console.log(verify.verify(publicKey, signature));
// Prints: true or false
Run Code Online (Sandbox Code Playgroud)
但它给我一个错误
>> node sign.js
internal/crypto/keygen.js:73
throw new ERR_INVALID_OPT_VALUE('publicKeyEncoding', publicKeyEncoding);
^
TypeError [ERR_INVALID_OPT_VALUE]: The value "undefined" is invalid for option "publicKeyEncoding"
at parseKeyEncoding (internal/crypto/keygen.js:73:11)
at check (internal/crypto/keygen.js:240:7)
at Object.generateKeyPairSync (internal/crypto/keygen.js:53:16)
at Object.<anonymous> …Run Code Online (Sandbox Code Playgroud) javascript encryption encryption-asymmetric public-key-encryption node.js
我按照github 的说明进行操作,但卡在了第 2 步。
第2步:
如果您使用的是 macOS Sierra 10.12.2 或更高版本,则需要修改 ~/.ssh/config 文件以自动将密钥加载到 ssh-agent 中并将密码存储在钥匙串中。
我没有 ~/.ssh/config 文件。
我应该创建一个并将其放在那里吗?
为什么他们认为我有一个我没有的文件?
来自他们的文档:
笔记:
我已经生成了密钥对。
尝试使用RSA加密消息:我一直收到此错误,但是我不确定这意味着什么:
生成密钥的代码
generator = KeyPairGenerator.getInstance("RSA");
RSAKeyGenParameterSpec kpgSpec = new RSAKeyGenParameterSpec(2048, BigInteger.valueOf(17489));
generator.initialize(kpgSpec);
KeyPair keyPair = generator.generateKeyPair();
publicKey = (RSAPublicKey) keyPair.getPublic();
privateKey = (RSAPrivateKey) keyPair.getPrivate();
Run Code Online (Sandbox Code Playgroud)
编码消息的代码
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(publicKeyBytes), BigInteger.valueOf(17489));
Cipher cipher;
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey currentKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, currentKey);
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
encrypted = bytesToString(encryptedBytes);
Run Code Online (Sandbox Code Playgroud)
错误:
W/System.err: java.lang.IllegalArgumentException: RSA modulus has a small prime factor
W/System.err: at com.android.org.bouncycastle.crypto.params.RSAKeyParameters.validate(RSAKeyParameters.java:46)
at com.android.org.bouncycastle.crypto.params.RSAKeyParameters.<init>(RSAKeyParameters.java:28)
at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.RSAUtil.generatePublicKeyParameter(RSAUtil.java:44)
at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineInit(CipherSpi.java:288)
at com.android.org.bouncycastle.jcajce.provider.asymmetric.rsa.CipherSpi.engineInit(CipherSpi.java:406)
Run Code Online (Sandbox Code Playgroud) java ×4
cryptography ×2
encryption ×2
node.js ×2
pem ×2
public-key ×2
rsa ×2
database ×1
exponent ×1
git ×1
github ×1
https ×1
javascript ×1
macos ×1
modulus ×1
openssl ×1
pfx ×1
postgresql ×1
python ×1
sha1 ×1
ssh ×1