维吉尼亚密码简介和C/C++语言实现

简介

维吉尼亚密码是古典密码的一种,从凯撒密码演变而来,用多个凯撒密码组成,是多表密码的简单实现。

凯撒密码

在这里插入图片描述

维吉尼亚密码

在这里插入图片描述

特点

引进了密钥,是一种二维加密

加密流程

将A-Z的26个字母(不区分大小写)进行0~26编号,从第一位开始,每一位:密文=【密钥+明文】mod26。(密钥可循环)

解密流程

跟加密流程逆着来,每一位:明文=【密文-密钥】mod26。(最后取正)

代码

#include"iostream"
using namespace std;
#define MINCHAR 97
#define CHARSUM 26
char table[CHARSUM][CHARSUM];
bool Init();
bool Encode(char* key, char* source, char* dest);
bool Dncode(char* key, char* source, char* dest);
int main()
{
  if(!Init())
  {
      cout << "初始化错误!" << endl;
      return 1;
  }
  char key[256];
  char str1[256];
  char str2[256];
  int operation;
  while(1)
  {
      do
      {
        cout << "请选择一个操作:1. 加密;2. 解密;-1. 退出\n";
        cin >> operation;
      }while(operation != -1 && operation != 1 && operation != 2);
      if(operation == -1)
        return 0;
      else if(operation == 1)//加密
      {
        cout << "请输入密钥:";
        cin >> key;
        cout << "请输入待加密字符串:";
        cin >> str1;
        Encode(key, str1, str2);
        cout << "加密后的字符串:" << str2 << endl;
      }
      else if(operation == 2)//解密
      {
        cout << "请输入密钥:";
        cin >> key;
        cout << "请输入待解密字符串:";
        cin >> str1;
        Dncode(key, str1, str2);
        cout << "解密后的字符串:" << str2 << endl;
      }
      cout << endl;
  }
  return 0;
}
// 初始化维吉尼亚方阵
bool Init()
{
  int i, j;
  for(i = 0; i < CHARSUM; i++)
  {
      for(j = 0; j < CHARSUM; j++)
      {
        table[i][j] = 65 + (i + j) % CHARSUM;
      }
  }
  return true;
}
// 加密
// key:密钥
// source:待加密的字符串
// dest:经过加密后的字符串
bool Encode(char* key, char* source, char* dest)
{
  char* tempSource = source;
  char* tempKey = key;
  char* tempDest = dest;
    do
  {
      *tempDest = table[(*tempKey) - MINCHAR][(*tempSource) - MINCHAR];
      tempDest++;
      if(!(*(++tempKey)))
        tempKey = key;
  }while(*tempSource++);
    dest[strlen(source)] = 0;
  return true;
}
// 解密
// key:密钥
// source:待解密的字符串
// dest:经过解密后的字符串
bool Dncode(char* key, char* source, char* dest)
{
  char* tempSource = source;
  char* tempKey = key;
  char* tempDest = dest;
  char offset;
    do
  {
      offset = (*tempSource) - (*tempKey);
      offset = offset >= 0 ? offset : offset + CHARSUM;
      *tempDest = MINCHAR + offset;
      tempDest++;
      if(!(*(++tempKey)))
        tempKey = key;
  }while(*++tempSource);
  dest[strlen(source)] = 0;
  return true;
}

效果如图:

在这里插入图片描述

确定密钥长度m

Kasiski测试法

简介

Kasiski测试法是由Friedrich Kasiski于1863年给出了其描述,然而早在约1854年这一方法就由Charles Babbage首先发现。它主要基于这样一个事实:两个相同的明文段将加密成相同的密文段,它们的位置间距假设为δ,则δ≡0(mod m)。反过来,如果在密文中观察到两个相同的长度至少为3的密文段,那么将给破译者带来很大方便,因为它们实际上对应了相同的明文串。
  
使用流程

搜索长度至少为3的相同的密文段,记下其离起始点的那个密文段的距离。假如得到如下几个距离δ₁,δ₂,…,那么,可以猜测m为这些δi的最大公因子的因子。
  
例如

密钥:FORESTFORESTFORESTFORESTFOR
  明文:better to do well than to say well
  密文: GSKXWKYCUSOXQZKLSGYCJEQPJZC
  第一个YC出现后到第二个YC的结尾一共有12个字母,那么密钥的长度应是12的约数1,2,3,4,6,12之中的一个。
  当密文很长的时候,可以找出几组重复的密文段,找出它们间距的相同约数,就是密钥长度。

  • 12
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值