Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ct3-crypto-keys.c
- Israel Torres hakin9@israeltorres.org
- Sun Aug 14 15:37:12 PDT 2011
- "Cryptotutorials #3 Cryptography fundamentals - Crypto with Keys"
- demo to:
- associate letters to numbers, generate key and encrypt/decrypt w/key
- compile with:
- gcc ct3-crypto-keys.c -o ct3-crypto-keys && ./ct3-crypto-keys
- created and tested on gcc version 4.2.1 / Mac OS X 10.7 11A511
- */
- #include <time.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define ALPHABET 26 // the size of the alphabet used
- #define ALPHAUPP 65 // using ASCII 65-90 for alphabet
- #define KEYBUFFR 50 // the size of the keystream
- int main (int argc, char * argv[])
- {
- srand((unsigned)time(NULL)); // seed the 'randomizer'
- // test string
- char teststring[]="THESECRETMESSAGE"; int counter=0;
- printf("teststring: [%s]\n",teststring);
- for (;counter < strlen(teststring); counter++){
- printf("%d\t",teststring[counter]-ALPHAUPP);
- printf("%c\n",teststring[counter]);
- }
- // genereate pseudo-random keystream
- printf("generating keystream: [");
- int random=0; counter=0; int keystream[KEYBUFFR]={0};
- for (;counter < strlen(teststring); counter++){
- keystream[counter]=rand()%9; // keystream to contain values 0 - 9
- printf("%d",keystream[counter]);
- }
- printf("]\n");
- // encrypt test string
- printf("encrypting...\n");
- char encrypted[KEYBUFFR]="\0"; counter=0;
- for (;counter < strlen(teststring); counter++){
- printf("%d\t",teststring[counter]-ALPHAUPP+keystream[counter]);
- encrypted[counter]=teststring[counter]+keystream[counter];
- printf("%c\n",encrypted[counter]);
- }
- printf("encrypted string: [%s]\n",encrypted);
- // decrypt encrypted test string
- printf("decrypting...\n");
- char decrypted[KEYBUFFR]="\0"; counter=0;
- for (;counter < strlen(encrypted); counter++){
- printf("%d\t",encrypted[counter]-ALPHAUPP-keystream[counter]);
- decrypted[counter]=encrypted[counter]-keystream[counter];
- printf("%c\n",decrypted[counter]);
- }
- printf("decrypted string: [%s]\n",decrypted);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement