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"]}

原文地址:https://www.cnblogs.com/time-is-life/p/9221450.html

时间: 2024-08-26 04:22:22

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

很好的iOS学习资料

https://github.com/vsouza/awesome-ios 汇集了很多好的资料 https://github.com/vsouza/awesome-ios Skip to content This repository Pull requests Issues Gist You don’t have any verified emails. We recommend verifying at least one email. Email verification helps ou

OPENVPN搭建与配置

Content-type: text/html; charset=UTF-8 openvpn Section: Maintenance Commands (8)Updated: 17 November 2008Index Return to Main Contents NAME openvpn - secure IP tunnel daemon. SYNOPSIS openvpn [ options ... ] INTRODUCTION OpenVPN is an open source VPN

Python:渗透测试开源项目

Python:渗透测试开源项目[源码值得精读] sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工具:HULK SSL安全扫描器:SSLyze 网络 Scapy: send, sniff and dissect and forge network packets. Usable interactively or as a library pypcap, Pcapy and pylibpcap:

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

sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工具:HULK SSL安全扫描器:SSLyze 网络 Scapy: send, sniff and dissect and forge network packets. Usable interactively or as a library pypcap, Pcapy and pylibpcap: several different Python

Cryptographic method and system

The present invention relates to the field of security of electronic data and/or communications. In one form, the invention relates to data security and/or privacy in a distributed and/or decentralised network environment. In another form, the invent

Phalcon 加密解密组件

2.40 Encryption/Decryption 加密/解密 Phalcon provides encryption facilities via thePhalcon\Crypt component. This class offers simple object-oriented wrappers to themcrypt php's encryption library. By default, this component provides secure encryption usi

de4dot - Deobfuscator for .NET

Features Here's a pseudo random list of the things it will do depending on what obfuscator was used to obfuscate an assembly: Inline methods. Some obfuscators move small parts of a method to another static method and calls it. Decrypt strings statica

String decryption with de4dot

Introduction de4dot is a wonderful tool for deobfuscating known and unknown .NET protections. Dealing with known and supported protections is easy – drag&drop executable on de4dot and it will create deobfuscated assembly. Removing unknown protections

使用php重新实现PHP脚本引擎内置函数

// 实在无聊,突发奇想,想把PHP里面部分已经提供封装好的函数重新使用PHP实现一遍, // 于是便有了下面的代码主要实现了PHP中部分字符串处理函数,同时实现了一些PHP中 // 没有,但是同样有作用的字符串处理函数同样的这些函数,也能够使用其他语言来实 // 现,比如用C/VBScript/Perl等等,那么你就能够有一个自己的函数库. // 以下函数不一定能够成功运行,只是为了学习而已. // // 如果无特别声明,全部是由于heiyeluren原创,要使用任何函数都请保留作者信息 /*