A very simple C++ module to encrypt/decrypt strings based on B64 and Vigenere ciper.

https://github.com/philipperemy/easy-encryption

Easy Encryption

A very simple yet powerful standalone C++ module (API) to encrypt/decrypt strings based on B64 and Vigenere ciper (symmetric cipher).

It works as follows:

  • Alice encodes in base64 the message, then uses the Vigenere private key to encrypt the message.
  • The encrypted message is sent through an unsecured channel.
  • Bob gets the message and decrypts it with the Vigenere private key. He then decodes it with base64.

Diagram summary:


Message -> B64 ENCODE -> VIGENERE ENCRYPT -> encrypted message -> VIGENERE DECRYPT -> B64 DECODE -> Message


The system is safe and unbreakable because:

  • Vigenere cipher is vulnerable to bruteforce attacks with dictionaries. Our system encodes first the string in base64. All frequencies and other attacks are void.
  • A very large key is used for the Vigenere encryption. We strongly encourage the key to be larger than any message to be sent. The reason is that Vigenere is almost uncrackable if the cipher key is extremely long compared to the average message length.
  • The reason why we apply b64 encode BEFORE Vigenere is because it's very easy for somebody to apply a b64 decode and see about the structure of the message. For example, if we send {"hello":123}, an attacker can sniff the message, b64 decode the message and get {"qsggn":ygf}. Of course the attacker still needs the Vigenere cipher key, but at least, he can get a pretty clear idea that JSON format messages are sent in the communication channel. The way to avoid this is to encode first in b64 then encrypt it with the Vigenere key. If the attacker tries to b64 decode first, it will see a random string of weird characters.

API

C++

  • Encrypt message
std::string encrypt(std::string& msg, std::string& key)
  • Decrypt message
std::string decrypt(std::string& encrypted_msg, std::string& key)

Python

  • Encrypt message
wrapper.encrypt(message, key): returns encrypted message
  • Decrypt message
wrapper.decrypt(encrypted_message, key): returns decrypted message

Compilation and execution

g++ cl.cpp
./a.out "Hello world" MYPRIVATEKEY 0

The encrypted message is: ttz9JqxZHBClNtu=.

./a.out ttz9JqxZHBClNtu= MYPRIVATEKEY 1

The decrypted message is Hello world.

Python wrapper

rm a.out
g++ cl.cpp
python3 wrapper.py

Example - Encoding/Decoding JSON format

Source code

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include "encrypt.h" using namespace std; int main() {
// std::string msg = "HELLO WORLD";
std::string msg = "{\"id\":1,\"method\":\"service.subscribe\",\"params\":[\"myapp/0.1c\", null,\"0.0.0.0\",\"80\"]}";
std::string key = "THISISMYKEY";
std::cout << " message to send: " << msg << std::endl;
std::string encrypted_msg = encrypt(msg, key);
std::cout << "encrypted message: " << encrypted_msg << std::endl;
std::string decrypted_msg = decrypt(encrypted_msg, key);
std::cout << "decrypted message: " << decrypted_msg << std::endl;
return 0;
}

Output

  message to send: {"id":1,"method":"service.subscribe","params":["myapp/0.1c", null,"0.0.0.0","80"]}
encrypted message: X5g7wjjTllj1ItCxShWUb77PKJsfP VNMAB7VtqaLCccGTr0ijkjxqw0IutQvXfSFK4OKo8cnpD1Lge0pdMCZf0fqQ8bjjFjkNn1h pBtdwNJD==
decrypted message: {"id":1,"method":"service.subscribe","params":["myapp/0.1c", null,"0.0.0.0","80"]}

