接口安全机制(DES和AES加密)

在对外发布服务接口的时候, 需要定制一套签名机制, 保证数据传递的有效性。

1、安全机制的设计方案

1.1 单向加密

在理论上, 从明文加密到密文后, 不可反向解密的。
可以从迭代加盐的方式尽可能保证加密数据不可反向解密。
传递敏感数据时使用,如密码。
在金融相关交易中, 用户密码是敏感数据, 其他数据是非敏感数据。所有的金融相关的应用中, 客户端都有一个独立的密码输入控件。这个控件就是做单向加密的。
使用单向加密的时候,传递的数据只有密文,没有明文,也没有密钥。

1.2 双向加密

是可以实现加密和解密双向运算的算法。需要通过密钥实现加解密计算的。
密钥种类: 公钥、私钥。
公钥: 可以对外公开的, 就是可以在网络中传递的。
私钥: 必须保密的, 绝对不会对外暴露的。如U盾
传递安全数据时使用。所谓安全数据,即非敏感的不可篡改数据。如金融交易中的收款人卡号、转账金额、货币类型等。
使用双向加密时, 传递的数据有明文、密文、公钥。

1.2.1 对称加密(如DES和AES)

只有一个密钥, 就是公钥。加解密用同一秘钥,任一方泄露就完蛋,黑客能够收发自如。
在这里插入图片描述

1.2.2 非对称加密(如RES)

有两个密钥, 公钥(钥匙B/D)和私钥(钥匙A/C)。若只泄露了一个秘钥,黑客只能收或发信息。
在这里插入图片描述

2、DES加密

DES的密文是非定长密文。根据明文数据和key数据动态伸缩。

3、AES加密

AES的key要求长度为16。

4、DES和AES的对比

DES - 加密后的数据是16的整数倍。 是16字节整数倍。
AES - 要求key的长度必须是16字节。 AES相对效率较低, 但是可以通过偏移量强化加密。

5、DES和AES的使用场景

DES和AES在使用场景上没有区别。
传递非敏感的安全性数据可以使用。如: QQ通讯录获取, 微信中的消息传递。

6、RES加密

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现文件的AES和DES加密需要以下步骤: 1. 读取文件数据 2. 对数据进行填充,使其满足加密算法的要求 3. 设置加密算法的参数,如密钥、加密模式、填充方式等 4. 执行加密操作 5. 将加密后的数据写入文件 下面是一个使用 OpenSSL 库实现文件的 AES 和 DES 加密的示例代码: ```c #include <stdio.h> #include <openssl/evp.h> #define BUFFER_SIZE 1024 void encrypt_file_aes(const char* input_file, const char* output_file, const unsigned char* key, const unsigned char* iv) { // 打开输入文件 FILE* in = fopen(input_file, "rb"); if (in == NULL) { printf("无法打开输入文件 %s\n", input_file); return; } // 打开输出文件 FILE* out = fopen(output_file, "wb"); if (out == NULL) { fclose(in); printf("无法打开输出文件 %s\n", output_file); return; } // 创建 AES 加密上下文 EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); if (ctx == NULL) { fclose(in); fclose(out); printf("无法创建加密上下文\n"); return; } // 初始化 AES 加密上下文 if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法初始化加密上下文\n"); return; } unsigned char inbuf[BUFFER_SIZE]; unsigned char outbuf[BUFFER_SIZE + EVP_MAX_BLOCK_LENGTH]; int inlen, outlen; // 循环读取、加密和写入文件数据 do { inlen = fread(inbuf, 1, BUFFER_SIZE, in); if (EVP_EncryptUpdate(ctx, outbuf, &outlen, inbuf, inlen) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法加密数据\n"); return; } fwrite(outbuf, 1, outlen, out); } while (inlen == BUFFER_SIZE); // 结束加密操作 if (EVP_EncryptFinal_ex(ctx, outbuf, &outlen) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法结束加密操作\n"); return; } fwrite(outbuf, 1, outlen, out); // 释放资源 fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); } void decrypt_file_aes(const char* input_file, const char* output_file, const unsigned char* key, const unsigned char* iv) { // 打开输入文件 FILE* in = fopen(input_file, "rb"); if (in == NULL) { printf("无法打开输入文件 %s\n", input_file); return; } // 打开输出文件 FILE* out = fopen(output_file, "wb"); if (out == NULL) { fclose(in); printf("无法打开输出文件 %s\n", output_file); return; } // 创建 AES 解密上下文 EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); if (ctx == NULL) { fclose(in); fclose(out); printf("无法创建解密上下文\n"); return; } // 初始化 AES 解密上下文 if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法初始化解密上下文\n"); return; } unsigned char inbuf[BUFFER_SIZE + EVP_MAX_BLOCK_LENGTH]; unsigned char outbuf[BUFFER_SIZE]; int inlen, outlen; // 循环读取、解密和写入文件数据 do { inlen = fread(inbuf, 1, BUFFER_SIZE + EVP_MAX_BLOCK_LENGTH, in); if (EVP_DecryptUpdate(ctx, outbuf, &outlen, inbuf, inlen) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法解密数据\n"); return; } fwrite(outbuf, 1, outlen, out); } while (inlen == BUFFER_SIZE + EVP_MAX_BLOCK_LENGTH); // 结束解密操作 if (EVP_DecryptFinal_ex(ctx, outbuf, &outlen) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法结束解密操作\n"); return; } fwrite(outbuf, 1, outlen, out); // 释放资源 fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); } void encrypt_file_des(const char* input_file, const char* output_file, const unsigned char* key, const unsigned char* iv) { // 打开输入文件 FILE* in = fopen(input_file, "rb"); if (in == NULL) { printf("无法打开输入文件 %s\n", input_file); return; } // 打开输出文件 FILE* out = fopen(output_file, "wb"); if (out == NULL) { fclose(in); printf("无法打开输出文件 %s\n", output_file); return; } // 创建 DES 加密上下文 EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); if (ctx == NULL) { fclose(in); fclose(out); printf("无法创建加密上下文\n"); return; } // 初始化 DES 加密上下文 if (EVP_EncryptInit_ex(ctx, EVP_des_cbc(), NULL, key, iv) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法初始化加密上下文\n"); return; } unsigned char inbuf[BUFFER_SIZE]; unsigned char outbuf[BUFFER_SIZE + EVP_MAX_BLOCK_LENGTH]; int inlen, outlen; // 循环读取、加密和写入文件数据 do { inlen = fread(inbuf, 1, BUFFER_SIZE, in); if (EVP_EncryptUpdate(ctx, outbuf, &outlen, inbuf, inlen) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法加密数据\n"); return; } fwrite(outbuf, 1, outlen, out); } while (inlen == BUFFER_SIZE); // 结束加密操作 if (EVP_EncryptFinal_ex(ctx, outbuf, &outlen) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法结束加密操作\n"); return; } fwrite(outbuf, 1, outlen, out); // 释放资源 fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); } void decrypt_file_des(const char* input_file, const char* output_file, const unsigned char* key, const unsigned char* iv) { // 打开输入文件 FILE* in = fopen(input_file, "rb"); if (in == NULL) { printf("无法打开输入文件 %s\n", input_file); return; } // 打开输出文件 FILE* out = fopen(output_file, "wb"); if (out == NULL) { fclose(in); printf("无法打开输出文件 %s\n", output_file); return; } // 创建 DES 解密上下文 EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); if (ctx == NULL) { fclose(in); fclose(out); printf("无法创建解密上下文\n"); return; } // 初始化 DES 解密上下文 if (EVP_DecryptInit_ex(ctx, EVP_des_cbc(), NULL, key, iv) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法初始化解密上下文\n"); return; } unsigned char inbuf[BUFFER_SIZE + EVP_MAX_BLOCK_LENGTH]; unsigned char outbuf[BUFFER_SIZE]; int inlen, outlen; // 循环读取、解密和写入文件数据 do { inlen = fread(inbuf, 1, BUFFER_SIZE + EVP_MAX_BLOCK_LENGTH, in); if (EVP_DecryptUpdate(ctx, outbuf, &outlen, inbuf, inlen) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法解密数据\n"); return; } fwrite(outbuf, 1, outlen, out); } while (inlen == BUFFER_SIZE + EVP_MAX_BLOCK_LENGTH); // 结束解密操作 if (EVP_DecryptFinal_ex(ctx, outbuf, &outlen) != 1) { fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); printf("无法结束解密操作\n"); return; } fwrite(outbuf, 1, outlen, out); // 释放资源 fclose(in); fclose(out); EVP_CIPHER_CTX_free(ctx); } ``` 使用示例: ```c int main() { // 128 位 AES 密钥和 IV unsigned char aes_key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; unsigned char aes_iv[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; // 64 位 DES 密钥和 IV unsigned char des_key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; unsigned char des_iv[] = { 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; // 加密 AES 文件 encrypt_file_aes("input.txt", "output_aes.bin", aes_key, aes_iv); // 解密 AES 文件 decrypt_file_aes("output_aes.bin", "input_aes.txt", aes_key, aes_iv); // 加密 DES 文件 encrypt_file_des("input.txt", "output_des.bin", des_key, des_iv); // 解密 DES 文件 decrypt_file_des("output_des.bin", "input_des.txt", des_key, des_iv); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值