采用c RSA加密解密 [英] rsa encryption decryption using c
问题描述
我试着写的RSA加密C code和使用Open SSL解密。但我不能这样做。我GOOGLE了它但无论code,我从网上得到它是从我的头。主
函数放在这里,我从堆栈溢出了。我试着用它...但它不工作。可能是我不好。
I'm trying to write C code of RSA encryption and decryption using Open SSL. But I am not able to do so. I googled it but whatever code I got from internet it was out of my head.
main
function goes here which I got from stack overflow. I tried using it … but its not working. May be my bad.
encrypt(FILE *rsa_Pkey_fole,FILE *in_file,FILE *out_file){
}
int main(int argc, char *argv[])
{
FILE *rsa_pkey_file, *infile;
int rv;
if (argc < 2) {
fprintf(stderr, "Usage: %s <PEM RSA Public Key File>\n", argv[0]);
exit(1);
}
rsa_pkey_file = fopen(argv[1], "rb");
infile = fopen(argv[2], "w");
if (!rsa_pkey_file) {
perror(argv[1]);
fprintf(stderr, "Error loading PEM RSA Public Key File.\n");
exit(2);
}
rv = encrypt(rsa_pkey_file, infile.txt, stdout);
fclose(rsa_pkey_file);
return rv;
}
和类似的方式解密。
我该怎么办RSA加密和简单的方式使用Open SSL库在C文件解密?
How can I do RSA encryption and decryption of a file using Open SSL library in C in simple way?
推荐答案
对于RSA加密的步骤如下:
Steps for RSA encryption are as follows:
- 阅读公钥到
RSA *
结构。这取决于你的密钥格式。如果关键是在PEM
格式,请使用PEM_read_RSA_PUBKEY
功能。如果是在DER
的形式,使用d2i_RSA
。 - 加密使用RSA公共密钥数据。使用
RSA_public_encrypt
功能。 - 将数据写入到文件或任何你想做的事情。
- Read the public key into
RSA *
structure. It depends on your key format. If key is inPEM
format, usePEM_read_RSA_PUBKEY
functions. If it is inDER
form, used2i_RSA
. - Encrypt your data using RSA public key. Use
RSA_public_encrypt
function. - Write the data to file or whatever you want to do.
有关RSA解密步骤是:
Steps for RSA decryption are:
- 读取专用钥匙插入
RSA *
结构。这是类似于RSA加密与一些微小的差别步骤1。 - 是
RSA_private_decrypt
解密数据。使用RSA_private_decrypt
。
- Read the private key into
RSA *
structure. It is similar to step 1 in RSA encryption with some minor difference. - Decrypt the data using
RSA_private_decrypt
. UseRSA_private_decrypt
.
您可以看看OpenSSL文档这是相当有用的,它的名称是直观的。我只给你广泛的层面上的想法。如果您需要更多的帮助,我可以张贴code的例子。
You can look OpenSSL documentation which is quite useful and its names are intuitive. I give you just broad level idea. If you need more help, I can post the code example.
这篇关于采用c RSA加密解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!