单表替换密码

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

实际问题:
密钥处理应该是这个程序的重点,加密和解密都没有什么要注意的地方。用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];//其余位直接复制
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容