Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void encrypt(char* text, size_t text_len, char key){
- int i = 0;
- for (i = 0; i < text_len; i++){
- if (('a' <= text[i]) && (text[i] <= 'z')){
- text[i] = 'a' + (1 + (text[i] + key) % 'a') % 26;
- }
- else if(('A' <= text[i]) && (text[i] <= 'Z')){
- text[i] = 'A' + (1 + (text[i] + key + 'A' - 'a') % 'A') % 26;
- }
- }
- }
- void decrypt1(char* text, size_t text_len, char key){
- int i = 0;
- key = 'z' - key + 'a' + 1;
- for (i = 0; i < text_len; i++){
- if (('a' <= text[i]) && (text[i] <= 'z')){
- text[i] = 'a' + (text[i] + key - 1) % 'a' % 26;
- }
- else if(('A' <= text[i]) && (text[i] <= 'Z')){
- text[i] = 'A' + (text[i] + key + 'A' - 'a' - 1) % 'A' % 26;
- }
- }
- }
- void decrypt2(char* text, size_t text_len, char key){
- int i = 0;
- for (i = 0; i < text_len; i++){
- if (('a' <= text[i]) && (text[i] <= 'z')){
- text[i] = 'a' + ('z' + text[i] - key) % 'a' % 26;
- }
- else if(('A' <= text[i]) && (text[i] <= 'Z')){
- text[i] = 'A' + ('Z' + text[i] - key - 'A' + 'a') % 'A' % 26;
- }
- }
- }
- int main(){
- char text[256];
- size_t text_len;
- char k = 'c';
- scanf("%s", &text);
- text_len = strlen(text);
- encrypt(text, text_len, k);
- printf("Plain text: %s\n", text);
- decrypt1(text, text_len, 'c');
- printf("decrypt1\nCipher text: %s\n", text);
- encrypt(text, text_len, k);
- decrypt2(text, text_len, k);
- printf("decrypt2\nCipher text: %s\n", text);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment