日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

单表置换密码java代码实现_单表替换密码

發(fā)布時間:2025/3/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单表置换密码java代码实现_单表替换密码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

要求:

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

實際問題:

密鑰處理應該是這個程序的重點,加密和解密都沒有什么要注意的地方。用key[]數組接收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;

第二部分去掉重復字母和標點符號:

//----------------去掉重復和標點符號---------------

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中,在拿出來于密鑰文前面的字符對比,無則復制繼續(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)的字母順序補位:

//------------用未出現(xiàn)的字母補全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

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-------"<

cout << key <

//根據原文長度,申請密文空間

cin.getline(plaintext, text_len);

en_len = strlen(plaintext);

ciphertext = (char*)calloc(en_len + 1, sizeof(char));

//加密,輸出密文

encrypt(key, plaintext, ciphertext);

cout <

//解密,輸出原文

decrypt(key, ciphertext,plaintext);

cout <

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中,在拿出來于密鑰文前面的字符對比,無則復制繼續(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)的字母補全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;

}

}

}

//------------------加密---------------

//區(qū)分大小寫,其余字符直接復制

void encrypt(char key[],char platext[], char ciptext[])

{

int pla_len = strlen(platext);

for (int i = 0; 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];

}

}

//-----------------解密----------------

//區(qū)分大小寫,其余字符直接復制

void decrypt(char key[], char ciptext[], char platext[])

{

int cip_len = strlen(ciptext);

for (int i = 0; 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];//其余位直接復制

}

}

總結

以上是生活随笔為你收集整理的单表置换密码java代码实现_单表替换密码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。