class CryptUtils
{
private static String TRANSFORMATION
= "AES/CBC/PKCS5Padding"; private static String ALGORITHM
= "AES"; private static String DIGEST
= "SHA-256"; private static Cipher _cipher;
private static SecretKey _password;
private static IvParameterSpec _IVParamSpec;
//16-byte private key
private static byte[] iv = "radomaesvectors".getBytes();
byte[] decryptedVal = null;
try {
//Encode digest
byte[] bytehash = digest.digest(passKey.getBytes(StandardCharsets.UTF_8));
Formatter formatter = new Formatter();
for (byte myByte: bytehash) {
strHash="";
strHash
= String.
format("%02x", myByte
); formatter.format(StringUtils.leftPad(strHash, 2, "0"));
}
strHash = formatter.toString();
if (strHash.length() > 32) {
strHash = strHash.substring(0, 32);
}
byte[] bytekeytemp = strHash.getBytes(StandardCharsets.UTF_8);
byte[] bytekey = new byte[32];
System.
arraycopy(bytekeytemp,
0, bytekey,
0,
32);
_password = new SecretKeySpec(bytekey, ALGORITHM);
//Initialize objects
_cipher = Cipher.getInstance(TRANSFORMATION);
_IVParamSpec = new IvParameterSpec(iv);
if (Mode.equals("ENCRYPT")){
_cipher.init(Cipher.ENCRYPT_MODE, _password, _IVParamSpec);
byte[] ciphertext = _cipher.doFinal(cryptText.getBytes(StandardCharsets.UTF_8));
byte[] encodedValue = Base64.getEncoder().encode(ciphertext);
encryptedVal = encryptedVal.replace("+", "-");
encryptedVal = encryptedVal.replace("/", "_");
retText = encryptedVal;
}
else {
_cipher.init(Cipher.DECRYPT_MODE, _password, _IVParamSpec);
cryptText = cryptText.replace("-", "+");
cryptText = cryptText.replace("_", "/");
byte[] decodedValue =
Base64.getDecoder().decode(cryptText.getBytes(StandardCharsets.UTF_8));
decryptedVal = _cipher.doFinal(decodedValue);
retText
= new String(decryptedVal
);
}
throw new Exception("Encryption error, No such algorithm ->" + ALGORITHM
); } catch (NoSuchPaddingException e) {
throw new Exception("Encryption error, No such padding PKCS7"); throw new Exception("Encryption error:" + exceptionToString
(e
)); }
return retText;
}
import Crypto.Cipher.AES
import Crypto.Random
import base64
import binascii
import hashlib
class Cipher_AES:
pad_default = lambda x, y: x + (y - len(x) % y) * " ".encode("utf-8")
unpad_default = lambda x: x.rstrip()
pad_user_defined = lambda x, y, z: x + (y - len(x) % y) * z.encode("utf-8")
unpad_user_defined = lambda x, z: x.rstrip(z)
pad_pkcs5 = lambda x, y: x + (y - len(x) % y) * chr(y - len(x) % y).encode("utf-8")
# pad_pkcs5=lambda x, y: "0".encode('utf-8') *(y - len(x) % y) + x
unpad_pkcs5 = lambda x: x[:-ord(x[-1])]
# unpad_pkcs5=lambda x: x[-ord(x[-1]):]
def __init__
(self, key
="abcdefgh12345678", iv
=Crypto.
Random.
new().
read(Crypto.
Cipher.
AES.
block_size)): self.__key = key
self.__iv = iv
def set_key(self, key):
self.__key = key
def get_key(self):
return self.__key
def set_iv(self, iv):
self.__iv = iv
def get_iv(self):
return self.__iv
def Cipher_MODE_ECB(self):
self.__x = Crypto.Cipher.AES.new(self.__key.encode("utf-8"), Crypto.Cipher.AES.MODE_ECB)
def Cipher_MODE_CBC(self):
self.__x = Crypto.Cipher.AES.new(self.__key.encode("utf-8"), Crypto.Cipher.AES.MODE_CBC,self.__iv.encode("utf-8"))
def encrypt(self, text, cipher_method, pad_method="", code_method=""):
if cipher_method.upper() == "MODE_ECB":
self.Cipher_MODE_ECB()
elif cipher_method.upper() == "MODE_CBC":
self.Cipher_MODE_CBC()
cipher_text = b"".join([self.__x.encrypt(i) for i in self.text_verify(text.encode("utf-8"), pad_method)])
if code_method.lower() == "base64":
return base64.encodebytes(cipher_text).decode("utf-8").rstrip()
elif code_method.lower() == "hex":
return binascii.b2a_hex(cipher_text).decode("utf-8").rstrip()
else:
return cipher_text.decode("utf-8").rstrip()
def decrypt(self, cipher_text, cipher_method, pad_method="", code_method=""):
if cipher_method.upper() == "MODE_ECB":
self.Cipher_MODE_ECB()
elif cipher_method.upper() == "MODE_CBC":
self.Cipher_MODE_CBC()
if code_method.lower() == "base64":
cipher_text = base64.decodebytes(cipher_text.encode("utf-8"))
elif code_method.lower() == "hex":
cipher_text = binascii.a2b_hex(cipher_text.encode("utf-8"))
else:
cipher_text = cipher_text.encode("utf-8")
return self.unpad_method(self.__x.decrypt(cipher_text).decode("utf-8"), pad_method)
def text_verify(self, text, method):
while len(text) > len(self.__key):
text_slice = text[:len(self.__key)]
text = text[len(self.__key):]
yield text_slice
else:
if len(text) == len(self.__key):
yield text
else:
yield self.pad_method(text, method)
def pad_method(self, text, method):
if method == "":
return Cipher_AES.pad_default(text, len(self.__key))
elif method == "PKCS5Padding":
return Cipher_AES.pad_pkcs5(text, len(self.__key))
else:
return Cipher_AES.pad_user_defined(text, len(self.__key), method)
def unpad_method(self, text, method):
if method == "":
return Cipher_AES.unpad_default(text)
elif method == "PKCS5Padding":
return Cipher_AES.unpad_pkcs5(text)
else:
return Cipher_AES.unpad_user_defined(text, method)
def main2(msg, token):
iv = "globalaesvectors"
key = token
cipher = Cipher_AES(key=key, iv=iv)
cipher_method = "MODE_CBC"
pad_method = "PKCS5Padding"
code_method = "base64"
encrypted_payload = cipher.encrypt(payload, cipher_method=cipher_method, pad_method=pad_method,
code_method=code_method)
decode = cipher.decrypt(encrypted_payload, cipher_method,pad_method, code_method)
print(encrypted_payload)
return encrypted_payload
if __name__ == '__main__':
import hashlib
import json
payload ={} # some payload
import json
key = '' # key
token = hashlib.sha256(key.encode('utf-8')).hexdigest()[:32]
payload = json.dumps(payload)
x = main2(payload, token)