美文网首页
单表替换密码

单表替换密码

作者: 比轩 | 来源:发表于2015-11-03 22:34 被阅读1215次

要求:
实现单表替换密码,用键盘接收明文和密钥,屏幕答应替换表和密文,大小写敏感,输入健壮性。

实际问题:
密钥处理应该是这个程序的重点,加密和解密都没有什么要注意的地方。用key[]数组接收keytext[],并分三部分处理。

第一部分统一换为小写:
//-----------------处理大写为小写------------------
int i, j, z, len = strlen(Ktext);
for (i = 0; i < len; i++)
    if (Ktext[i] >= 65 && Ktext[i] <= 90)
        Ktext[i] = Ktext[i] + 32;
第二部分去掉重复字母和标点符号:
//----------------去掉重复和标点符号---------------
for (i = 0, j = 0; i < strlen(Ktext); i++)
{
    if (i == 0 && Ktext[i] >= 97 && Ktext[i] <= 122)//第一位直接复制过去
    {
        K[j] = Ktext[i];
        j++;
    }

    else if (i != 0 && Ktext[i] >= 97 && Ktext[i] <= 122)//先复制到key中,在拿出来于密钥文前面的字符对比,无则复制继续,有则置空
    {
        K[j] = Ktext[i];
        for (z = 0; z < i; z++)
        {
            if (K[j] == Ktext[z])
                K[j] = NULL;
        }
        if (K[j] != NULL)
            j++;
    }
}
第三部分就是用未出现的字母顺序补位:
//------------用未出现的字母补全key-------------
len = strlen(K);
char temp = 'a' - 1;
int tag = 0;//设定标识位,扫描有相同字符置为1,无则置为0
for (i = 0; i < 26; i++)
{
    temp = temp + 1;
    for (j = 0; j < len; j++)
    {
        if (K[j] == temp)
            tag = 1;
    }
    if (tag == 1) {//根据标志位的值选择是否补位
        tag = 0;
    }
    else if (tag == 0) {
        K[len] = temp;
        len += 1;
    }
}
程序源码:
// Console-单表代换密码.cpp
//2015-10-3
#include "stdafx.h"
#include <iostream>
using namespace std;
int text_len = 500;

//处理key
void make_key(char[], char[]);

//加密
void encrypt(char [], char[], char[]);

//解密
void decrypt(char[], char[], char[]);

int main()
{
//申请密钥文,密钥,原文空间
char *keytext, *key, *plaintext, *ciphertext;
int en_len;
plaintext = (char*)calloc(text_len, sizeof(char));
keytext = (char*)calloc(text_len, sizeof(char));
key = (char*)calloc(27, sizeof(char));
cout << "-------input keytext-------" << endl;
cin.getline(keytext, text_len);
//处理密钥,输出替换表
make_key(keytext, key);
cout << "-------exchange list-------"<<endl<<"abcdefghijklmnopqrstuvwxyz" << endl;
cout << key <<endl << endl <<"-------input plaintext------"<< endl;
//根据原文长度,申请密文空间
cin.getline(plaintext, text_len);
en_len = strlen(plaintext);
ciphertext = (char*)calloc(en_len + 1, sizeof(char));
//加密,输出密文
encrypt(key, plaintext, ciphertext);
cout <<endl<<"---------ciphertext by encryption------------"<<endl<< ciphertext << endl;
//解密,输出原文
decrypt(key, ciphertext,plaintext);
cout <<"----------palintext by decryption------------"<<endl<< plaintext << endl;

return 0;
}

//处理key
void make_key(char Ktext[], char K[])
{
//-----------------处理大写为小写------------------
int i, j, z, len = strlen(Ktext);
for (i = 0; i < len; i++)
    if (Ktext[i] >= 65 && Ktext[i] <= 90)
        Ktext[i] = Ktext[i] + 32;
//----------------去掉重复和标点符号---------------
for (i = 0, j = 0; i < strlen(Ktext); i++)
{
    if (i == 0 && Ktext[i] >= 97 && Ktext[i] <= 122)//第一位直接复制过去
    {
        K[j] = Ktext[i];
        j++;
    }

    else if (i != 0 && Ktext[i] >= 97 && Ktext[i] <= 122)//先复制到key中,在拿出来于密钥文前面的字符对比,无则复制继续,有则置空
    {
        K[j] = Ktext[i];
        for (z = 0; z < i; z++)
        {
            if (K[j] == Ktext[z])
                K[j] = NULL;
        }
        if (K[j] != NULL)
            j++;
    }
}
//------------用未出现的字母补全key-------------
len = strlen(K);
char temp = 'a' - 1;
int tag = 0;//设定标识位,扫描有相同字符置为1,无则置为0
for (i = 0; i < 26; i++)
{
    temp = temp + 1;
    for (j = 0; j < len; j++)
    {
        if (K[j] == temp)
            tag = 1;
    }
    if (tag == 1) {//根据标志位的值选择是否补位
        tag = 0;
    }
    else if (tag == 0) {
        K[len] = temp;
        len += 1;
    }
}
}

