采用c RSA加密解密 [英] rsa encryption decryption using c

查看:388
本文介绍了采用c RSA加密解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写的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:


  1. 阅读公钥到 RSA * 结构。这取决于你的密钥格式。如果关键是在 PEM 格式,请使用 PEM_read_RSA_PUBKEY 功能。如果是在 DER 的形式,使用 d2i_RSA

  2. 加密使用RSA公共密钥数据。使用 RSA_public_encrypt 功能。

  3. 将数据写入到文件或任何你想做的事情。

  1. Read the public key into RSA * structure. It depends on your key format. If key is in PEM format, use PEM_read_RSA_PUBKEY functions. If it is in DER form, use d2i_RSA.
  2. Encrypt your data using RSA public key. Use RSA_public_encrypt function.
  3. Write the data to file or whatever you want to do.

有关RSA解密步骤是:

Steps for RSA decryption are:


  1. 读取专用钥匙插入 RSA * 结构。这是类似于RSA加密与一些微小的差别步骤1。

  2. RSA_private_decrypt 解密数据。使用 RSA_private_decrypt

  1. Read the private key into RSA * structure. It is similar to step 1 in RSA encryption with some minor difference.
  2. Decrypt the data using RSA_private_decrypt. Use RSA_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