一门研究设计密码算法和破译密码算法的综合性技术科学,是网络空间安全学科中理论体系最完善的一门科学,也是信息安全的基石。密码学一般由密码编码和密码分析两大分支组成。ios
从密码学的发展历史来看,能够分为古典密码学和现代密码学。c++
古典密码学
主要依靠人工计算和简单的机械,而且以人的主观意识来设计和应用。古典加密算法每每只是对单个的代替或置换操做。web
现代密码学…算法
替代密码的原理是使用替代法进行加密,就是将明文中的字符用其余字符替代后造成密文,例如,明文字母a、b、c、d,用D、E、F、G作对应替换后造成密文。安全
替代密码包括多种类型,如单表替代密码,多明码替代密码,多字母替代密码,多表替代密码等网络
凯撒密码
又称循环移位密码,是一种典型的单表替代密码,它的加密方法就是将明文中的每一个字母用此字符在字母表中后面第k个字母替代,加密过程能够表示为:app
E(m) = (m+k) mod n
【其中,m为明文字母在字母表中的位置数,n为字母表中的字母个数,k为密钥,E(m)为密文字母在字母表中对应的位置数,mod n为取模运算。】svg
因为是对名文字母进行统一的偏移代替,所以密钥极易被穷举破解,为了提升破解难度,多表代替密码则是在加解密时使用了多个替换表,表明性算法有维吉尼亚密码,希尔密码,一次一密钥密码,Playfair密码等。编码
维吉尼亚密码
该密码体制有一个参数 n ,在加解密时,一样把英文字母映射为 0~25 的数字再进行运算,并按 n 个字母一组进行变换。明文空间、密文空间及密钥空间都是长度为 n 的英文字母串的集合,加密体制描述以下:加密
加密变换定义为:设密钥 k = (k1,k2,...,kn),明文 m = (m1,m2,...,mn), 密文 c = (c1,c2,...,cn). 加密变换:Ek(m) = (c1,c2,...,cn), 其中,ci = (mi+ki) mod 26, i=1,2,...,n. 解密算法:Dk(m) = (m1,m2,...,mn), 其中,mi = (ci-ki) mod 26, i=1,2,...,n.
如下分别是对两种加密方式的实现{后者包含了解密操做}【望指正】!!!
#include<iostream> #include<map> #include<string> #define n 26 using namespace std; template<typename _MapType> auto get_map_key_value(const _MapType& input_map, const decltype(input_map.begin()->second)& mapped_value) -> decltype(input_map.begin()->first) { auto iter = std::find_if(input_map.begin(), input_map.end(), [mapped_value](const auto& item) { return (item.second == mapped_value); }); if (iter == input_map.end()) { return decltype(input_map.begin()->first)(); } return iter->first; } static map<char, int> Alphabetic_query = { {'a',1},{'b',2},{'c',3},{'d',4}, {'e',5},{'f',6},{'g',7},{'h',8}, {'i',9},{'j',10},{'k',11},{'l',12}, {'m', 13},{'n',14},{'o',15},{'p',16}, {'q',17},{'r',18},{'s',19}, {'t',20}, {'u',21},{'v',22},{'w',23},{'x',24}, {'y',25},{'z',26} }; int main() { cout << "THE FIRST METHOD CALLED Caesar PASSWORD" << endl; cout << "Enter the characters you need to encrypt and the key:"; string characters,Enc_password;; char key, *position; int bridge_key, bridge_characters; cin >> characters >> key; if (characters.length() != NULL && key != NULL) { position = &characters[0]; if (key >= 65 && key <= 90) bridge_key = Alphabetic_query.at(key + 32); else bridge_key = Alphabetic_query.at(key); while (position != NULL) { if ((*position) >= 65 && (*position) <= 90) bridge_characters = Alphabetic_query.at(*position + 32); else bridge_characters = Alphabetic_query.at(*position); Enc_password += Caesar(bridge_characters, bridge_key); position++; } } cout << "Encrypted character" << Enc_password << endl; } char Caesar(int m,int k) { return get_map_key_value(Alphabetic_query, (m + k) % n); }
#include<iostream> #include<map> #include<string> #define n 26 using namespace std; template<typename _MapType> auto get_map_key_value(const _MapType& input_map, const decltype(input_map.begin()->second)& mapped_value) -> decltype(input_map.begin()->first) { auto iter = std::find_if(input_map.begin(), input_map.end(), [mapped_value](const auto& item) { return (item.second == mapped_value); }); if (iter == input_map.end()) { return decltype(input_map.begin()->first)(); } return iter->first; } static map<char, int> Alphabetic_query = { {'a',1},{'b',2},{'c',3},{'d',4}, {'e',5},{'f',6},{'g',7},{'h',8}, {'i',9},{'j',10},{'k',11},{'l',12}, {'m', 13},{'n',14},{'o',15},{'p',16}, {'q',17},{'r',18},{'s',19}, {'t',20}, {'u',21},{'v',22},{'w',23},{'x',24}, {'y',25},{'z',26} }; string characters, Enc_password, key; char *position_c, *position_k; int bridge_key, bridge_characters; bool flag; int main() { cout << "THE SECOND METHOD CALLED Virginia password" << endl; cout << "Enter the characters you need to encrypt and the key:"; cin >> characters >> key; string result_e = Virginia_add(characters, key); cout << "Encrypted character" << result_e << endl; string result_u = Virginia_solution(result_e,key); cout << "Unencrypted character" << result_u << endl; } string transform(string &characters,string &key,bool flag) { if (characters.length() != NULL && key.length() != NULL && characters.length() == key.length()) { position_c = &characters[0]; position_k = &key[0]; while (position_c != NULL && position_k != NULL) { if (*position_k >= 65 && *position_k <= 90) bridge_key = Alphabetic_query.at(*position_k + 32); else bridge_key = Alphabetic_query.at(*position_k); if ((*position_c) >= 65 && (*position_c) <= 90) bridge_characters = Alphabetic_query.at(*position_c + 32); else bridge_characters = Alphabetic_query.at(*position_c); if(flag==true) Enc_password += Virginia(bridge_characters, bridge_key); else Enc_password += Virginia(bridge_characters, bridge_key); *position_c++; *position_k++; } } return Enc_password; } char Virginia(int m, int k) { return get_map_key_value(Alphabetic_query, (m + k) % n); } char Unvirginia(int m, int k) { return get_map_key_value(Alphabetic_query, (m - k) % n); } string Virginia_add(string &characters, string &key){ return transform(characters, key,1); } string Virginia_solution(string &result,string &key) { return transform(result, key,0); }