//------------------加密---------------
//区分大小写,其余字符直接复制
void encrypt(char key[],char platext[], char ciptext[])
{
int pla_len = strlen(platext);
for (int i = 0; i <pla_len ; i++)
{
    if (platext[i] >= 65 && platext[i] <= 90) //处理大写
    {
        ciptext[i] = key[platext[i] - 65];//根据原文在替换序列中的位置,输出对应位置的密文到密文字符串
    }
    else if (platext[i] >= 97 && platext[i] <= 122)//处理小写
    {
        ciptext[i] = key[platext[i] - 97] - 32;
    }
    else
        ciptext[i] = platext[i];
}
}
//-----------------解密----------------
//区分大小写,其余字符直接复制
void decrypt(char key[], char ciptext[], char platext[])
{
int cip_len = strlen(ciptext);

for (int i = 0; i <cip_len; i++)
{
    if (ciptext[i] >= 65 && ciptext[i] <= 90)//处理大写
    {
        for (int j = 0; j < 26; j++)//根据密文确定对应密钥字符在序列中的位置,输出对应位置的原文
        {
            if (ciptext[i] + 32 == key[j])
                platext[i] = 'a' + j;
        }
    }
    else if (ciptext[i] >= 97 && ciptext[i] <= 122)//处理小写
    {
        for (int j = 0; j < 26; j++)
        {
            if (ciptext[i] == key[j])
                platext[i] = 'a' + j-32;
        }
    }
    else
        platext[i] = ciptext[i];//其余位直接复制
}
}

相关文章

  • 单表替换密码

    要求:实现单表替换密码,用键盘接收明文和密钥,屏幕答应替换表和密文,大小写敏感,输入健壮性。 实际问题:密钥处理应...

  • 简单替换密码

    简单替换密码是将明文中使用的字母表替换为另一套字母表的密码。凯撒密码也可以说是简单替换密码的一种。 简单替换...

  • 小记1—基础概念

    1.密码发展阶段 古典密码 现代密码学 量子密码 2.古典密码 代替密码、移位密码、代数密码(比如异或运算) 单表...

  • Python 密码学实践 —— 凯撒密码

    一、原理 凯撒密码 是密码学中的一种简单的替换加密技术。明文中的所有字符都会替换为其按照字母表顺序向左(或向右)偏...

  • 加密算法研究记录

    历史上的密码 凯撒密码 凯撒密码是通过将明文中所使用的字母表按照一定的字数“平移”来进行加密和解密的。 简单替换密...

  • 密码学笔记2——维吉尼亚密码的破解

    1.凯撒密码 官方例子 恺撒密码的替换方法是通过排列明文和密文字母表,密文字母表示通过将明文字母表向左或向右移动一...

  • 密码学(二)之代换密码

    (一)代换密码(替换密码)** 上一讲中,我们讲移位密码其实是将字母表中的字母一一对应到各数字,然后通过数字平移来...

  • 代换密码的密码分析—详细分析过程

    代换密码的密码分析 使用单表代换密码,对于给出的密文进行分析并得到结果,了解加密解密过程 密文信息如下:AHNFC...

  • 1.6 典型的古典密码

    主要是其中思想 分个类 置换密码单纯将明文字母做位置顺序上的移动单表代替加法密码乘法密码y = kx(mod 26...

  • ctf古典密码从0到1

    亲爱的,关注我吧 本文共计6357个词 阅读预计花费8分钟 1.古典密码和现代密码的区别: 2.代换密码 a)单表...

网友评论

      本文标题:单表替换密码

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