Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- //#include <conio.h> Need in Windows
- char* encryption(char* str, char* key)
- {
- int i = 0, j = 0;
- char* crypted = (char*) malloc(sizeof(str));
- if(strlen(key) > strlen(str))
- {
- printf("\nThe key cannot be larger than the string itself.");
- return NULL;
- }
- else
- {
- while(str[i] != '\0')
- {
- for(j = 0; j < strlen(key); j++)
- {
- if(j == (strlen(str)))
- continue;
- else
- {
- crypted[i] = str[i] ^ key[j];
- i++;
- }
- }
- }
- }
- return crypted;
- }
- int main()
- {
- char str[50], key[50];
- int i = 0;
- char* strptr;
- //clrscr(); Need this in Windows
- printf("Enter the string to be encrypted -: ");
- gets(str);
- printf("\nEnter the key to be used -: ");
- gets(key);
- printf("\nString in hex -: ");
- for(i = 0; str[i] != '\0'; i++)
- printf("%x", str[i]);
- strptr = encryption(str, key);
- if(strptr == NULL)
- {
- printf("\nEncryption failed.\n");
- //getch(); Need this in Windows
- exit(1);
- }
- printf("\nThe encrypted string in hexadecimal is -: ");
- for(i = 0; strptr[i] != '\0'; i++)
- printf("%x", strptr[i]);
- strptr = encryption(strptr, key);
- printf("\nDecrypting -: ");
- for(i = 0; strptr[i] != '\0'; i++)
- printf("%c", strptr[i]);
- printf("\n");
- //getch(); Need this in Windows
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement