利用feistal算法和RSA算法,以数字信封的形式对明文文件进行加密。要求如下: 1、发送方利用系统自带的伪随机数生成函数生成会话密钥 2、用接收方的公钥对会话密钥加密 3、用会话密钥以OFB的模式对明文文件p_text.txt(文件大于1KB)进行加密,结果存于密文文件c_text.txt 4、接收方利用私钥解密会话密钥,然后用会话密钥对密文文件解密,结果存于明文文件p1_text.txt,最后对比p_text.txt和p1_text.txt
时间: 2024-03-11 15:43:33 浏览: 82
以下是C语言实现示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#define BLOCK_SIZE 8 // Feistal算法分块大小
#define KEY_SIZE 128 // RSA算法密钥长度
// 生成随机数
void generate_random(unsigned char *key, int len) {
int i;
srand((unsigned int)time(NULL));
for (i = 0; i < len; i++) {
key[i] = rand() % 256;
}
}
// Feistal算法的轮函数
void feistal_round(unsigned char *data, unsigned char *key) {
int i;
unsigned char tmp[BLOCK_SIZE];
memcpy(tmp, data, BLOCK_SIZE); // 保存data的副本
for (i = 0; i < BLOCK_SIZE; i++) {
data[i] ^= key[i]; // 异或操作
}
memcpy(key, tmp, BLOCK_SIZE); // 更新key
}
// Feistal算法加密
void feistal_encrypt(unsigned char *data, unsigned char *key, int rounds) {
int i;
for (i = 0; i < rounds; i++) {
feistal_round(data, key);
}
}
// RSA加密
int rsa_encrypt(unsigned char *in, int in_len, unsigned char *out, RSA *rsa) {
int len = RSA_public_encrypt(in_len, in, out, rsa, RSA_PKCS1_PADDING);
return len;
}
// RSA解密
int rsa_decrypt(unsigned char *in, int in_len, unsigned char *out, RSA *rsa) {
int len = RSA_private_decrypt(in_len, in, out, rsa, RSA_PKCS1_PADDING);
return len;
}
// OFB模式加密
void ofb_encrypt(FILE *in, FILE *out, unsigned char *key, int key_len) {
unsigned char iv[BLOCK_SIZE], tmp[BLOCK_SIZE], cipher[BLOCK_SIZE];
int i, nread;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_EncryptInit_ex(ctx, EVP_aes_128_ofb(), NULL, key, iv);
memcpy(tmp, iv, BLOCK_SIZE);
while ((nread = fread(tmp, 1, BLOCK_SIZE, in)) > 0) {
EVP_EncryptUpdate(ctx, cipher, &i, tmp, BLOCK_SIZE);
fwrite(cipher, 1, nread, out);
memcpy(tmp, cipher, BLOCK_SIZE);
}
EVP_EncryptFinal_ex(ctx, cipher, &i);
fwrite(cipher, 1, i, out);
EVP_CIPHER_CTX_free(ctx);
}
// OFB模式解密
void ofb_decrypt(FILE *in, FILE *out, unsigned char *key, int key_len) {
unsigned char iv[BLOCK_SIZE], tmp[BLOCK_SIZE], cipher[BLOCK_SIZE];
int i, nread;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex(ctx, EVP_aes_128_ofb(), NULL, key, iv);
memcpy(tmp, iv, BLOCK_SIZE);
while ((nread = fread(tmp, 1, BLOCK_SIZE, in)) > 0) {
EVP_DecryptUpdate(ctx, cipher, &i, tmp, BLOCK_SIZE);
fwrite(cipher, 1, nread, out);
memcpy(tmp, cipher, BLOCK_SIZE);
}
EVP_DecryptFinal_ex(ctx, cipher, &i);
fwrite(cipher, 1, i, out);
EVP_CIPHER_CTX_free(ctx);
}
int main() {
unsigned char key[BLOCK_SIZE], iv[BLOCK_SIZE];
unsigned char enc_key[KEY_SIZE], dec_key[KEY_SIZE];
unsigned char buffer[BLOCK_SIZE];
int i, nread;
FILE *in, *out;
RSA *rsa;
EVP_PKEY *pubkey, *prikey;
char *pubkey_path = "pubkey.pem";
char *prikey_path = "prikey.pem";
char *p_text_path = "p_text.txt";
char *c_text_path = "c_text.txt";
char *p1_text_path = "p1_text.txt";
int feistal_rounds = 16;
// 读取明文文件
in = fopen(p_text_path, "rb");
if (in == NULL) {
printf("Failed to open file %s\n", p_text_path);
return 1;
}
// 生成Feistal算法的轮密钥
generate_random(key, BLOCK_SIZE);
memcpy(iv, key, BLOCK_SIZE);
// 生成会话密钥
generate_random(enc_key, KEY_SIZE / 8);
// 用接收方的公钥加密会话密钥
pubkey = EVP_PKEY_new();
FILE *pubkey_file = fopen(pubkey_path, "r");
if (!PEM_read_PUBKEY(pubkey_file, &pubkey, NULL, NULL)) {
printf("Failed to read public key\n");
return 1;
}
fclose(pubkey_file);
rsa = EVP_PKEY_get1_RSA(pubkey);
int enc_len = rsa_encrypt(enc_key, KEY_SIZE / 8, enc_key, rsa);
// 将加密后的会话密钥和Feistal算法的轮密钥写入密文文件
out = fopen(c_text_path, "wb");
fwrite(enc_key, 1, enc_len, out);
fwrite(key, 1, BLOCK_SIZE, out);
// 使用OFB模式加密明文文件
ofb_encrypt(in, out, iv, BLOCK_SIZE);
fclose(out);
fclose(in);
// 读取密文文件
in = fopen(c_text_path, "rb");
if (in == NULL) {
printf("Failed to open file %s\n", c_text_path);
return 1;
}
// 解密会话密钥
prikey = EVP_PKEY_new();
FILE *prikey_file = fopen(prikey_path, "r");
if (!PEM_read_PrivateKey(prikey_file, &prikey, NULL, NULL)) {
printf("Failed to read private key\n");
return 1;
}
fclose(prikey_file);
rsa = EVP_PKEY_get1_RSA(prikey);
int dec_len = rsa_decrypt(enc_key, enc_len, dec_key, rsa);
// 读取Feistal算法的轮密钥
nread = fread(buffer, 1, BLOCK_SIZE, in);
memcpy(key, buffer, BLOCK_SIZE);
// 使用OFB模式解密密文文件
out = fopen(p1_text_path, "wb");
ofb_decrypt(in, out, iv, BLOCK_SIZE);
fclose(out);
fclose(in);
// 比较明文和解密后的明文是否相同
in = fopen(p_text_path, "rb");
out = fopen(p1_text_path, "rb");
while ((nread = fread(buffer, 1, BLOCK_SIZE, in)) > 0) {
fread(iv, 1, BLOCK_SIZE, out);
feistal_encrypt(iv, key, feistal_rounds);
for (i = 0; i < nread; i++) {
buffer[i] ^= iv[i];
}
fwrite(buffer, 1, nread, out);
}
fclose(in);
fclose(out);
printf("Encryption and decryption completed successfully.\n");
return 0;
}
```
需要注意的是,这段代码中使用了OpenSSL库来实现RSA加密和解密、OFB模式加密和解密等操作,需要在编译时链接该库。在gcc编译时可以添加`-lcrypto`选项。此外,还需要自行生成公钥和私钥文件,并将其分别命名为`pubkey.pem`和`prikey.pem`,并放在代码所在目录下。
阅读全文