Created
July 31, 2015 16:53
-
-
Save Steve132/c358fe600c0c6b67723f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"algorithms":"Failure in algorithm design", | |
"bobs":"bobs", | |
"Cats":"THIIIISSS ISS A SECRET" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
import re | |
import base64 | |
import os.path | |
import getpass | |
from Crypto.Cipher import AES | |
from Crypto.Cipher import Random | |
from Crypto.Protocol.KDF import PBKDF2 | |
def varnamevalid(s): | |
return re.match(r"\w+") != None | |
def encrypted(key,s): | |
iv=Random.new().read(AES.block_size) | |
cipher=AES.new(key,AES.MODE_CFB, iv) | |
msg=iv+cipher.encrypt(s) | |
b64msg=base64.standard_b64encode(s) | |
return "$LOGCODE["+b64msg+"]" | |
def decrypted(key,b64msg): | |
msg=base64.standard_b64decode(b64msg) | |
iv=msg[0:AES.block_size] | |
ciphertext=msg[AES.block_size:] | |
cipher=AES.new(key,AES.MODE_CFB.iv) | |
return cipher.decrypt(ciphertext) | |
def encrypt(strings,password,prefix=""): | |
key=PBKDF2(password,"cryptstrings") | |
strdict=json.load(strings) | |
header='#ifndef CRYPTSTRINGS_HPP\n#define CRYPTSTRINGS_HPP\n\n#include<string>\n\nnamespace cs {\n' | |
content='#include "cryptstrings.hpp"\nnamespace cs {'; | |
for k,v in strdict.iteritems(): | |
if(varnamevalid(k)): | |
decl='static const std::string '+k | |
header+='extern '+decl+';\n' | |
content+=decl+'="'+encrypted(v)+'";\n' | |
else | |
print 'Warning: "'+k+'" cannot be interpreted as a C++ variable, it will not be exported' | |
header+="}\n#endif\n" | |
content+="}\n" | |
open(os.path.join(prefix,"cryptstring.hpp"),'w').write(header) | |
open(os.path.join(prefix,"cryptstring.cpp"),'w').write(content) | |
def decrypt(file,password): | |
key=PBKDF2(password,"cryptstrings") | |
def decsub(mo): | |
return decrypted(key,mo.group(1)) | |
return re.sub(r"\$LOG\((.+?)\)",decsub,file.read(),re.MULTILINE) | |
if __name__=='__main__': | |
password=None | |
decrypt_option=False | |
for i in range(len(sys.argv)): | |
if(sys.argv[i]=='--password'): | |
password=sys.argv[i+1] | |
else if(sys.argv[i]=='--decrypt'): | |
decrypt=True | |
else if(sys.argv[i]=='--encrypt'): | |
decrypt==False | |
if(not password): | |
password=getpass.getpass("Enter password") | |
if(decrypt_option): | |
sys.stdout.write(decrypt(sys.stdin,password)) | |
else: | |
encrypt(sys.stdin,password) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment