最近在读《图解密码技术(日)结城浩(著).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=26)
j-=26;
dest[i] = src[j];
}
return 0;
}
int CaesarPassword_deCode(char *buf)
{
int i;
char deCode[26];
char tempBuf[500];
for (i=0; i<26; i++)
{
memset(&tempBuf, 0, sizeof(tempBuf));
GenerateDecTable(deCode, alphabet, i);
// Calculate clear
CaesarPassword_DEC(buf, tempBuf, deCode, strlen(buf));
printf("\nout deCode(%d):\n%s\n\n", i, tempBuf);
}
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, decTable, strlen(encData));
printf("\nout DEC Data:\n%s\n\n", decData);
if (memcmp(data, decData, strlen(data)) != 0)
printf("\n\nError\n\n");
CaesarPassword_deCode(encData);
}
return 0;
}
可见只需 26 次解密,就一定能有一次成功,机密性太脆弱。
please input key:
3
please input data:
saji
out ENC Data:
vdml
out DEC Data:
saji
out deCode(0):
vdml
out deCode(1):
uclk
out deCode(2):
tbkj
out deCode(3):
saji
out deCode(4):
rzih
out deCode(5):
qyhg
out deCode(6):
pxgf
out deCode(7):
owfe
out deCode(8):
nved
out deCode(9):
mudc
out deCode(10):
ltcb
out deCode(11):
ksba
out deCode(12):
jraz
out deCode(13):
iqzy
out deCode(14):
hpyx
out deCode(15):
goxw
out deCode(16):
fnwv
out deCode(17):
emvu
out deCode(18):
dlut
out deCode(19):
ckts
out deCode(20):
bjsr
out deCode(21):
airq
out deCode(22):
zhqp
out deCode(23):
ygpo
out deCode(24):
xfon
out deCode(25):
wenm
please input key: