Created
October 24, 2021 20:52
-
-
Save felipefpx/6342f305314293a46079f62921fe179f to your computer and use it in GitHub Desktop.
AES/CBC/PKCS5Padding
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
import os | |
import base64 | |
import hashlib | |
from Crypto import Random | |
from Crypto.Cipher import AES | |
class AESCipher: | |
def __init__(self, key=KEY): | |
self.bs = 16 | |
self.key = hashlib.sha256(key.encode()).digest() | |
def encrypt(self, message): | |
message = self._pad(message) | |
iv = Random.new().read(AES.block_size) | |
cipher = AES.new(self.key, AES.MODE_CBC, iv) | |
return base64.b64encode(iv + cipher.encrypt(message)).decode('utf-8') | |
def decrypt(self, enc): | |
enc = base64.b64decode(enc) | |
iv = enc[:AES.block_size] | |
cipher = AES.new(self.key, AES.MODE_CBC, iv) | |
return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8') | |
def _pad(self, s): | |
return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs) | |
@staticmethod | |
def _unpad(s): | |
return s[:-ord(s[len(s)-1:])] |
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
import javax.crypto.BadPaddingException; | |
import javax.crypto.Cipher; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.security.*; | |
import java.security.spec.AlgorithmParameterSpec; | |
import java.util.Arrays; | |
import java.util.Base64; | |
public class AESCipher { | |
private static final String characterEncoding = "UTF-8"; | |
private static final String cipherTransformation = "AES/CBC/PKCS5Padding"; | |
private static final String aesEncryptionAlgorithm = "AES"; | |
public static String encrypt(final String plain, final String key) | |
throws InvalidKeyException, | |
NoSuchAlgorithmException, | |
NoSuchPaddingException, | |
IllegalBlockSizeException, | |
BadPaddingException, | |
IOException { | |
return Base64.getEncoder().encodeToString(encrypt(plain.getBytes(), key)); | |
} | |
public static String decrypt(final String plain, final String key) | |
throws InvalidKeyException, | |
NoSuchAlgorithmException, | |
NoSuchPaddingException, | |
InvalidAlgorithmParameterException, | |
IllegalBlockSizeException, | |
BadPaddingException, | |
IOException { | |
byte[] encryptedBytes = decrypt(Base64.getDecoder().decode(plain.getBytes(StandardCharsets.UTF_8)), key); | |
return new String(encryptedBytes); | |
} | |
public static byte[] encrypt(byte[] mes, final String key) | |
throws NoSuchAlgorithmException, | |
NoSuchPaddingException, | |
InvalidKeyException, | |
IllegalBlockSizeException, | |
BadPaddingException, IOException { | |
byte[] keyBytes = key.getBytes(characterEncoding); | |
MessageDigest md = MessageDigest.getInstance("SHA-256"); | |
md.update(keyBytes); | |
keyBytes = md.digest(); | |
SecretKeySpec newKey = new SecretKeySpec(keyBytes, aesEncryptionAlgorithm); | |
Cipher cipher = Cipher.getInstance(cipherTransformation); | |
SecureRandom random = new SecureRandom(); | |
byte[] ivBytes = new byte[16]; | |
random.nextBytes(ivBytes); | |
cipher.init(Cipher.ENCRYPT_MODE, newKey, random); | |
byte[] destination = new byte[ivBytes.length + mes.length]; | |
System.arraycopy(ivBytes, 0, destination, 0, ivBytes.length); | |
System.arraycopy(mes, 0, destination, ivBytes.length, mes.length); | |
return cipher.doFinal(destination); | |
} | |
public static byte[] decrypt(byte[] bytes, final String key) | |
throws NoSuchAlgorithmException, | |
NoSuchPaddingException, | |
InvalidKeyException, | |
InvalidAlgorithmParameterException, | |
IllegalBlockSizeException, | |
BadPaddingException, IOException { | |
byte[] keyBytes = key.getBytes(characterEncoding); | |
MessageDigest md = MessageDigest.getInstance("SHA-256"); | |
md.update(keyBytes); | |
keyBytes = md.digest(); | |
byte[] ivB = Arrays.copyOfRange(bytes, 0, 16); | |
byte[] codB = Arrays.copyOfRange(bytes, 16, bytes.length); | |
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivB); | |
SecretKeySpec newKey = new SecretKeySpec(keyBytes, aesEncryptionAlgorithm); | |
Cipher cipher = Cipher.getInstance(cipherTransformation); | |
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec); | |
return cipher.doFinal(codB); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment