一个加密程序
#1 算法解释
罗列名词
明文:加密前的信息
密文:加密后的信息
加密过程
#0 前置操作
规定一个密表,把每个字母和符号规定一个3*3的代码
字母
第一行a x x x
第二行b x x x
第三行c x x x
Tip: 每个x代表一个数字,0或1
#1 第一步操作
安照密表加密,把代码拼接成三行
#2 第二步
压缩,将这三行仅包含0和1数串,人话就是二进制数,转换成三个十进制数,密钥是明文长度*3
#2 我的代码:
包括3个自制头文件+1个主体代码
SCC.h
cpp
#include<iostream>
#include<map>
using namespace std;
//为防止屎山,特加注释
map<char,int>SCC;//标准交流码Standard Communication Code
map<string,char>_SCC;
char list[]={'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','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','+','-','*','/','"',39,';',':',',','.','<','>','?','(',')','[',']','{','}','|','\\','=','_','&','^','%','$','#','@','!','`','~',' ','1','2','3','4','5','6','7','8','9','0'};
void init_SCC(){
for(int i=0;i<84;i++){
SCC[list[i]]=i;
}
return;
}
int code[95][3][3]={
{//a
{1,0,0},
{0,0,0},
{0,0,0}
},
{//b
{0,1,0},
{0,0,0},
{0,0,0}
},
{//c
{0,0,1},
{0,0,0},
{0,0,0}
},
{//d
{0,0,0},
{1,0,0},
{0,0,0}
},
{//e
{0,0,0},
{0,1,0},
{0,0,0}
},
{//f
{0,0,0},
{0,0,1},
{0,0,0}
},
{//g
{0,0,0},
{0,0,0},
{1,0,0}
},
{//h
{0,0,0},
{0,0,0},
{0,1,0}
},
{//i
{0,0,0},
{0,0,0},
{0,0,1}
},
{//j
{1,1,0},
{0,0,0},
{0,0,0}
},
{//k
{1,0,1},
{0,0,0},
{0,0,0}
},
{//l
{1,0,0},
{1,0,0},
{0,0,0}
},
{//m
{1,0,0},
{0,1,0},
{0,0,0}
},
{//n
{1,0,0},
{0,0,1},
{0,0,0}
},
{//o
{1,0,0},
{0,0,0},
{1,0,0}
},
{//p
{1,0,0},
{0,0,0},
{0,1,0}
},
{//q
{1,0,0},
{0,0,0},
{0,0,1}
},
{//r
{1,1,1},
{0,0,0},
{0,0,0}
},
{//s
{1,1,0},
{1,0,0},
{0,0,0}
},
{//t
{1,1,0},
{0,1,0},
{0,0,0}
},
{//u
{1,1,0},
{0,0,1},
{0,0,0}
},
{//v
{1,1,0},
{0,0,0},
{1,0,0}
},
{//w
{1,1,0},
{0,0,0},
{0,1,0}
},
{//x
{1,1,0},
{0,0,0},
{0,0,1}
},
{//y
{1,0,1},
{1,0,0},
{0,0,0}
},
{//z
{1,0,1},
{0,1,0},
{0,0,0}
},
{//A
{1,0,1},
{0,0,1},
{0,0,0}
},
{//B
{1,0,1},
{0,0,0},
{1,0,0}
},
{//C
{1,0,1},
{0,0,0},
{0,1,0}
},
{//D
{1,0,1},
{0,0,0},
{0,0,1}
},
{//E
{1,0,0},
{1,1,0},
{0,0,0}
},
{//F
{1,0,0},
{1,0,1},
{0,0,0}
},
{//G
{1,0,0},
{1,0,0},
{1,0,0}
},
{//H
{1,0,0},
{1,0,0},
{0,1,0}
},
{//I
{1,0,0},
{1,0,0},
{0,0,1}
},
{//J
{1,0,0},
{0,1,1},
{0,0,0}
},
{//K
{1,0,0},
{0,1,0},
{1,0,0}
},
{//L
{1,0,0},
{0,1,0},
{0,1,0}
},
{//M
{1,0,0},
{0,1,0},
{0,0,1}
},
{//N
{1,0,0},
{0,0,1},
{1,0,0}
},
{//O
{1,0,0},
{0,0,1},
{0,1,0}
},
{//P
{1,0,0},
{0,0,1},
{0,0,1}
},
{//Q
{1,0,0},
{0,0,0},
{1,1,0}
},
{//R
{1,0,0},
{0,0,0},
{1,0,1}
},
{//S
{1,0,0},
{0,0,0},
{0,1,1}
},
{//T
{1,1,1},
{1,0,0},
{0,0,0}
},
{//U
{1,1,1},
{0,1,0},
{0,0,0}
},
{//V
{1,1,1},
{0,0,1},
{0,0,0}
},
{//W
{1,1,1},
{0,0,0},
{1,0,0}
},
{//X
{1,1,1},
{0,0,0},
{0,1,0}
},
{//Y
{1,1,1},
{0,0,0},
{0,0,1}
},
{//Z
{1,1,0},
{1,1,0},
{0,0,0}
},
{//+
{1,1,0},
{1,0,1},
{0,0,0}
},
{//-
{1,1,0},
{1,0,0},
{1,0,0}
},
{//*
{1,1,0},
{1,0,0},
{0,1,0}
},
{// /
{1,1,0},
{1,0,0},
{0,0,1}
},
{//"
{1,1,0},
{0,1,1},
{0,0,0}
},
{//'
{1,1,0},
{0,1,0},
{1,0,0}
},
{//;
{1,1,0},
{0,1,0},
{0,1,0}
},
{//:
{1,1,0},
{0,1,0},
{0,0,1}
},
{//,
{1,1,0},
{0,0,1},
{1,0,0}
},
{//.
{1,1,0},
{0,0,1},
{0,1,0}
},
{//<
{1,1,0},
{0,0,1},
{0,0,1}
},
{//>
{1,1,0},
{0,0,0},
{1,1,0}
},
{//?
{1,1,0},
{0,0,0},
{1,0,1}
},
{//(
{1,1,0},
{0,0,0},
{0,1,1}
},
{//)
{1,0,1},
{1,1,0},
{0,0,0}
},
{//[
{1,0,1},
{1,0,1},
{0,0,0}
},
{//]
{1,0,1},
{1,0,0},
{1,0,0}
},
{//{
{1,0,1},
{1,0,0},
{0,1,0}
},
{//}
{1,0,1},
{1,0,0},
{0,0,1}
},
{//|
{1,0,1},
{0,1,1},
{0,0,0}
},
{//反斜杠
{1,0,1},
{0,1,0},
{1,0,0}
},
{//=
{1,0,1},
{0,1,0},
{0,1,0}
},
{//_
{1,0,1},
{0,1,0},
{0,0,1}
},
{//&
{1,0,1},
{0,0,1},
{1,0,0}
},
{//^
{1,0,1},
{0,0,1},
{0,1,0}
},
{//%
{1,0,1},
{0,0,1},
{0,0,1}
},
{//$
{1,0,1},
{0,0,0},
{1,1,0}
},
{//#
{1,0,1},
{0,0,0},
{1,0,1}
},
{//@
{1,0,1},
{0,0,0},
{0,1,1}
},
{//!
{1,0,0},
{1,1,1},
{0,0,0}
},
{//`
{1,0,0},
{1,1,0},
{1,0,0}
},
{//~
{1,0,0},
{1,1,0},
{0,1,0}
},
{//" "
{1,0,0},
{1,1,0},
{0,0,1}
},
{//1
{1,0,0},
{1,0,1},
{1,0,0}
},
{//2
{1,0,0},
{1,0,1},
{0,1,0}
},
{//3
{1,0,0},
{1,0,1},
{0,0,1}
},
{//4
{1,0,0},
{1,0,0},
{1,1,0}
},
{//5
{1,0,0},
{1,0,0},
{1,0,1}
},
{//6
{1,0,0},
{1,0,0},
{0,1,1}
},
{//7
{1,0,0},
{0,1,1},
{1,0,0}
},
{//8
{1,0,0},
{0,1,1},
{0,1,0}
},
{//9
{1,0,0},
{0,1,1},
{0,0,1}
},
{//0
{1,0,0},
{0,1,0},
{1,1,0}
}
};
void init__SCC(){
string _this;
for(int i=0;i<95;i++){
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
_this+=code[i][j][k];
}
}
_SCC[_this]=list[i];
}
return;
}
GOTO.h
cpp
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
//string _10_2(string _10){
// string _2_temp;
// string _2;
// ll _int_10=stoi(_10);
// for(;_int_10>=0;_2_temp+=_int_10%2+'0',_int_10=(_int_10-_int_10%2)/2);
// for(int i=0,j=_2_temp.length()-1;i<_2_temp.length();_2[i]=_2_temp[j],i++,j--);
// return _2;
//}
string _int_string(int _int){
string ret;
for(int i=10;_int%i==_int;ret='0'+_int%i,i*=10);
}
int _str_int(string _str){
return stoi(_str);
}
string _10_2(string _10){
long long dec=stoll(_10);
string bin="";
while(dec>0){
int remainder=dec%2;
bin=to_string(remainder)+bin;
dec/=2;
}
if(bin.empty())return "0";
return bin;
}
ll _n_n_n(int a,int n){
ll ret=1;
for(int i=0;i<n;ret*=a,i++);
return ret;
}
string _2_10(string _2){
string _10;
ll _10_temp;
for(int i=0;i<_2.length();_10_temp+=(_2[_2.length()-i]-'0')*_n_n_n(_2[_2.length()-i]-'0',_2.length()-i),i++);
_10=to_string(_10_temp);
return _10;
}
StringToCode.h
cpp
#include"SCC.h"
#include"GOTO.h"
class StringToCode_ret{
public:
string ret[3];
int length=0;
};
StringToCode_ret StringToCode(string _s){
int _this;
string arr_temp[3];
StringToCode_ret ret;
for(int i=0;i<_s.length();i++){
_this=SCC[_s[i]];
for(int j=0;j<3;j++){
arr_temp[j]+=('0'+code[_this][j][0])+('0'+code[_this][j][1])+('0'+code[_this][j][2]);
}
}
// Password={113,119,101,114,116,121,117,105,111,112,91,93,124,97,115,100,102,103,104,106,107,108,59,39,122,120,99,118,98,110,109,44,46,47,123,125,58,60,62,63,96,33,64,35,36,37,94,38,42,};
ret.ret[0]=_2_10(arr_temp[0]);
ret.ret[1]=_2_10(arr_temp[1]);
ret.ret[2]=_2_10(arr_temp[2]);
return ret;
}
string _empty="";
string _put_front_zero(string _1,int n/*biao zhun*/){
if(_1.length()==n){
return _1;
}
string front="";
for(int i=0;i<n-_1.length();i++){
front+="0";
}
_1=front+_1;
return _1;
}
string CodeToString(StringToCode_ret _s){
string arr_temp[3];
arr_temp[0]=_10_2(_s.ret[0]);
arr_temp[1]=_10_2(_s.ret[1]);
arr_temp[2]=_10_2(_s.ret[2]);
arr_temp[1]=_put_front_zero(arr_temp[1],_s.length);
arr_temp[2]=_put_front_zero(arr_temp[2],_s.length);
string _this;
string ret;
for(int i=0;i<arr_temp[0].length();i+=3){
for(int j=i;j<i+3;j++){
for(int k=0;k<3;k++){
_this+=arr_temp[j][k];
}
}
ret+=_SCC[_this];
_this=_empty;
}
return ret;
}
main.cpp
cpp
#include"StringToCode.h"
#include<cstring>
#include<windows.h>
int main(){
init_SCC();
init__SCC();
StringToCode_ret ret;
string str;
getline(cin,str);
ret=StringToCode(str);
ret.length=str.length()*3;
cout<<"加密/压缩后的信息(第1个是密钥,剩下3个数字都是密文)\n"<<"length:"<<ret.length<<endl<<ret.ret[0]<<endl<<ret.ret[1]<<endl<<ret.ret[2];
str=CodeToString(ret);
cout<<"加密/压缩前的信息:"<<str;
}
mian.cpp只是一个调试代码,调试尚未通过,出现了我解决不了的问题:
#3 问题描述
来看一段视频
问题