linux c加密和解密的例子

此方法利用按位异或的算法,按位异或就是指同位得1,异位得0;
例如:2和1异或
2的二进制是10
1的二进制是01
2^1的结果是00
我们将2^1的结果00再次和1异或
2^1的结果是00
1的二进制是01
异或结果是10(也就是2)
相信大家已经明白了我的意思了。
先看一个固定的key(31)的算法
//加密解密程序
voidencrypt(char*message)
{
    while(*message) {
        //对message的每一个字符和31进行按位异或
        *message = *message ^ 31;
        message++;
    }
}
 
intmain()
{
    chars[] = "Hello qizexi";
     
    //运行一次:进行加密
    encrypt(s);
    printf("加密:%s\n", s);
    //再运行一次是解密
    encrypt(s);
    printf("解密:%s\n", s);
     
    return0;
}


下面是我们程序中经常看到的模样:
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
 
//加密解密程序
voidencrypt(char*message, constchar *key)
{
    inti;
    intlen = strlen(key);
    while(*message) {
        //对message的每一个字符和key进行按位异或
        for(i = 0; i < len; i++) {
            *message = *message ^ (int)key[i];
        }
        message++;
    }
}
 
intmain()
{
    //密文
    chars[] = "Hello qizexi";
    //密匙
    char*key = "qizexi@163.com";
     
    //运行一次:进行加密
    encrypt(s, key);
    printf("加密:%s\n", s);
    //再运行:进行解密
    encrypt(s, key);
    printf("解密:%s\n", s);
     
    return0;
}


  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的Linux文本IO加密/解密代码的示例,使用了openssl库中的AES算法: ```c #include <openssl/aes.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define KEY_SIZE 256 #define BLOCK_SIZE 16 void encrypt_file(const char *in_filename, const char *out_filename, const char *key_str) { unsigned char key[KEY_SIZE/8]; AES_KEY aes_key; int bytes_read, bytes_written; unsigned char in_data[BLOCK_SIZE]; unsigned char out_data[BLOCK_SIZE]; FILE *in_file = fopen(in_filename, "rb"); FILE *out_file = fopen(out_filename, "wb"); // Convert key string to binary memset(key, 0, sizeof(key)); memcpy(key, key_str, strlen(key_str)); // Set encryption key AES_set_encrypt_key(key, KEY_SIZE, &aes_key); // Encrypt file while ((bytes_read = fread(in_data, 1, BLOCK_SIZE, in_file)) > 0) { AES_encrypt(in_data, out_data, &aes_key); bytes_written = fwrite(out_data, 1, bytes_read, out_file); } fclose(in_file); fclose(out_file); } void decrypt_file(const char *in_filename, const char *out_filename, const char *key_str) { unsigned char key[KEY_SIZE/8]; AES_KEY aes_key; int bytes_read, bytes_written; unsigned char in_data[BLOCK_SIZE]; unsigned char out_data[BLOCK_SIZE]; FILE *in_file = fopen(in_filename, "rb"); FILE *out_file = fopen(out_filename, "wb"); // Convert key string to binary memset(key, 0, sizeof(key)); memcpy(key, key_str, strlen(key_str)); // Set decryption key AES_set_decrypt_key(key, KEY_SIZE, &aes_key); // Decrypt file while ((bytes_read = fread(in_data, 1, BLOCK_SIZE, in_file)) > 0) { AES_decrypt(in_data, out_data, &aes_key); bytes_written = fwrite(out_data, 1, bytes_read, out_file); } fclose(in_file); fclose(out_file); } int main(int argc, char *argv[]) { if (argc != 4) { printf("Usage: %s <input file> <output file> <key>\n", argv[0]); exit(EXIT_FAILURE); } const char *in_filename = argv[1]; const char *out_filename = argv[2]; const char *key_str = argv[3]; encrypt_file(in_filename, "encrypted.tmp", key_str); decrypt_file("encrypted.tmp", out_filename, key_str); remove("encrypted.tmp"); printf("File %s encrypted and decrypted successfully.\n", in_filename); return 0; } ``` 这个例子中,我们使用了256位的AES算法,并将加密解密操作封装到`encrypt_file`和`decrypt_file`函数中。我们还实现了一个简单的命令行接口,可以通过传递输入文件、输出文件和密钥来使用这些函数加密解密文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值