Vigenere如何停止增量

时间:2017-04-28 18:12:07

标签: c increment cs50 vigenere

#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>



int main(int argc, string argv[])
{
//string for chiper text
        char s[100];

//exit if no argument or more than one    
        if( argc != 2 ) 
       {
          printf("One argument expected.");
        return 1;
       }
//check the key string, exit if it's not alphabetic   
    for (int i = 0; i < argc; i++)
    {
        for (int j = 0, n = strlen(argv[1]); j < n; j++)
        {
            if (isalpha(argv[1][j]))
            {
            }
           else
           {
           printf("Key from a command-line-argument should be an 
                   alphabetic");
           return 1;
           }
        }

    }
//this will keep key
       int k;
//command line argument as a key string      
       string key = argv[1]; 

            printf("plaintext:  ");

            string p = get_string();   //plain text, string

            printf("chiper text: ");



            for (int i = 0, n=strlen(p); i < n; i++)    //iterate while 
                                                         plaintext ends
            {

                       for (int j = 0, x=i%strlen(key);  j <= x && 
                       isalpha(p[i]); j++ )  //iterate for leight of key 
                                            string and if p[i] alphabetic
                    {
                        if(islower(key[j]))
                        {
                        k=(int)key[j]-97;   
                        }
                    }

                            if (isalpha(p[i]))
                            {

                           //encripts capital letters
                                                     if (isupper(p[i])) 
                                                    {
                                                        s[i] = p[i]+k;
                                                        if (s[i] > 90)
                                                        {
                                                            s[i] = s[i]-26;
                                                        }
                                                    }

                                    //encripts lower case letter
                                                    if  (islower(p[i]))
                                                    {
                                                        s[i] = p[i]+k;
                                                        if (p[i]+k > 122)
                                                        {
                                                            s[i] = 
                                                        (p[i]+k)-26;
                                                        }
                                                     }   
                            }  
                            //don't encript non alphabetic character
                            else
                            {
                               s[i] = p[i];
                            }   
            printf("%c", s[i]);
            }
         return 0;   
}                                                                                

尝试解决问题set2。凯撒很容易,缩写 - 和平的蛋糕。但是Vigenere - 哦,它让我发疯了! 如果没有字母,我找不到增加密钥的方法。 J一次又一次地迭代。我尝试了很多方法,现在只是卡住了。请帮忙。

1 个答案:

答案 0 :(得分:2)

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

typedef char* string; 


int main(int argc, string argv[]) {
    if (argc != 2) {
        printf("how to run: file_name keyword\n");
        return 1;
    }

    string key = argv[1];
    int k, key_length = strlen(key);
    for (k = 0; k < key_length; k++) {
        key[k] = tolower(key[k]) - 97; 
    }   

    printf("insert to encrypt:\n");
    char text[100];
    scanf("%[^\n]", text);
    printf("before: %s\n",text);

    int i, text_length = strlen(text);
    int ascii_before, alpha_before, alpha_after, ascii_after;
    for ( i= 0; i < text_length; i++) {
        if (isalpha(text[i])) {
            if (islower(text[i])) {//lower case
                ascii_before = text[i];
                alpha_before = ascii_before - 'a';
                //caesar formula
                alpha_after = (alpha_before + key[i%key_length]) % 26;
                ascii_after = alpha_after + 'a';
                text[i] = ascii_after;          
            } else { //upper case
                ascii_before = text[i];
                alpha_before = ascii_before - 'A';
                //caesar formula
                alpha_after = (alpha_before + key[i%key_length]) % 26;
                ascii_after = alpha_after + 'A';    
                text[i] = ascii_after;
            }
        }       
    }   

    printf("after: %s\n",text);
    return 0;
}