加密在C纯文本文件 [英] Encrypting a plain text file in C

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

问题描述

我目前正在写在C Linux应用程序,从配置文件中读取。这个配置文件包含了一些,我想对数据进行加密,所以它不是纯文本。我花了几个小时研究这一点,并没有找到一个可行的解决方案。因为应用程序将需要从配置到读我将需要能够对它进行加密和解密的飞行。因此,从研究到目前为止,我真的很喜欢OpenSSL的密码库。

I am currently writing a linux application in C that reads from a configuration file. This configuration file contains some data that I would like to encrypt so it is not plain text. I have spent hours researching this and have not found a viable solution. Since the application will need to read from the configuration I will need to be able to encrypt it and decrypt it on the fly. So far from research I really like openSSL crypto library. I know from the command line you can do:

OpenSSL的-AES ENC-256-CBC -salt -in file.txt的退房手续file.enc

openssl enc -aes-256-cbc -salt -in file.txt -out file.enc

如果任何人都可以提供我怎么可以在C做一个这样的例子,这将是最AP preciated。

If anyone can provide an example of how I can do this in C, it would be most appreciated.

推荐答案

您应该看一看在奥赖利-Book 。有关于如何事情加密几个例子。不幸的是,大多数是用于网络的加密。

You should have a look at the O'Reilly-Book. There are a couple of examples on how to encrypt things. Unfortunately the most are for Network-Encryption.

我发现,在书的例子,但没有测试

I found an example in the book, but didnt test it:

#include <openssl/evp.h>

int main(int argc,  char *argv[])
{
    EVP_CIPHER_CTX  ctx;
    char            key[EVP_MAX_KEY_LENGTH];
    char            iv[EVP_MAX_IV_LENGTH];
    char            *ct, *out;
    char            final[EVP_MAX_BLOCK_LENGTH];
    char            str[] = "123456789abcdef";
    int             i;
    if (!seed_prng())
    {
    printf("Fatal Error!  Unable to seed the PRNG!\n");
    abort();
    }
    select_random_key(key, EVP_MAX_KEY_LENGTH);
    select_random_iv(iv, EVP_MAX_IV_LENGTH);
    EVP_EncryptInit(&ctx, EVP_bf_cbc(), key, iv);
    ct = encrypt_example(&ctx, str, strlen(str), &i);
    printf("Ciphertext is %d bytes.\n", i);
    EVP_DecryptInit(&ctx, EVP_bf_cbc(), key, iv);
    out = decrypt_example(&ctx, ct, 8);
    printf("Decrypted: >>%s<<\n", out);
    out = decrypt_example(&ctx, ct + 8, 8);
    printf("Decrypted: >>%s<<\n", out);
    if (!EVP_DecryptFinal(&ctx, final, &i))
    {
    printf("Padding incorrect.\n");
    abort();
    }
    final[i] = 0;
    printf("Decrypted: >>%s<<\n", final);
}

char *encrypt_example(EVP_CIPHER_CTX *ctx, char *data, int inl, int *rb) 
{
    char *ret;
    int i, tmp, ol;
    ol = 0;
    ret = (char *)malloc(inl + EVP_CIPHER_CTX_block_size(ctx));
    for (i = 0; i < inl / 100; i++)
    {
    EVP_EncryptUpdate(ctx, &ret[ol], &tmp, &data[ol], 100);
    ol += tmp;
    }
    if (inl % 100)
    {
    EVP_EncryptUpdate(ctx, &ret[ol], &tmp, &data[ol], inl%100);
    ol += tmp;
    }
    EVP_EncryptFinal(ctx, &ret[ol], &tmp);
    *rb = ol + tmp;
    return ret;
}

char *decrypt_example(EVP_CIPHER_CTX *ctx, char *ct, int inl)
{
    /* We're going to null-terminate the plaintext under the assumption it's
     * non-null terminated ASCII text. The null can be ignored otherwise.
     */
    char *pt = (char *)malloc(inl + EVP_CIPHER_CTX_block_size(ctx) + 1);
    int ol;
    EVP_DecryptUpdate(ctx, pt, &ol, ct, inl);
    if (!ol) /* there's no block to decrypt */
    {
    free(pt);
    return NULL;
    }
    pt[ol] = 0;
    return pt;
}

希望这会帮助你。

Hope this will help you.

这篇关于加密在C纯文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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