加密算法DES、AES

加密算法是一种用于保护数据隐私的技术,它通过对数据进行加密,使得除了拥有解密密钥的人之外,其他人无法读取或修改数据。常见的加密算法有对称加密算法和非对称加密算法。

对称加密算法指加密和解密使用同一个密钥的加密算法,常见的对称加密算法有DES、AES等。下面是一个使用AES对称加密算法进行加密和解密的例子:```c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>

#define KEY_SIZE 128

// 将十六进制字符串转换成二进制数组
void hex2bin(unsigned char *hex, unsigned char *bin, int len) {
    int i;
    for (i = 0; i < len; i++) {
        sscanf(hex + 2 * i, "%2hhx", &bin[i]);
    }
}

// AES加密
void aes_encrypt(unsigned char *key, unsigned char *input, unsigned char *output, int len) {
    AES_KEY aes_key;
    AES_set_encrypt_key(key, KEY_SIZE, &aes_key);
    AES_encrypt(input, output, &aes_key);
}

// AES解密
void aes_decrypt(unsigned char *key, unsigned char *input, unsigned char *output, int len) {
    AES_KEY aes_key;
    AES_set_decrypt_key(key, KEY_SIZE, &aes_key);
    AES_decrypt(input, output, &aes_key);
}

int main() {
    unsigned char key_hex[] = "00112233445566778899aabbccddeeff";
    unsigned char input[] = "Hello World!";
    unsigned char output[16];
    unsigned char decrypted[16];
    unsigned char key[KEY_SIZE / 8];
    hex2bin(key_hex, key, KEY_SIZE / 4);
    aes_encrypt(key, input, output, sizeof(input));
    printf("Encrypted: ");
    for (int i = 0; i < sizeof(output); i++) {
        printf("%02x", output[i]);
    }
    printf("\n");
    aes_decrypt(key, output, decrypted, sizeof(output));
    printf("Decrypted: %s\n", decrypted);
    return 0;
}


```

非对称加密算法指加密和解密使用不同密钥的加密算法,常见的非对称加密算法有RSA、DSA等。下面是一个使用RSA非对称加密算法进行加密和解密的例子:```c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

#define KEY_SIZE 2048

// 读取PEM格式的公钥和私钥
void read_key_files(char *pub_key_file, char *priv_key_file, RSA **pub_key, RSA **priv_key) {
    FILE *fp;
    if ((fp = fopen(pub_key_file, "r")) == NULL) {
        printf("Error: cannot open public key file.\n");
        exit(1);
    }
    *pub_key = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
    fclose(fp);
    if ((fp = fopen(priv_key_file, "r")) == NULL) {
        printf("Error: cannot open private key file.\n");
        exit(1);
    }
    *priv_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
    fclose(fp);
}

int main() {
    RSA *pub_key, *priv_key;
    char *pub_key_file = "public.pem";
    char *priv_key_file = "private.pem";
    unsigned char input[] = "Hello World!";
    unsigned char output[KEY_SIZE / 8];
    unsigned char decrypted[KEY_SIZE / 8];
    read_key_files(pub_key_file, priv_key_file, &pub_key, &priv_key);
    RSA_public_encrypt(sizeof(input), input, output, pub_key, RSA_PKCS1_PADDING);
    printf("Encrypted: ");
    for (int i = 0; i < sizeof(output); i++) {
        printf("%02x", output[i]);
    }
    printf("\n");
    RSA_private_decrypt(sizeof(output), output, decrypted, priv_key, RSA_PKCS1_PADDING);
    printf("Decrypted: %s\n", decrypted);
    return 0;
}


```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值