A very simple C++ module to encrypt/decrypt strings based on B64 and Vigenere ciper.的更多相关文章

  1. MicroSoft CryptoAPI data/file encrypt/decrypt

    linux 用第三方库 Crypto++, 还未实战. CryptoAPI使用两种密钥:会话密钥与公共/私人密钥对.会话密钥使用相同的加密和解密密钥,这种算法较快,但必须保证密钥的安全传递.公共/私人 ...

  2. 困在栅栏里的恺撒WriteUp(附栅栏密码加密解密脚本)

    题目地址:http://www.shiyanbar.com/ctf/1867 这道题目并不难,就是先用栅栏密码解密,然后再用恺撒密码解密就好. 1. 6代表了栅栏密码的栏数(说实话,一开始我也没看出来 ...

  3. Inside the c++ object module 阅读摘要

    这本书是 Stanley B. Lippman于1996年所写,而最早的c++标准是 ISO/IEC 14882:1998[18],即C++98. Chapter 1: Object Lessons ...

  4. laravel5.5 使用alipay SDK报错Cannot redeclare Encrypt() (previously declared in ../vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:448)

    错误现象: 在laravel5.5 中,使用alipaySDK 报错: Cannot redeclare Encrypt() (previously declared in ../vendor/lar ...

  5. SAP ABAP: Error Message "Statement already exist" when creating a function module.

    https://archive.sap.com/discussions/thread/1089149     First check above link where my problem is so ...

  6. php对接app支付宝支付出错Cannot redeclare Decrypt()

    报错原因: alipaySDK中定义的Encrypt()/Decrypt()函数与Laravel中定义的Encrypt()/Decrypt()函数重名了. 解决办法: 修改alipaySDK中定义的函 ...

  7. FEE Development Essentials

    FEE Development Essentials JS Basic function call() and apply() func1.bind(thisObj,arg1...argn) Cust ...

  8. Python:渗透测试开源项目

    Python:渗透测试开源项目[源码值得精读] sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工 ...

  9. Python:渗透测试开源项目【源码值得精读】

    sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工具:HULK SSL安全扫描器:SSLyze 网 ...

随机推荐

  1. linux 安装软件

    apt-get install softname /安装软件apt-get update 是更新 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出 ...

  2. <转>用 Java 技术创建 RESTful Web 服务

    转自:https://www.ibm.com/developerworks/cn/web/wa-jaxrs/#N1017E JAX-RS:一种更为简单.可移植性更好的替代方式 Dustin Amrhe ...

  3. 去除HTML5 SUMMARY 标签前的三角形

    在CSS添加如下代码(Chrome): details summary::-webkit-details-marker { display:none; }

  4. 《剑指offer》-斐波那契数列

    大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. n<=39 这么直接的问fibonacci,显然是迭代计算.递归的问题在于重复计算,而迭代则避免了这一点:递归是自 ...

  5. VMware虚拟机 Ubuntu 16.04 安装

    第一步:VMware虚拟机 Ubuntu 16.04 安装 第二步: 解决窗口全屏问题 linux下给root用户设置密码 修改root用户的密码 $ sudo passwd root 密码会要求重复 ...

  6. POJ 1258 Agri-Net (Prim&Kruskal)

    题意:FJ想连接光纤在各个农场以便网络普及,现给出一些连接关系(给出邻接矩阵),从中选出部分边,使得整个图连通.求边的最小总花费. 思路:裸的最小生成树,本题为稠密图,Prim算法求最小生成树更优,复 ...

  7. POJ 2376 Cleaning Shifts【贪心】

    POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...

  8. 基于thinkphp的RBAC权限控制

    RBAC  Role-Based Access Control 权限控制在后台管理中是十分常见的,它的模型大体上是下面这张图的形式 我用的字段和上面不一样,图只是个示例 一个简易的权限控制模型只需要3 ...

  9. C#编码、解码

    1.HttpUtility.UrlEncode 方法: 对 URL 字符串进行编码,以便实现从 Web 服务器到客户端的可靠的 HTTP 传输.重载列表: [1]将字节数组转换为已编码的 URL 字符 ...

  10. 使用yum时报错 This system is not registered to Red Hat Subscription Management

    错误原因:使用redhat的yum源是需要注册付费的. 1.卸载RedHat自带的yum包 查看已安装的yum rpm -qa|grep yum #卸载已安装的yum rpm -qa | grep y ...