C中的Vigenere Square解密

时间:2015-10-30 17:07:56

标签: c arrays encryption vigenere

我正在尝试使用存储在数组中的密码和vigenere square来加密和解密消息。我能够成功加密消息但是我无法知道如何再次解密它。有任何想法吗?

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

void encrypt(char *text, char *pass, int n, int m, char *vs);
void decrypt(char *text, char *pass, int n, int m, char *vs);
void vigsq(char *vs);

main()
{
     char text[] = "HELLOWORLD";
     char pass[] = "MAGIC";
     int n, m;
     char vs[26*26];
     char *vsPointer, *textPointer, *passPointer;
     char command;
     char inputfile[10];
     char outputfile[10];
     //printf("Please enter e/d, pass phrase, input file, output file:");
     //scanf("%c, %s, %s, %s", &command, &pass, &inputfile, &outputfile);
     vsPointer = vs;
     textPointer = text;
     passPointer = pass;
     vigsq(vsPointer);
     //printf("%c, %s, %s, %s\n", command, pass, inputfile, outputfile);

    n = strlen(text);
    m = strlen(pass);
    //printf("%d, %d", n, m);
    encrypt(textPointer, passPointer, n, m, vsPointer);
    printf("%s\n", text);
    decrypt(textPointer, passPointer, n, m, vsPointer);
    printf("%s\n", text);   
}

void encrypt(char *text, char *pass, int n, int m, char *vs)
{
    int i;
    int ascii1;
    int ascii2;
    int passcount = 0;
    char encrypt;
    for (i = 0; i < n; i++)
    {
        ascii1 = (int)text[i];
        ascii2 = (int)pass[passcount];
        ascii1 = ascii1 - 64;
        ascii2 = ascii2 - 64;
        encrypt = vs[((ascii1 -1)*26) + (ascii2)];
        // printf("%d, %d, %c\n", ascii1, ascii2, encrypt);
        text[i] = encrypt;
        passcount++;
        if (passcount == m)
        {
            passcount = 0;
        }
    }   
}

void decrypt(char *text, char *pass, int n, int m, char *vs)
{
    int i;
    int ascii1;
    int ascii2;
    int passcount = 0;
    char decrypt;
    for (i = 0; i < n; i++)
    {
        ascii1 = (int)text[i];
        ascii2 = (int)pass[passcount];
        ascii1 = ascii1 - 64;
        ascii2 = ascii2 - 64;
        decrypt = vs[//Don't know what to put here];
        //printf("%d, %d, %c\n", ascii1, ascii2, decrypt);
        text[i] = decrypt;
        passcount++;
        if (passcount == m)
        {
            passcount = 0;
        }
    }       
}

void vigsq(char *vs)
{
    char alphabet[] = {'A','B','C','D','E','F','G','H','I','J','K',
        'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

    int a = 0, i, j, count = 0;
    for(i = 1; i <= 26*26; i++)
    {                           
        vs[i] = alphabet[a];
        a++;
        if (i % 26 == 0)
        {
            count++;
            a = count;
        }   
        if (a == 26)        
        {
            a = 0;
        }               
    }
}

(请忽略稍后我将实施的I / O内容。) 谢谢你的帮助:)

1 个答案:

答案 0 :(得分:1)

当您对字母进行编码时,您会在密码字母m[j]的行中查找字母,并在Vigenère表中查找明文字母p[i]的列。解码时,必须找到加密字母c[i]位于密码字母行中的列。在代码中,加密是一种简单的表查找,但解密不是。

但是你并不需要这张桌子。您可以通过算法描述Vigenère密码。密码真的只是一个Ceasar密码 - 一个简单的字母移位&amp; ndash,其中移位金额由密码中的当前字母决定。加密意味着向前移动,解密意味着向后移动:

c[i] = (p[i] + m[j]) % 26
p[i] = (c[i] - m[j]) & 26

在该公式中,字母由数字0(A)至25(Z)表示。请记住,索引从C开始为零。这也意味着你必须从你的字母中减去'A'(65)​​,而不是64。

因为解密和加密非常相似,所以你可以在一个函数中实现它们,并带有操作模式的标志:

enum {
    Encrypt, 
    Decrypt
};                    

void vigenere(char *text, const char *pass, int mode)
{
    int i;
    int passcount = 0;

    for (i = 0; text[i] != '\0'; i++)
    {
        int ascii1 = text[i] - 'A';
        int ascii2 = pass[passcount] - 'A';

        // Negate shift for decryption
        if (mode == Decrypt) ascii2 = -ascii2;

        text[i] = 'A' + (26 + ascii1 + ascii2) % 26;

        passcount++;
        if (pass[passcount] == '\0') passcount = 0;
    }
}

注意事项:

  • 在公式中添加26是为了防止采用负数的余数,这将在C中产生负数。
  • 只有当所有字符都是大写字母时,代码才有效。
  • 省略了显式字符串长度;您可以通过检查当前字符是否为终止空值'\0'来测试您是否已到达C字符串的末尾。

像这样调用此代码:

int main()
{
    char text[] = "HELLOWORLD";
    const char *pass = "MAGIC";

    printf("plain %s\n", text);    

    vigenere(text, pass, Encrypt);
    printf("crypt %s\n", text);

    vigenere(text, pass, Decrypt);
    printf("decpt %s\n", text); 

    return 0;  
}