密码学原理_Crypto++实现一次性密码本(OTP)
来源:互联网 发布:淘宝卖家论坛首页 编辑:程序博客网 时间:2024/06/06 13:02
Requirements
请用 Crypto++实现一个 One Time Pad。请提交全部源代码,以及把“hello world” 加密后产生的密文。
Compilation Options
g++ -g3 -O2 -Wall -Wextra -o opt opt.cpp -lcryptopp -std=c++11
Example
Welcome to Sunny's One Time Pad.Wish you a good trip.Please input you plaintext:Hello, WorldThe length of plaintext is 12.plaintext: 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64key: E7 5A D3 FE 8B 4B 84 6A 5B 28 E6 60ciphertext: af 3f bf 92 e4 67 a4 3d 34 5a 8a 04
Description
Since some asciis are invisible, ciphertexts are expressed by hex.
Source Code
#include <iostream>#include <sstream>#include <iomanip>#include <cryptopp/osrng.h>#include <cryptopp/hex.h>#include <cryptopp/filters.h>using namespace std;using namespace CryptoPP;void print_hex(string, int);int main() { string prologue = "Welcome to Sunny's One Time Pad.\nWish you a good trip."; cout << prologue << endl; cout << "Please input you plaintext:" << endl; string plaintext; getline(cin, plaintext); int lengthOfText = plaintext.length(); cout << "The length of plaintext is " << lengthOfText << "." << endl; RandomPool prng; SecByteBlock seed(lengthOfText); OS_GenerateRandomBlock(false, seed, seed.size()); prng.IncorporateEntropy(seed, seed.size()); string key; HexEncoder keystring(new StringSink(key)); keystring.Put(seed, seed.size()); keystring.MessageEnd(); stringstream ss; for (auto &it: plaintext) ss << hex << (int)it; string mystr = ss.str(); cout << "plaintext: " << '\t'; print_hex(mystr, lengthOfText); cout << "key: " << '\t' << '\t'; print_hex(key, lengthOfText); string ciphertext; int *i1 = new int[lengthOfText * 2]; int *i2 = new int[lengthOfText * 2]; int *i3 = new int[lengthOfText * 2]; /* * Core Operation: XOR operation * XOR plaintext and key by bit */ for (int i = 0; i < lengthOfText * 2; i++) { if (key[i] >= 'A') i1[i] = key[i] - 'A' + 10; else i1[i] = key[i] - '0'; if (mystr[i] >= 'a') i2[i] = mystr[i] - 'a' + 10; else i2[i] = mystr[i] - '0'; i3[i] = i1[i] ^ i2[i]; } int *i4 = new int[lengthOfText]; for (int i = 0; i < lengthOfText; i++) { i4[i] = i3[i * 2] * 16 + i3[i * 2 + 1]; } cout << "ciphertext: " << '\t'; for (int i = 0; i < lengthOfText; i++) { cout << setfill('0') << setw(2) << hex << i4[i] << ' '; } cout << endl; return 0;}void print_hex(string str, int length) { for (int i = 0; i < length * 2; i++) { cout << hex << str[i]; if (i % 2) cout << ' '; } cout << endl;}
阅读全文
0 0
- 密码学原理_Crypto++实现一次性密码本(OTP)
- 密码学原理_Crypto++实现3DES加密
- 密码学原理_Crypto++实现RSA加密
- 密码学原理_Crypto++实现SHA-1近似碰撞寻找
- OTP原理及实现
- 流加密,一次性密码本的原理,简介,事例以及攻击方式
- OTP动态密码_Java代码实现
- 【密码学】维吉尼亚密码加解密原理及其破解算法Java实现
- 使用一次性密码本通过 SSH 安全登录 Linux
- [python] 密码学:代换密码的实现
- [python] 密码学:仿射密码的实现
- [python] 密码学:维吉尼亚密码的实现
- [python] 密码学:置换密码的实现
- otp的原理
- 算法实现: OTP(One-Time Pad) 一次一密密码本 加解密
- 密码学之Hill密码的Java实现1
- 脱机一次性口令OTP原理(将用户id加密到一串数字中)类似微信支付宝付款码,将军令等
- [密码学]恺撒密码
- 我们都很好
- Slim 框架学习,第十七天 _Router(八)
- 如何在excel中设置下拉菜单
- daemon线程
- JDBC Java数据库连接 2 2017年12月20日 22:45:56
- 密码学原理_Crypto++实现一次性密码本(OTP)
- extract-text-webpack-plugin配置出错的解决办法
- 杂货边角(6):Windows的数据类型和编译器固定数据类型对照
- 学Python(6)—日期计算案例
- latex 页眉插入图片logo
- 项目经理面试中可能遇到的问题(持续更新)
- 本地代码上传到Github
- 数据结构--静态单链表
- Android7.0去电流程源码分析(二)