实验要求:

实现维吉尼亚密码,用键盘接收明文和密钥,屏幕打印密文和解密后的明文。大小写铭感,明文小写则密文大写,明文大写则密文小写。

维吉尼亚替换表.jpg

实际问题:

接收密文的字符串少申请了一位空间,导致空间控制的一个bug,花了一个小时才定位到问题,之前完全没有想到是这个问题。大概就是因为少了一位空间,导致\0被覆盖,然后编译器帮我扩展了五个字节,导致这五个字没有初始化,所以输出密文的时候后面总是有莫名其妙的字符出现。

核心代码实现:
ciphertext= (char*)calloc(plainlen+1, sizeof(char));
...
for (int j = 0 ; j < plainlen ; j++) //加密过程
{
if (plaintext[j] >= 65 && plaintext[j] <= 90)
ciphertext[j] = ((plaintext[j] - 65) + (key[j] - 65) % 26 + 97);
else if(plaintext[j] >= 97 && plaintext[j] <= 122)
ciphertext[j] = ((plaintext[j] - 97) + (key[j] - 65) % 26 + 65);
else
ciphertext[j] = plaintext[j];
}
源码:
// 维吉尼亚密码加密-键盘接收加密解密
//**2015-9-24**
#include "stdafx.h"
#include <iostream>
using namespace std;
int array_num = 500;
void encipher(char [], char [], char []);
void decipher(char [], char [], char []);
int main()
{
    int plainlen,TEMP;              //申请原文,密文,密钥的空间并初始化
    char *plaintext,*key,*ciphertext;
    plaintext = (char*)calloc(array_num, sizeof(char));
    key = (char*)calloc(array_num, sizeof(char));
    cout << "输入密文" << endl;
    cin.getline(plaintext, array_num);
    cout << "输入key" << endl;
    cin.getline(key, array_num);
    plainlen = strlen(plaintext);
    ciphertext= (char*)calloc(plainlen+1, sizeof(char));  //密文的空间由输入明文的长度确定
    encipher(plaintext, key, ciphertext);//加密
    int cipherlen = strlen(ciphertext);
    decipher(ciphertext, key, plaintext);//解密
    system("pause");
    return 0;
}
void encipher(char plaintext[], char key[], char ciphertext[])
{
    cout << "-------------------加密过程-------------------" <<           endl;
    cout <<"***原文***\n"<< plaintext << endl << endl;
    char temp;
    int keylen = strlen(key);
    int plainlen = strlen(plaintext);
    for (int i = 0; i < plainlen; i++)  //处理密钥字符串全部为大写,长度和明文以及密文长度相同
    {
        if (key[i] >= 97 && (key[i] <= 122))
        {
            temp = key[i%keylen] - 32;
            key[i] = temp;
        }
        else
        key[i] = key[i%keylen];
    }
for (int j = 0 ; j < plainlen ; j++) //加密过程
{
    if (plaintext[j] >= 65 && plaintext[j] <= 90)
        ciphertext[j] = (((plaintext[j] - 65) + (key[j] - 65)) % 26 + 97);
    else if(plaintext[j] >= 97 && plaintext[j] <= 122)
        ciphertext[j] = (((plaintext[j] - 97) + (key[j] - 65)) % 26 + 65);
    else
        ciphertext[j] = plaintext[j];
    }
    cout <<"***密文***\n"<< ciphertext << endl;
}
void decipher(char ciphertext[], char key[], char plaintext[])
{
    cout << "-----------------解密过程--------------------"<<endl;
    cout << "***密文***\n" << ciphertext << endl << endl;
    //cout << "***key***\n" << key << endl << endl;
    int cipherlen = strlen(ciphertext);
    for (int j = 0; j < cipherlen; j++) //解密过程
    {
        if (ciphertext[j] >= 65 && ciphertext[j] <= 90)
                plaintext[j] = (((ciphertext[j] - 65+26) - (key[j] - 65))%26 + 97);
        else if (ciphertext[j] >= 97 && ciphertext[j] <= 122)
            plaintext[j] = (((ciphertext[j] - 97+26) - (key[j] - 65))%26 + 65);
        else
            plaintext[j] = ciphertext[j];
 }
    cout << "***原文***\n" << plaintext << endl;
}