單表替換密碼

要求:
實(shí)現(xiàn)單表替換密碼,用鍵盤接收明文和密鑰,屏幕答應(yīng)替換表和密文,大小寫敏感,輸入健壯性。

實(shí)際問題:
密鑰處理應(yīng)該是這個(gè)程序的重點(diǎn),加密和解密都沒有什么要注意的地方。用key[]數(shù)組接收keytext[],并分三部分處理。

第一部分統(tǒng)一換為小寫:
//-----------------處理大寫為小寫------------------
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;
第二部分去掉重復(fù)字母和標(biāo)點(diǎn)符號(hào):
//----------------去掉重復(fù)和標(biāo)點(diǎn)符號(hào)---------------
for (i = 0, j = 0; i < strlen(Ktext); i++)
{
    if (i == 0 && Ktext[i] >= 97 && Ktext[i] <= 122)//第一位直接復(fù)制過去
    {
        K[j] = Ktext[i];
        j++;
    }

    else if (i != 0 && Ktext[i] >= 97 && Ktext[i] <= 122)//先復(fù)制到key中,在拿出來于密鑰文前面的字符對(duì)比,無則復(fù)制繼續(xù),有則置空
    {
        K[j] = Ktext[i];
        for (z = 0; z < i; z++)
        {
            if (K[j] == Ktext[z])
                K[j] = NULL;
        }
        if (K[j] != NULL)
            j++;
    }
}
第三部分就是用未出現(xiàn)的字母順序補(bǔ)位:
//------------用未出現(xiàn)的字母補(bǔ)全key-------------
len = strlen(K);
char temp = 'a' - 1;
int tag = 0;//設(shè)定標(biāo)識(shí)位,掃描有相同字符置為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) {//根據(jù)標(biāo)志位的值選擇是否補(bǔ)位
        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()
{
//申請(qǐng)密鑰文,密鑰,原文空間
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;
//根據(jù)原文長度,申請(qǐng)密文空間
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;
//----------------去掉重復(fù)和標(biāo)點(diǎn)符號(hào)---------------
for (i = 0, j = 0; i < strlen(Ktext); i++)
{
    if (i == 0 && Ktext[i] >= 97 && Ktext[i] <= 122)//第一位直接復(fù)制過去
    {
        K[j] = Ktext[i];
        j++;
    }

    else if (i != 0 && Ktext[i] >= 97 && Ktext[i] <= 122)//先復(fù)制到key中,在拿出來于密鑰文前面的字符對(duì)比,無則復(fù)制繼續(xù),有則置空
    {
        K[j] = Ktext[i];
        for (z = 0; z < i; z++)
        {
            if (K[j] == Ktext[z])
                K[j] = NULL;
        }
        if (K[j] != NULL)
            j++;
    }
}
//------------用未出現(xiàn)的字母補(bǔ)全key-------------
len = strlen(K);
char temp = 'a' - 1;
int tag = 0;//設(shè)定標(biāo)識(shí)位,掃描有相同字符置為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) {//根據(jù)標(biāo)志位的值選擇是否補(bǔ)位
        tag = 0;
    }
    else if (tag == 0) {
        K[len] = temp;
        len += 1;
    }
}
}

//------------------加密---------------
//區(qū)分大小寫,其余字符直接復(fù)制
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];//根據(jù)原文在替換序列中的位置,輸出對(duì)應(yīng)位置的密文到密文字符串
    }
    else if (platext[i] >= 97 && platext[i] <= 122)//處理小寫
    {
        ciptext[i] = key[platext[i] - 97] - 32;
    }
    else
        ciptext[i] = platext[i];
}
}
//-----------------解密----------------
//區(qū)分大小寫,其余字符直接復(fù)制
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++)//根據(jù)密文確定對(duì)應(yīng)密鑰字符在序列中的位置,輸出對(duì)應(yīng)位置的原文
        {
            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];//其余位直接復(fù)制
}
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容