Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- char menu(void);
- char action(char option);
- void encypt();
- void decrypt();
- char menu(void){
- char option;
- printf("Pick whether you would like to enycrypt a text or decrypt a text.\n");
- printf("1 - encrypt\n");
- printf("2 - decrypt\n");
- printf("x - end");
- scanf(" %c", &option);
- return option;
- }
- char action(char option){
- switch(option){
- case '1':
- encypt();
- case '2':
- decrypt();
- }
- }
- void encrypt(){
- char message_1[100], ch,message_2[100];
- int i, key;
- printf("Enter a message to encrypt: ");
- fgets(message_1,100,stdin);
- printf("Enter key: ");
- scanf("%d", &key);
- for(i = 0; message_1[i] != '\0'; ++i){
- ch = message_1[i];
- if(ch >= 'a' && ch <= 'z'){
- ch = ch + key;
- if(ch > 'z'){
- ch = ch - 'z' + 'a' - 1;
- }
- message_1[i] = ch;
- }
- else if(ch >= 'A' && ch <= 'Z'){
- ch = ch + key;
- if(ch > 'Z'){
- ch = ch - 'Z' + 'A' - 1;
- }
- message_1[i] = ch;
- }
- }
- printf("Encrypted message: %s", message_1);
- }
- void decrypt(){
- char ch,message_2[100];
- int i, key;
- printf("Enter a message to decrypt: \n");
- fgets(message_2,100,stdin);
- for(i = 0; message_2[i] != '\0'; ++i){
- ch = message_2[i];
- if(ch >= 'a' && ch <= 'z'){
- ch = ch - key;
- if(ch < 'a'){
- ch = ch + 'z' - 'a' + 1;
- }
- message_2[i] = ch;
- }
- else if(ch >= 'A' && ch <= 'Z'){
- ch = ch - key;
- if(ch < 'A'){
- ch = ch + 'Z' - 'A' + 1;
- }
- message_2[i] = ch;
- }
- }
- printf("Enter key: ");
- scanf("%d", &key);
- printf("Decrypted message: %s", message_2);
- }
- int main(){
- char option;
- printf("Encryption and decription program\n");
- printf("-------------------------------------\n");
- do{
- while(option != 'x'){
- if(option == '1'){
- do(encypt());
- }else{
- do(decrypt());
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement