美文网首页
维吉尼亚加密

维吉尼亚加密

作者: 比轩 | 来源:发表于2015-11-03 21:19 被阅读157次

实验要求:

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

维吉尼亚替换表.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;
}

相关文章

  • 维吉尼亚加密

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

  • 密码那些事儿(十)

    在说维吉尼亚加密法的破解方法之前,有必要来回顾一下它的加密原理。 维吉尼亚加密法是由26套密码组成的表,我们默认要...

  • 维吉尼亚密码加密文件

    一.维吉尼亚密码加密文本文件 要求:用维吉尼亚密码实 现加密任意文本文件,注意用控制台方式实现。输入格式:>-e/...

  • 维吉尼亚密码:加密强悍,却为何没人用?

    维吉尼亚加密法。 讲到概率论出现之后,传统的移位法就不好使了。 维吉尼亚加密法,就是为了对抗频率分析出现的——使用...

  • 维吉尼亚密码

    维吉尼亚密码是以法国外交官、密码学家布莱斯·德·维吉尼亚的名字命名的,不过不是他本人发明的。 【加密原理】...

  • 密码那些事儿(十三)

    维吉尼亚2.0版本被破解后,加密一方继续改进,改进的方向就是修补了2.0版本的漏洞。 之前我们说过,维吉尼亚2.0...

  • 维吉尼亚加密遇到的问题

    1. c-型array赋值给c++string类型: 另一种方式 另外string型中查找字符或短字符串用find...

  • 字母类语言:为什么在加密上有基因优势?

    前面讲了替代法、移位法、维吉尼亚加密法,以及它们对应的破解法。 现在有个问题: 这些加密法和解密法进化的动力是什么...

  • 简单的加密算法——维吉尼亚密码

    学号:16030140019 姓名: 莫益彰 【嵌牛导读】:凯撒密码是一种简单的加密方法,即将文本中的每一个字符都...

  • 维吉尼亚密码加密解密对照表

    采用替代密码算法中的维吉尼亚密码方法,密文C=“HEADVIGENERE”,密钥K=KEY,求明文P 将密文HEA...

网友评论

      本文标题:维吉尼亚加密

      本文链接:https://www.haomeiwen.com/subject/nkiqhttx.html