在Forge Javascript Cryptography Library中,我很难确定在使用私钥和公钥后如何重建私钥和公钥。我尝试了以下方法:
var rsa = forge.pki.rsa;
var keypair = rsa.generateKeyPair({bits: 2048, e: 0x10001});
var ct = keypair.publicKey.encrypt("Arbitrary Message Here");
keypair.privateKey.decrypt(ct);
按预期输出:"Arbitrary Message Here"
。我试图用这种方式重建公钥:
var nVal = JSON.stringify(keypair.publicKey.n);
var eVal = JSON.stringify(keypair.publicKey.e);
var nwRsa = rsa.setPublicKey(JSON.parse(nHere), JSON.parse(eHere));
var nwCt = nwRsa.encrypt("Arbitrary Message Here");
当我再次尝试加密邮件时,出现错误:
"TypeError: Object #<Object> has no method 'bitLength'"
我不确定为什么,因为我相信我已经正确使用了这个方法:
/**
* Sets an RSA public key from BigIntegers modulus and exponent.
*
* @param n the modulus.
* @param e the exponent.
*
* @return the public key.
*/
pki.setRsaPublicKey = pki.rsa.setPublicKey = function(n, e) {
var key = {
n: n,
e: e
};
我还没有尝试过重建私钥。我不确定我做得不对,有人可以帮帮我吗?一如既往地提前感谢!
额外的Afterthough:这里是我在html页面中导入的文件,我相信我已经解决了所有依赖问题,但我以防万一(请注意,它们不是按字母顺序排列的,但依赖顺序):
<script src="forge/util.js"></script>
<script src="forge/debug.js"></script>
<script src="forge/jsbn.js"></script>
<script src="forge/oids.js"></script>
<script src="forge/asn1.js"></script>
<script src="forge/sha1.js"></script>
<script src="forge/sha256.js"></script>
<script src="forge/md5.js"></script>
<script src="forge/md.js"></script>
<script src="forge/aes.js"></script>
<script src="forge/prng.js"></script>
<script src="forge/random.js"></script>
<script src="forge/jsbn.js"></script>
<script src="forge/pkcs1.js"></script>
<script src="forge/rsa.js"></script>
<script src="forge/des.js"></script>
<script src="forge/rc2.js"></script>
<script src="forge/pbe.js"></script>
<script src="forge/pem.js"></script>
<script src="forge/hmac.js"></script>
<script src="forge/pbkdf2.js"></script>
<script src="forge/pkcs7asn1.js"></script>
<script src="forge/pkcs12.js"></script>
<script src="forge/pss.js"></script>
<script src="forge/mgf1.js"></script>
<script src="forge/mgf.js"></script>
<script src="forge/x509.js"></script>
<script src="forge/pki.js"></script>
TL; DR:在Forge中,如何将其公钥提供给其他人,以及如何将其私钥存储多次使用?
答案 0 :(得分:12)
我很幸运能够通过提出issue on Github来与开发者联系。他给了我正确的使用方法,它们似乎都有效。直接引用,这是正确的用法:
最简单,最兼容的传输公钥或传输方式 存储私钥是将它们转换为PEM格式。私钥可能 在转换它们时可以选择加密(通过使用不同的 方法,见下文)。
将公钥转换为PEM或从PEM转换:
// convert a Forge public key to PEM-format var pem = pki.publicKeyToPem(publicKey); // convert a PEM-formatted public key to a Forge public key var publicKey = pki.publicKeyFromPem(pem); Converting a private key to/from PEM: // convert a Forge private key to PEM-format // (preferred method if you don't want encryption) var pem = pki.privateKeyToPem(privateKey); // wraps and encrypts a Forge private key and outputs it in PEM format // (preferred method if you do want encryption) var pem = pki.encryptRsaPrivateKey(privateKey, 'password'); // encrypts a Forge private key and outputs it in PEM format using OpenSSL's // proprietary legacy format + encapsulated PEM headers (DEK-Info) // (perhaps more compatible with some legacy OpenSSL-based applications) var pem = pki.encryptRsaPrivateKey(privateKey, 'password', {legacy: true}); // decrypts a PEM-formatted, encrypted private key var privateKey = pki.decryptRsaPrivateKey(pem, 'password');