Skip to content

Commit c29c4c5

Browse files
committed
add easy-encryption as files
1 parent bb2aed0 commit c29c4c5

File tree

15 files changed

+456
-0
lines changed

15 files changed

+456
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [philipperemy]

contrib/easy-encryption/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app
33+
34+
a.out

contrib/easy-encryption/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Philippe Rémy
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

contrib/easy-encryption/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Easy Encryption
2+
A very simple standalone C++ module (API) to obfuscate/deobfuscate strings based on B64 and Vigenere ciper (symmetric cipher).
3+
4+
**DISCLAIMER: This encryption is NOT secure and can be used as a "cheap way" to obfuscate some messages in a communication channel. If you need a solid and unbreakable encryption, please use a widely adopted standard and well researched cipher like AES-GCM.** You can find more information there: [pyca](https://github.com/pyca/cryptography).
5+
6+
7+
It works as follows:
8+
9+
- Alice encodes in base64 the message, then uses the Vigenere private key to encrypt the message.
10+
- The encrypted message is sent through an unsecured channel.
11+
- Bob gets the message and decrypts it with the Vigenere private key. He then decodes it with base64.
12+
13+
Diagram summary:
14+
_______________________________________________________________________________________________________________
15+
**Message -> B64 ENCODE -> VIGENERE ENCRYPT -> encrypted message -> VIGENERE DECRYPT -> B64 DECODE -> Message**
16+
_______________________________________________________________________________________________________________
17+
18+
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.
19+
20+
## API
21+
22+
### C++
23+
24+
- **Encrypt message**
25+
```c++
26+
std::string encrypt(std::string& msg, std::string& key)
27+
```
28+
29+
- **Decrypt message**
30+
```c++
31+
std::string decrypt(std::string& encrypted_msg, std::string& key)
32+
```
33+
34+
### Python
35+
36+
- **Encrypt message**
37+
```python
38+
wrapper.encrypt(message, key): returns encrypted message
39+
```
40+
41+
- **Decrypt message**
42+
```python
43+
wrapper.decrypt(encrypted_message, key): returns decrypted message
44+
```
45+
46+
## Compilation and execution
47+
```bash
48+
g++ cl.cpp
49+
./a.out "Hello world" MYPRIVATEKEY 0
50+
```
51+
52+
The encrypted message is: `ttz9JqxZHBClNtu=`.
53+
54+
```bash
55+
./a.out ttz9JqxZHBClNtu= MYPRIVATEKEY 1
56+
```
57+
58+
The decrypted message is `Hello world`.
59+
60+
## Python wrapper
61+
62+
```bash
63+
rm a.out
64+
g++ cl.cpp
65+
python3 wrapper.py
66+
```
67+
68+
## Example - Encoding/Decoding JSON format
69+
70+
### Source code
71+
```c++
72+
#include <stdio.h>
73+
#include <string.h>
74+
#include <string>
75+
#include <iostream>
76+
#include <stdio.h>
77+
#include <ctype.h>
78+
#include "encrypt.h"
79+
80+
using namespace std;
81+
82+
int main() {
83+
// std::string msg = "HELLO WORLD";
84+
std::string msg = "{\"id\":1,\"method\":\"service.subscribe\",\"params\":[\"myapp/0.1c\", null,\"0.0.0.0\",\"80\"]}";
85+
std::string key = "THISISMYKEY";
86+
std::cout << " message to send: " << msg << std::endl;
87+
std::string encrypted_msg = encrypt(msg, key);
88+
std::cout << "encrypted message: " << encrypted_msg << std::endl;
89+
std::string decrypted_msg = decrypt(encrypted_msg, key);
90+
std::cout << "decrypted message: " << decrypted_msg << std::endl;
91+
return 0;
92+
}
93+
94+
```
95+
96+
### Output
97+
```
98+
message to send: {"id":1,"method":"service.subscribe","params":["myapp/0.1c", null,"0.0.0.0","80"]}
99+
encrypted message: X5g7wjjTllj1ItCxShWUb77PKJsfP VNMAB7VtqaLCccGTr0ijkjxqw0IutQvXfSFK4OKo8cnpD1Lge0pdMCZf0fqQ8bjjFjkNn1h pBtdwNJD==
100+
decrypted message: {"id":1,"method":"service.subscribe","params":["myapp/0.1c", null,"0.0.0.0","80"]}
101+
```

contrib/easy-encryption/b64.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <string>
2+
#include <sstream>
3+
#include <iostream>
4+
#include <vector>
5+
6+
using namespace std;
7+
8+
9+
static std::string base64_encode(const std::string &in) {
10+
11+
std::string out;
12+
13+
int val=0, valb=-6;
14+
for (int jj = 0; jj < in.size(); jj++) {
15+
char c = in[jj];
16+
val = (val<<8) + c;
17+
valb += 8;
18+
while (valb>=0) {
19+
out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(val>>valb)&0x3F]);
20+
valb-=6;
21+
}
22+
}
23+
if (valb>-6) out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[((val<<8)>>(valb+8))&0x3F]);
24+
while (out.size()%4) out.push_back('=');
25+
return out;
26+
}
27+
28+
static std::string base64_decode(const std::string &in) {
29+
30+
std::string out;
31+
32+
std::vector<int> T(256,-1);
33+
for (int i=0; i<64; i++) T["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
34+
35+
int val=0, valb=-8;
36+
for (int jj = 0; jj < in.size(); jj++) {
37+
char c = in[jj];
38+
if (T[c] == -1) break;
39+
val = (val<<6) + T[c];
40+
valb += 6;
41+
if (valb>=0) {
42+
out.push_back(char((val>>valb)&0xFF));
43+
valb-=8;
44+
}
45+
}
46+
return out;
47+
}
14.4 KB
Binary file not shown.
14.1 KB
Binary file not shown.
51.5 KB
Binary file not shown.

contrib/easy-encryption/cl.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <string>
4+
#include <iostream>
5+
#include <stdio.h>
6+
#include <ctype.h>
7+
#include <fstream>
8+
#include "encrypt.h"
9+
10+
using namespace std;
11+
12+
string load( const char* path )
13+
{
14+
FILE* src = fopen(path, "rb");
15+
string res;
16+
17+
fseek(src,0,SEEK_END);
18+
int size = ftell(src);
19+
rewind( src );
20+
res.resize(size);
21+
fread(const_cast<char*>(res.data()), size, 1, src);
22+
fclose(src);
23+
return res;
24+
}
25+
26+
void write( const char* path, string& str )
27+
{
28+
# if 1
29+
FILE* fp = fopen( path, "wb" );
30+
fwrite( str.c_str(), strlen( str.c_str() ), 1, fp );
31+
fclose( fp );
32+
#else
33+
ofstream ofs(path);
34+
ofs << str;
35+
ofs.close();
36+
#endif
37+
}
38+
39+
int main(int argc, char** argv)
40+
{
41+
//ee64 src.txt dst.txt key 0
42+
#if 1
43+
std::string key = argv[3];
44+
int encrypt_flag = atoi(argv[4]);
45+
46+
string src = load( argv[1] );
47+
string dst;
48+
if(encrypt_flag == 0) {
49+
dst = encrypt(src, key);
50+
} else {
51+
dst = decrypt(src, key);
52+
}
53+
54+
write( argv[2], dst );
55+
#else
56+
std::string msg = argv[1];
57+
std::string key = argv[2];
58+
int encrypt_flag = atoi(argv[3]);
59+
60+
if(encrypt_flag == 0) {
61+
std::cout << encrypt(msg, key) << std::endl;
62+
} else {
63+
std::cout << decrypt(msg, key) << std::endl;
64+
}
65+
#endif
66+
return 0;
67+
}

contrib/easy-encryption/encrypt.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "b64.h"
2+
#include "vigenere.h"
3+
4+
std::string encrypt(std::string& msg, std::string& key) {
5+
std::string b64_str = base64_encode(msg);
6+
std::string vigenere_msg = encrypt_vigenere(b64_str, key);
7+
// std::cout << vigenere_msg << std::endl;
8+
return vigenere_msg;
9+
}
10+
11+
12+
std::string decrypt(std::string& encrypted_msg, std::string& key) {
13+
std::string newKey = extend_key(encrypted_msg, key);
14+
std::string b64_encoded_str = decrypt_vigenere(encrypted_msg, newKey);
15+
std::string b64_decode_str = base64_decode(b64_encoded_str);
16+
return b64_decode_str;
17+
}

0 commit comments

Comments
 (0)