英文:
RSA Encryption and Decryption in Erlang + RSA Key pair generating
问题
在Erlang编程语言中执行RSA加密和解密的过程如下:
{ok,[Entry]} = public_key:pem_to_der("./test_private_key"),
{ok,{'RSAPrivateKey', 'two-prime', N , E, D, _P, _Q, _E1, _E2, _C, _Other}} = public_key:decode_private_key(Entry),
PlainText = "now is the time for all good men to come to the aid of their country",
PrivKey = [crypto:mpint(E), crypto:mpint(N), crypto:mpint(D)] ,
PubKey = [crypto:mpint(E), crypto:mpint(N)],
Foo = crypto:rsa_private_encrypt(PlainText,PrivKey,rsa_pkcs1_padding),
Bar = crypto:rsa_public_decrypt(Foo,PubKey,rsa_pkcs1_padding),
PlainText == binary_to_list(Bar).
或者:
PrvKey = [257, 77181119504409699204797322144340611322502523292227043692039327666381749008487, 77131657261414365158890407156927969457179535804176721705182844017912686753873],
PubKey = [257, 77181119504409699204797322144340611322502523292227043692039327666381749008487],
Enc = crypto:rsa_private_encrypt("hello", PrvKey, rsa_pkcs1_padding),
Dec = crypto:rsa_public_decrypt(Enc,PubKey,rsa_pkcs1_padding),
B64 = base64:encode_to_string(Enc),
这是执行RSA加密和解密的示例代码,无需翻译代码部分。
英文:
How to do RSA encryption and decryption process in the Erlang programming language? how to make key RSA public key and private key? I have tried some sample codes like the below as they were old and not compatible with my Erlang OTP 25 version they were not useful, i.e some packages like rsa_private_encrypt can't be found in Erlang 25 and show errors, And they were for PEM files:
{ok,[Entry]} = public_key:pem_to_der("./test_private_key"),
{ok,{'RSAPrivateKey', 'two-prime', N , E, D, _P, _Q, _E1, _E2, _C, _Other}} = public_key:decode_private_key(Entry),
PlainText = "now is the time for all good men to come to the aid of their country",
PrivKey = [crypto:mpint(E), crypto:mpint(N), crypto:mpint(D)] ,
PubKey = [crypto:mpint(E), crypto:mpint(N)],
Foo = crypto:rsa_private_encrypt(PlainText,PrivKey,rsa_pkcs1_padding),
Bar = crypto:rsa_public_decrypt(Foo,PubKey,rsa_pkcs1_padding),
PlainText == binary_to_list(Bar).
OR
PrvKey = [257, 77181119504409699204797322144340611322502523292227043692039327666381749008487, 77131657261414365158890407156927969457179535804176721705182844017912686753873],
PubKey = [257, 77181119504409699204797322144340611322502523292227043692039327666381749008487],
Enc = crypto:rsa_private_encrypt("hello", PrvKey, rsa_pkcs1_padding),
Dec = crypto:rsa_public_decrypt(Enc,PubKey,rsa_pkcs1_padding),
B64 = base64:encode_to_string(Enc),
答案1
得分: 0
以下是翻译好的部分:
在阅读文档并参考以前的代码时,现在它正常工作:
声明您的明文或消息:
Msg = "Hello World",
然后生成您的公钥和私钥:
{PublicKey, PrivateKey} = crypto:generate_key(rsa, {2048, 65537}),
使用公钥生成加密消息:
RsaEncryptedCrypto = crypto:public_encrypt(rsa, list_to_binary(Msg), PublicKey, rsa_pkcs1_padding),
现在您可以解密您的加密消息:
RsaDecryptedCrypto = crypto:private_decrypt(rsa, RsaEncryptedCrypto, PrivateKey, rsa_pkcs1_padding)。
注意: 我们使用list_to_binary将明文转换为二进制,因为该方法需要该参数。
现在我们将解密后的二进制转换回字符串:
R = binary_to_list(RsaDecryptedCrypto),
然后打印输出:
io:format("~s~n",[R])。
所有代码:
Msg = "Hello World",
{PublicKey, PrivateKey} = crypto:generate_key(rsa, {2048, 65537}),
RsaEncryptedCrypto = crypto:public_encrypt(rsa, list_to_binary(Msg), PublicKey, rsa_pkcs1_padding),
RsaDecryptedCrypto = crypto:private_decrypt(rsa, RsaEncryptedCrypto, PrivateKey, rsa_pkcs1_padding),
R = binary_to_list(RsaDecryptedCrypto),
io:format("~s~n",[R])。
文档链接:https://www.erlang.org/doc/man/crypto.html#public_encrypt-4
英文:
While reading documentation and helping from previous codes, Now gladly it works:
Declare your plaintext or message:
Msg = "Hello World",
Then generate your public and private keys:
{PublicKey, PrivateKey} = crypto:generate_key(rsa, {2048,65537}),
Make your encrypted message with the public key:
RsaEncryptedCrypto = crypto:public_encrypt(rsa,list_to_binary(Msg),PublicKey,rsa_pkcs1_padding),
Now you can decrypt your encrypted message:
RsaDecryptedCrypto = crypto:private_decrypt(rsa,RsaEncryptedCrypto,PrivateKey,rsa_pkcs1_padding),
Note: We use list_to_binary to change our plaintext to binary as the method wants that argument
Now we change the decrypted binary back to string:
R = binary_to_list(RsaDecryptedCrypto)
we print the output:
io:format("~s~n",[R]).
All the codes:
Msg = "Hello World",
{PublicKey, PrivateKey} = crypto:generate_key(rsa, {2048,65537}),
RsaEncryptedCrypto = crypto:public_encrypt(rsa,list_to_binary(Msg),PublicKey,rsa_pkcs1_padding),
RsaDecryptedCrypto = crypto:private_decrypt(rsa,RsaEncryptedCrypto,PrivateKey,rsa_pkcs1_padding),
R = binary_to_list(RsaDecryptedCrypto),
io:format("~s~n",[R]).
Document: https://www.erlang.org/doc/man/crypto.html#public_encrypt-4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论