C语言直接对数据加密解密

/*根据密码表进行数据加密/解密的代码(纯C书写)*/


/* 数据加密, 解密程序 by Flying Blue 081130 */
/* 这个程序是为我的游戏进行数据加密用的, 现在我把它开源 */
/* 理论上来说, 这个程序可以在任何编译器上编译 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* 求区间在[mmin, mmax]的随机数 */
int random(int mmin, int mmax)
{
    return rand() % (mmax - mmin + 1) + mmin;
}

/* 交换两个值 */
void _swap(unsigned char* arr, int i1, int i2)
{
    unsigned char tmp = arr[i1];
    arr[i1] = arr[i2];
    arr[i2] = tmp;
}

/* 随机生成编码表 */
void cipher_GenEncTable(unsigned char strTable[0x100])
{
    int i, n1, n2;

    /* 初始化随机数种子 */
    srand(time(0));

    /* 先生成一张按顺序的ASCII码表 */
    for(i = 0; i < 0x100; i++)
        strTable[i] = i;

    /* 再随机交换其中的两个值 */
    for(i = 0; i < 0x100; i++)
    {
        n1 = random(0, 0xFF);

        /* 防止生成的 n1 == n2 如果他们相等了那就没意义了 */
        while(n1 == (n2 = random(0, 0xFF)));

        /* 交换第n1个和第n2个值 */
        _swap(strTable, n1, n2);
    }
}

/* 根据编码表得到其解码表 */
void cipher_EncToDecTable(const unsigned char strEncTable[0x100], unsigned char strDecTable[0x100])
{
    int i;
    for(i = 0; i < 0x100; i++)
    {
        strDecTable[strEncTable[i]] = i;
    }
}

/* 打印出表内数据 */
void cipher_PrintTable(unsigned char strTable[0x100])
{
    int i, j, count;
    count = 0;

    printf("unsigned char Table[] = \n{\n");
    for(i = 0; i < 0x10; i++)
    {
        printf("\t");
        for(j = 0; j < 0x10; j++)
        {
            printf("0x%02X, ", strTable[count]);
            count++;
        }
        printf("\n");
    }
    printf("};\n");
}

/*
 * 根据给的表的不同进行数据编码(解码)
 * 其实, 编码表和解码表的意义是相同的,
 * 编码表可以用来解码, 解码表可以用来编码,
 * 只是叫法不同而已.
 */

void cipher_Coding(const unsigned char *src, unsigned char *dst, int len, const unsigned char strTable[0x100])
{
    int i;
    for(i = 0; i < len; i++)
    {
        dst[i] = strTable[src[i]];
    }
}

void main()
{
    unsigned char tblEnc[0x100], tblDec[0x100];
    cipher_GenEncTable(tblEnc);                /* 生成编码表 */
    cipher_PrintTable(tblEnc);

    cipher_EncToDecTable(tblEnc, tblDec);    /* 根据编码表生成解码表 */
    cipher_PrintTable(tblDec);

    char *str = "你好你是谁?我也不知道";
    printf("原文: %s\n", str);

    char strdst[50] = {0};
    cipher_Coding((unsigned char*)str, (unsigned char*)strdst, 23, tblEnc);
    printf("密文: %s\n", strdst);

    cipher_Coding((unsigned char*)strdst, (unsigned char*)strdst, 23, tblDec);
    printf("明文: %s\n", strdst);

}
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
AES(Advanced Encryption Standard,高级加密标准)是一种对称加密算法,常用于保护数据的机密性。在C语言中,可以使用不同的库或算法实现AES加密解密。 一种实现AES的方式是使用OpenSSL库。OpenSSL是一个开源的密码库,提供了丰富的加密算法实现。在C语言中使用OpenSSL库实现AES加密解密的示例代码如下: ```c #include <stdio.h> #include <stdlib.h> #include <openssl/aes.h> void encrypt(const unsigned char *plaintext, int plaintext_len, const unsigned char *key, unsigned char *ciphertext) { AES_KEY aes_key; AES_set_encrypt_key(key, 128, &aes_key); AES_encrypt(plaintext, ciphertext, &aes_key); } void decrypt(const unsigned char *ciphertext, int ciphertext_len, const unsigned char *key, unsigned char *plaintext) { AES_KEY aes_key; AES_set_decrypt_key(key, 128, &aes_key); AES_decrypt(ciphertext, plaintext, &aes_key); } int main() { const unsigned char *plaintext = "Hello World!"; unsigned char ciphertext[AES_BLOCK_SIZE]; unsigned char decrypted_plaintext[AES_BLOCK_SIZE]; unsigned char key[AES_BLOCK_SIZE] = "0123456789abcdef"; encrypt(plaintext, AES_BLOCK_SIZE, key, ciphertext); printf("Ciphertext: "); for (int i = 0; i < AES_BLOCK_SIZE; i++) { printf("%02x ", ciphertext[i]); } printf("\n"); decrypt(ciphertext, AES_BLOCK_SIZE, key, decrypted_plaintext); printf("Decrypted plaintext: "); for (int i = 0; i < AES_BLOCK_SIZE; i++) { printf("%c", decrypted_plaintext[i]); } printf("\n"); return 0; } ``` 上述代码实现了AES加密与解密的功能。首先,在`encrypt`函中,使用`AES_set_encrypt_key`函设置加密密钥,并使用`AES_encrypt`函进行加密操作。然后,在`decrypt`函中,使用`AES_set_decrypt_key`函设置解密密钥,并使用`AES_decrypt`函进行解密操作。 在`main`函中,定义了需要加密的明文、密文和解密后的明文,以及密钥。调用`encrypt`函进行加密,并打印密文。然后调用`decrypt`函进行解密,并打印解密后的明文。 当然,这只是一种C语言实现AES的方式,还有其他的库或算法可以实现AES加密解密,具体实现方式可能略有不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值