Created
July 6, 2017 13:12
-
-
Save DonnchaC/653b9bca2a045d2657007100c47633e3 to your computer and use it in GitHub Desktop.
CryptNote is an encryption app for Windows, Android and Linux alleged by Turkey to be used by "FETÖ" - http://www.aksam.com.tr/guncel/fetonun-yeni-haberlesme-agi-cryptnote-desifre-edildi/haber-639566
This file contains hidden or 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
""" | |
Cryptnote encryption algorithim | |
""" | |
import hashlib | |
import base64 | |
from binascii import unhexlify | |
from Crypto.Cipher import AES | |
""" | |
public OpensslAes128PasswordEncrypter(String password, String charset) throws NoSuchAlgorithmException, NoSuchPaddingException { | |
this.charset = charset; | |
byte[] iv = makeIvFromPassword(password); | |
byte[] key = makeKeyFromPassword(password); | |
this.paramSpec = new IvParameterSpec(iv); | |
this.keySpec = new SecretKeySpec(key, "AES"); | |
this.cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
} | |
private byte[] makeKeyFromPassword(String password) throws NoSuchAlgorithmException { | |
String sha = StringUtil.encode("SHA", password).substring(0, 16); | |
String keyString = ""; | |
for (int i = 0; i < 16; i++) { | |
keyString = keyString + "0" + sha.substring(i, i + 1); | |
} | |
return StringUtil.hexStringToByteArray(keyString); | |
} | |
private byte[] makeIvFromPassword(String password) throws NoSuchAlgorithmException { | |
String md5 = StringUtil.encode("MD5", password).substring(0, 16); | |
String ivString = ""; | |
for (int i = 0; i < 16; i++) { | |
ivString = ivString + "0" + md5.substring(i, i + 1); | |
} | |
return StringUtil.hexStringToByteArray(ivString); | |
} | |
public static String encode(String messageDigest, String string) | |
throws NoSuchAlgorithmException { | |
byte[] plainText = string.getBytes(); | |
MessageDigest md = MessageDigest.getInstance(messageDigest); | |
md.reset(); | |
md.update(plainText); | |
byte[] encodedPassword = md.digest(); | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < encodedPassword.length; i++) { | |
if ((encodedPassword[i] & 0xFF) < 16) { | |
sb.append("0"); | |
} | |
sb.append(Long.toString(encodedPassword[i] & 0xFF, 16)); | |
} | |
return sb.toString(); | |
} | |
} | |
""" | |
def pkcs5_pad(s): | |
return s + (16 - len(s) % 16) * chr(16 - len(s) % 16) | |
def pkcs5_unpad(s): | |
return s[0:-ord(s[-1])] | |
def makeKey(digest, password): | |
# Cryptnote only uses 16 hex digests, 64 bit key!!! | |
# Every four bits padded with 0000 to make a "128 bit" | |
# key from 64 bits of key material. | |
sha_hex = stringEncode(digest, password)[0:16] | |
keystring = [("0" + byte) for byte in sha_hex] | |
return unhexlify("".join(keystring)) | |
def makeKeyFromPassword(password): | |
return makeKey("sha1", password) | |
def makeIVFromPassword(password): | |
return makeKey("md5", password) | |
def stringEncode(messageDigest, plainText): | |
md = hashlib.new(messageDigest) | |
md.update(plainText.encode("utf-8")) | |
return md.hexdigest() | |
def get_cipher(password): | |
key = makeKeyFromPassword(password) | |
iv = makeIVFromPassword(password) | |
return AES.new(key, AES.MODE_CBC, iv) | |
def encrypt(password, message): | |
cipher = get_cipher(password) | |
encrypted = base64.encodestring(cipher.encrypt(pkcs5_pad(data))) | |
return "\n".join(["---- BEGIN CRYPTNOTE CONTENT ----", | |
encrypted.rstrip("\n"), | |
"---- END CRYPTNOTE CONTENT ----"]) | |
def decrypt(password, message): | |
data = base64.b64decode("".join(message.splitlines()[1:-1])) | |
cipher = get_cipher(password) | |
return pkcs5_unpad(cipher.decrypt(data)) | |
if __name__ == "__main__": | |
password = "password" | |
encrypted = """---- BEGIN CRYPTNOTE CONTENT ---- | |
oZQa5Qe/ndieWsj1k8l81d1jRy0REL63rbup1Hb5b4E= | |
---- END CRYPTNOTE CONTENT ----""" | |
print(decrypt(password, encrypted)) | |
data = " A new message" | |
print(encrypt(password, data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment