如何使用公钥RSA

时间:2018-03-13 15:50:05

标签: cryptography rsa public-key-encryption cryptojs

我刚接触加密并尝试使用公钥对邮件执行简单的RSA签名。

const PublicKey = "-----BEGIN PUBLIC KEY-----GIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCS7h56MiVTsV1SZ9gvWidH5XheZIpq1PUjKA//6m5N5HYkDtWQ9d+jx6niKDRUYf+aVc8StDKvIRJGT2ZZtIJ27FG9VpvZhR5yG38sh51MAPqZ/rpIzXg2Vj9dR2y3IUyWAafJ/VVGlecSYWREK1t6aMi7piHZpP/Rvn+1ImUTQIDAQAB-----END PUBLIC KEY-----"
const MessageKey = "4FSF5BE55B26FBABA402C23E1FF85E8F"

jsrsasign 库的帮助下,我实现了这个目标:

  const publicKey = RSA.KEYUTIL.getKey(PublicKey)
  const encrypted = RSA.KJUR.crypto.Cipher.encrypt(MessageKey, publicKey, "RSA")
  console.log(RSA.hextob64(encrypted))

但我担心这是不正确的。是否有人在JS中使用公钥对消息进行加密?

1 个答案:

答案 0 :(得分:1)

使用私钥签名,使用公钥加密

生成一个pub键(使用openssl和id_rsa):

openssl rsa -in ~/.ssh/id_rsa -pubout > ~/.ssh/id_rsa.pub.pem

加密:

cat plain.txt | openssl rsautl -encrypt -pubin -inkey ~/.ssh/id_rsa.pub.pem > cipher.txt

解密:

cat cipher.txt | openssl rsautl -decrypt -inkey ~/.ssh/id_rsa