本文共 1748 字,大约阅读时间需要 5 分钟。
最近在读《图解密码技术(日)结城浩(著).pdf》,想着为后续增加更深刻的印象与密码学算法的理解
#include
#include
#include
char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char decTable[26] = {0x00};
int CaesarPassword_ENC(char *dataBuf, char *passwordBuf, int dataLen)
{
int i, j;
for (i=0; i
{
for (j=0; j<26; j++)
{
if (dataBuf[i] == alphabet[j])
{
passwordBuf[i] = decTable[j];
break;
}
}
}
return 0;
}
int CaesarPassword_DEC(char *passwordBuf, char *dataBuf, int dataLen)
{
int i, j;
for (i=0; i
{
for (j=0; j<26; j++)
{
if (passwordBuf[i] == decTable[j])
{
dataBuf[i] = alphabet[j];
break;
}
}
}
return 0;
}
int GenerateDecTable(char *dest, char *src, int step)
{
int i, j;
for (i=0,j=0; i<26; i++)
{
j=i+step;
if (j>=26)
j-=26;
decTable[i] = alphabet[j];
}
return 0;
}
int main(void)
{
int key;
char data[500];
char encData[500];
char decData[500];
while(1)
{
key = 0;
memset(&decTable, 0, sizeof(decTable));
memset(&data, 0, sizeof(data));
memset(&encData, 0, sizeof(encData));
memset(&decData, 0, sizeof(decData));
printf("-----------------------------------------------------\n");
// Negotiate key
printf("please input key:\n");
scanf("%d", &key);
GenerateDecTable(decTable, alphabet, key);
// get data
printf("\nplease input data:\n");
scanf("%s", &data);
// Calculation of cipher
CaesarPassword_ENC(data, encData, strlen(data));
printf("\nout ENC Data:\n%s\n", encData);
// Calculate clear
CaesarPassword_DEC(encData, decData, strlen(encData));
printf("\nout DEC Data:\n%s\n\n", decData);
if (memcmp(data, decData, strlen(data)))
printf("\n\nError\n\n");
}
return 0;
}
标签:decData,解密,C语言,char,key,printf,data,凯撒,encData
来源: https://blog.csdn.net/Dennis_Sck/article/details/88890572