비트연산을하려고 합니다.
황소눈
암호화 과정을 c언어로 소스파일 만들고 싶은데요 s-des 입니다.
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;
typedef 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
temp = 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
sdes_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
* 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)
{
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;t 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;
}
printf(decryption ok!\n);
return 1;
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692401 | 유닉스안에서 C언어를 이용한 명함 만들기 입니다; 이해안가는 부분이있네요 | 2gether | 2025-04-22 |
2692374 | 고수님들 댓글 마니부탁해요!!! (2) | 엄지 | 2025-04-22 |
2692343 | scnaf에 자꾸 선언을 참조하라는데;; (8) | 도래 | 2025-04-22 |
2692282 | 도스상에서 생성된 exe파일에 press~ 뜨게 하기 (4) | 회사원 | 2025-04-21 |
2692256 | scanf("%*c"); ㅠㅠ 고수님들 | 거북이 | 2025-04-21 |
2692230 | 하노이탑 질문입니다. (1) | 미쁘다 | 2025-04-21 |
2692210 | 정보 올림피아드 문제인데.. 풀이 과정이 궁금합니다.(재귀함수) (5) | 물티슈 | 2025-04-20 |
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |