수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

비트연산을하려고 합니다.

황소눈

2023.04.01

암호화 과정을 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;
}

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

번호 제 목 글쓴이 날짜
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
2699444 [좀 급함] system("explorer [주소] ") 문에 변수를 사용할 수 있나요? 알 2025-06-26
2699415 파일//read//와 배열 아란 2025-06-25
2699386 구조체 안에 일부분만 char 배열에 복사하려면 어떻게 해야하나요? (1) 미즈 2025-06-25
2699361 연결리스트 정렬하는 부분에 대해서 질문 드립니다 아이처럼 2025-06-25
2699304 [기초]아직 안주무시는분 계신가요..?포인터배열? 좀 도와주세요. 놀리기 2025-06-24
2699272 printf() 함수이용해서 프로그램 만들기 질문요! (5) 다가 2025-06-24
2699221 PUSH와 POP코드를 더 간단하게 어떻게 해야할까요? 파라미 2025-06-24
2699192 설치오류가 자꾸 나요 한번봐주세여~ (1) 소녀틳향기 2025-06-23
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com