已经学过很多种密码的加密方法,也知道如何分析密码,但百学不如一用,今天我们就来制作一个简单的移位替换密码加密工具以及该密码的解密工具。
移位替换密码原理
移位替换密码是简单替换密码的一种,算是简单里面的简单,因此工具的制作也很简单。下面简述下移位替换密码的原理:
移位替换密码是针对明文的每个英文字母进行移位替换,假如密钥是3,那么密文就是向右移3位,即a->D,b->E,c->F。下面写出该移位替换密码的对照表:
a b c d e f g h i j k l m n o p q r s t u v w x y z D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
这就是凯撒密码!对相应字母进行替换即可完成加密,解密也是一样。下面再来说说如何进行对移位替换密码进行分析:
经过分析可以,密钥key只能为0-25里面的一个(即密钥空间为26)。因此,想要破解简单移位替换密钥就只需要穷举所有的密钥空间(俗称暴力破解),然后找出其中有意义的明文即可。
加密工具与解密工具制作
下面我们将加密工具与解密工具放在一个项目里面,编写程序如下:
#include<iostream> using namespace std; void decode(char*text); char* encode(char*plaintext, int key); int main() { int list = 0; while (true) { cout << "\tlist" << endl; cout << "1.Encry" << endl; cout << "2.Decry" << endl; cout << "0.Exit" << endl; cout << "please enter a number:"; cin >> list; if (list == 1 || list==2) { int strCount = 0; cout << "how many bytes do you need(how many character do you need:"; cin >> strCount; char*str = (char*)malloc(sizeof(char)*strCount); cout << "please enter a string:"; getchar(); cin.getline(str, strCount); if (list == 1) { cout << "please enter a key,the key must be a number:"; int key = 0; cin >> key; cout << encode(str, key) << endl; } else decode(str); } else if (list == 0) { exit(0); } else { cout << "you have not chosed a right number!" << endl; } } return 0; } /********decode************/ void decode(char*text) { char*t = text; for (int i = 0; i < 26; i++) { while(*t!='\0'){ if (*t == ' ') { t++; continue; } *t = (*t - 'A' + 1) % 26 + 'a'; t++; } t = text; cout << text << endl; } } /*************encode************/ char* encode(char*plaintext, int key) { char*p = plaintext; while (*p != '\0') { if (*p == ' ') { p++; continue; } *p = (*p - 'a' + key) % 26 + 'A'; p++; } return plaintext; }
运行示例
共
下面是白嫖用户的福利,点击即可下载编译好的exe文件链接。提取码: j2gw。