주석좀 부탁드릴게요;;;
핑1크캣
S-DES 구현 관련해서... 해석좀 부탁드릴게요!!
#include stdio.h
#include stdlib.h
#include string.h
#include time.h
#define KEY_SIZE 10
#define BLOCK_SIZE 8
#define WORD2BYTE(w,b) \
b[1] = w 8; \
b[0] = w;\
1typedef unsigned char BYTE;
typedef unsigned short WORD;
void
SDES_Random_Key_Generation(BYTE* pbKey)
{
const WORD wMod = (1 9);
WORD wKey = 0;
/* 1. initialize random seed */
srand(time(0));
/* 2. generate a random value */
wKey = rand() % wMod;
/* 3. move a word to two-byte array */
WORD2BYTE(wKey, pbKey);
#ifdef __Debug
printf([1]%02x%02x\n, pbKey[1], pbKey[0]);
#endif
return;
}
void
SDES_P10(BYTE* pbKey)
{
BYTE pbPermKey[2] = {0};
BYTE Temp = {0};
#ifdef __Debug
printf([2]%02x%02x\n, pbKey[1], pbKey[0]);
#endif
Temp = pbKey[0] & 0x80;
pbPermKey[1] = pbPermKey[1] | (Temp 6); // 3
Temp = pbKey[0] & 0x20;
pbPermKey[1] = pbPermKey[1] | (Temp 5); // 5
Temp = pbKey[1] & 0x01;
pbPermKey[0] = pbPermKey[0] | (Temp 7); // 2
Temp = pbKey[0] & 0x08;
pbPermKey[0] = pbPermKey[0] | (Temp 3); // 7
2Temp = pbKey[0] & 0x40;
pbPermKey[0] = pbPermKey[0] | (Temp 1); // 4
Temp = pbKey[0] & 0x01;
pbPermKey[0] = pbPermKey[0] | (Temp 4); // 10
Temp = pbKey[1] & 0x02;
pbPermKey[0] = pbPermKey[0] | (Temp 2); // 1
Temp = pbKey[0] & 0x02;
pbPermKey[0] = pbPermKey[0] | (Temp 1); // 9
Temp = pbKey[0] & 0x04;
pbPermKey[0] = pbPermKey[0] | (Temp 1); // 8
Temp = pbKey[0] & 0x10;
pbPermKey[0] = pbPermKey[0] | (Temp 4); // 6
pbKey[1] = pbPermKey[1];
pbKey[0] = pbPermKey[0];
#ifdef __Debug
printf([3]%02x%02x\n, pbKey[1], pbKey[0]);
#endif
return;
}
void
SDES_LS1(BYTE* pbData)
{
return;
}
void
SDES_P8(const BYTE LK, const BYTE RK, BYTE* K1)
{
return;
}
/*************************************************************************
* Be careful at pbKey is 2-byte, but pbK1 and pbK2 are 1 byte
*************************************************************************
*/
void
3SDES_Key_Generation(BYTE* pbKey, BYTE* pbK1, BYTE* pbK2)
{
/* 1. Generate a random key for encryption and decryption */
SDES_Random_Key_Generation(pbKey);
/* 2. P10 */
SDES_P10(pbKey);
/* 3. LS1 */
BYTE LK = 0, RK = 0;
LK = (pbKey[1] 3) | (pbKey[0] 5);
RK = pbKey[0] & 0x1F;
SDES_LS1(&LK);
SDES_LS1(&RK);
/* 4. P8 */
*pbK1 = 0;
SDES_P8(LK, RK, pbK1);
/* 5. LS2 */
SDES_LS1(&LK);
SDES_LS1(&RK);
/* 6. P8 */
*pbK2 = 0;
SDES_P8(LK, RK, pbK2);
return;
}
void
SDES_Encryption(const BYTE bK1, const BYTE bK2, const BYTE bPlaintext, BYTE* pbCiphertext)
{
BYTE L = 0, //(bPlaintext 4),
R = 0; //(bPlaintext & 0x0F);
/* 1. IP
* 1 2 3 4 5 6 7 8
4* 2 6 3 1 4 8 5 7
*/
BYTE Temp = 0;
Temp = Temp | ((bPlaintext & 0x40) 1);
Temp = Temp | ((bPlaintext & 0x04) 4);
Temp = Temp | ((bPlaintext & 0x20) 0);
Temp = Temp | ((bPlaintext & 0x80) 3);
Temp = Temp | ((bPlaintext & 0x10) 1);
Temp = Temp | ((bPlaintext & 0x01) 2);
Temp = Temp | ((bPlaintext & 0x08) 2);
Temp = Temp | ((bPlaintext & 0x02) 1);
L = Temp 4;
R = Temp & 0x0F;
#ifdef __Debug
printf(L=%02x, R=%02x\n, L, R);
#endif
/* 2. E/P */
BYTE bEP = 0;
/* 3. S0 and S1 */
/* 4. P4 */
/* 5. SW */
/* 6. E/P */
/* 7. S0 and S1 */
/* 8. P4 */
/* 9. IPˆ{-1} */
return;
}
void
SDES_Decryption(const BYTE bK1, const BYTE bK2, const BYTE pbCiphertext, BYTE* pbPlaintext)
5{
return;
}
int
main(void)
{
const BYTE* pbPlaintext = Information Security Experiment Third;
BYTE bKey[2] = {0};
BYTE bK1 = 0, bK2 = 0;
BYTE* pbMessage = 0;
BYTE* pbCiphertext = 0;
int i = 0;
int nPlaintextSize = strlen(pbPlaintext);
BYTE test = 0xb5;
/* Key generation */
SDES_Key_Generation(bKey, &bK1, &bK2);
/* Encryption */
pbCiphertext = (BYTE *)malloc(nPlaintextSize);
if (0 == pbCiphertext)
return -1;
memset(pbCiphertext, 0, nPlaintextSize);
for (i = 0; i nPlaintextSize; i++) {
SDES_Encryption(bK1, bK2, pbPlaintext[i], &pbCiphertext[i]);
}
/* Decryption */
pbMessage = (BYTE *)malloc(nPlaintextSize);
if (0 == pbMessage)
return -1;
memset(pbMessage, 0, nPlaintextSize);
for (i = 0; i nPlaintextSize; i++) {
SDES_Decryption(bK1, bK2, pbCiphertext[i], &pbMessage[i]);
}
/* Compare two results */
if (0 != memcmp(pbPlaintext, pbMessage, nPlaintextSize)) {
printf(Decryption failure\n);
return -1;
6}
printf(Decryption Ok!\n);
return 1;
}
구현을 완성시켜야하는데 너무 어렵네요..