AES加密,由S盒计算逆S盒程序

1.在工作中时常会遇到需要数据加密传输的情况,而AES加密则是安全性与运算速度都很好的加密算法,而且代码量极小,但给定的标准AES代码的S盒和逆S盒都是一样的,而为了加密数据的安全性,需要对内部的S盒修改时,则需要根据修修改后的S盒求出逆S盒,AES算法才能正确的加解密数据。

2.逆S盒是S盒的逆运算,比如数据0x12,则S盒则是将0x12,拆分为高低位两个数据,分别对应S盒的1行2列的数据,这里假设为0x58,而逆S盒的第5行8列的数据为0x12。

3.根据S盒与逆S盒原理写的C程序代码。

uint8_t Te_InvS[16][16] = { 0 };        //逆S盒缓存
uint8_t Te_InVSAdd[2] = { 0 };            //位置
for (uint8_t i = 0; i < 16; i++) {                    //计算逆S盒
    for (uint8_t n = 0; n < 16; n++) {
        Te_InVSAdd[0] = (S_BOX[i][n] >> 4) & 0x0f;        //取行
        Te_InVSAdd[1] = (S_BOX[i][n] >> 0) & 0x0f;        //取列
        Te_InvS[Te_InVSAdd[0]][Te_InVSAdd[1]] = i * 16 + n;        //置值
    }

其中的S_BOX为一个16*16的数组。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 OpenSSL 库来实现 Linux 下的 AES 加密和解密程序。以下是一个简单的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/aes.h> #define AES_BLOCK_SIZE 16 int main(int argc, char *argv[]) { AES_KEY aes_key; unsigned char key[AES_BLOCK_SIZE] = {'k', 'e', 'y', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c'}; unsigned char iv[AES_BLOCK_SIZE] = {'i', 'v', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd'}; unsigned char plaintext[] = "Hello, world!"; unsigned char ciphertext[sizeof(plaintext)]; unsigned char decryptedtext[sizeof(plaintext)]; int plaintext_len = strlen((char *) plaintext); int ciphertext_len, decryptedtext_len; AES_set_encrypt_key(key, AES_BLOCK_SIZE * 8, &aes_key); AES_cbc_encrypt(plaintext, ciphertext, plaintext_len, &aes_key, iv, AES_ENCRYPT); ciphertext_len = strlen((char *) ciphertext); AES_set_decrypt_key(key, AES_BLOCK_SIZE * 8, &aes_key); AES_cbc_encrypt(ciphertext, decryptedtext, ciphertext_len, &aes_key, iv, AES_DECRYPT); decryptedtext_len = strlen((char *) decryptedtext); printf("Plaintext: %s\n", plaintext); printf("Ciphertext: "); for(int i = 0; i < ciphertext_len; i++) printf("%x", ciphertext[i]); printf("\n"); printf("Decrypted text: %s\n", decryptedtext); return 0; } ``` 在该程序中,我们使用了 OpenSSL 库中 AES 加密算法的 CBC 模式。首先,我们需要定义一个 16 字节的密钥和初始化向量(IV)。然后,我们使用 `AES_set_encrypt_key()` 函数设置加密密钥,并使用 `AES_cbc_encrypt()` 函数加密明文。加密后的密文存储在 `ciphertext` 数组中,并使用 `strlen()` 函数计算密文长度。 接下来,我们使用 `AES_set_decrypt_key()` 函数设置解密密钥,并使用 `AES_cbc_encrypt()` 函数解密密文。解密后的明文存储在 `decryptedtext` 数组中,并使用 `strlen()` 函数计算明文长度。 最后,我们输出原始明文、加密后的密文和解密后的明文,以验证加密和解密过程是否正确。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值