Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int is_lower(char c) {
- return (c >= 'a' && c <= 'z');
- }
- int is_upper(char c) {
- return (c >= 'A' && c <= 'Z');
- }
- char *encrypt_with_number_key(char *str, int key) {
- int i = 0;
- while(str[i])
- {
- if(is_lower(str[i])){
- str[i] = (str[i] - 'a' + key)%26 + str[i];
- }
- else if(is_upper(str[i])){
- str[i] = (str[i] - 'A' + key)%26 + str[i];
- }
- i++;
- }
- return str;
- }
- char *encrypt_with_string_key(char *str, char key[]){
- int i = 0;
- while(str[i])
- {
- if(is_lower(str[i])){
- str[i] = str[i] + ((str[i] - 'a') +
- is_lower(key[i%strlen(key)]) ? key[i%strlen(key)] - 'a' : is_upper(key[i%strlen(key)]) ? key[i%strlen(key)] - 'A': 0)%26 ;
- }
- else if(is_upper(str[i])){
- str[i] = str[i] + ((str[i] - 'A') +
- is_lower(key[i%strlen(key)]) ? key[i%strlen(key)] - 'a' : is_upper(key[i%strlen(key)]) ? key[i%strlen(key)] - 'A': 0)%26 ;
- }
- i++;
- }
- return str;
- }
- int main(int argc, char **argv)
- {
- if(argc == 4)
- {
- if(atoi(argv[3]) == 0)
- {
- char *s = encrypt_with_number_key(&argv[1][0], atoi(argv[2]));
- printf("String:%s\nCipher:%s\n",argv[1], s);
- }
- if(atoi(argv[3]) == 1)
- {
- char *s = encrypt_with_string_key(&argv[1][0], argv[2]);
- printf("String:%s\nCipher:%s\n",argv[1],s);
- }
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement