주석좀 부탁드릴게요;;;
핑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;
}
구현을 완성시켜야하는데 너무 어렵네요..
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |
2700070 | while문 , 3의배수 출력하는 프로그램좀 짜주세욤. | 횃불 | 2025-07-01 |
2700041 | 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? | 헛장사 | 2025-07-01 |
2700012 | 배열// (1) | 전갈자리 | 2025-07-01 |
2699895 | 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) | 선아 | 2025-06-30 |
2699842 | 질문을 너무 많이 하네여.....죄송.... (2) | 해님꽃 | 2025-06-29 |
2699816 | 오류 질문입니다.. (1) | 해비치 | 2025-06-29 |
2699763 | 질문입니다 ! 꼭 좀 도와주세요ㅠㅠ (2) | 미라 | 2025-06-28 |
2699555 | c언어 다항식을 입력을 했는데 왜 출력이 안될까요? | 피스케스 | 2025-06-27 |
2699528 | C언어 포인터연산 질문입니다. (3) | 안녕나야 | 2025-06-26 |
2699476 | 끌어올림;;달력 짜봤는데요 이 소스 줄일 수 있나요? - 스샷첨부 (2) | 클라우드 | 2025-06-26 |