Advertisement
InfernalSpeed

Untitled

Feb 27th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string.h>
  4.  
  5. #define MESSAGE_MAX_LENGHT 100;
  6.  
  7. int encrypt(int value,int key);
  8. int decrypt(int value,int key);
  9. void encryptMessage(char* input, int key);
  10. void decryptMessage(char* input, int key);
  11.  
  12. int main(){
  13. int key;
  14. char input[MESSAGE_MAX_LENGHT];
  15.  
  16. printf("Introdu mesajul: ");
  17. gets(input);
  18.  
  19. printf("Indou cheia:");
  20. scanf("%d",&key);
  21.  
  22. encryptMessage(input, key);
  23. printf("Mesajul criptat: %s\n", input);
  24.  
  25. decryptMessage(input, key);
  26. printf("Mesajul decriptat: %s\n", input);
  27.  
  28.  
  29. }
  30.  
  31. int encrypt(int value, int key){
  32. int result=(value+key)%26;
  33. return result;
  34. }
  35.  
  36. int decrypt(int value, int key){
  37. int result=(value-key+26)%26;
  38. return result;
  39. }
  40.  
  41. void encryptMessage(char * input, int key){
  42. char message[MESSAGE_MAX_LENGHT];
  43. strcpy(message,input);
  44.  
  45. int index=0;
  46. while(message[index] != '\0'){
  47. if(message[index] != ' ')
  48. if(message[index] <= 'Z' && message[index] >='A')
  49. input[index]=encrypt(message[index] - 'A' ,key) + 'A';
  50. else input[index]=encrypt(message[index] - 'a', key) + 'a';
  51. index++;
  52. }
  53. }
  54.  
  55. void decryptMessage(char * input, int key){
  56. char message[MESSAGE_MAX_LENGHT];
  57. strcpy(message,input);
  58. int index=0;
  59. while(message[index] != '\0'){
  60. if(message[index] != ' ')
  61. if(message[index] <= 'Z')
  62. input[index] = decrypt(message[index] - 'A', key)+'A';
  63. else input[index] =decrypt(message[index] - 'a', key)+'a';
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement