Last active
March 16, 2022 13:59
-
-
Save jiavictor/1c6238e5069ea8d9eb50501d0ac4eb17 to your computer and use it in GitHub Desktop.
Simple AES-256-CBC Test
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
using System; | |
using System.Text; | |
using System.Security.Cryptography; | |
namespace AES256 | |
{ | |
class Program | |
{ | |
private string encrypt(string clearText, string secretKey, string initVector) | |
{ | |
try | |
{ | |
AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider(); | |
aesProvider.KeySize = 256; | |
aesProvider.BlockSize = 128; | |
aesProvider.Mode = CipherMode.CBC; | |
aesProvider.Padding = PaddingMode.PKCS7; | |
byte[] byteText = Encoding.UTF8.GetBytes(clearText); | |
byte[] byteKey = Encoding.UTF8.GetBytes(secretKey.PadRight(32, '\0')); | |
if (byteKey.Length > 32) | |
{ | |
byte[] bytePass = new byte[32]; | |
Buffer.BlockCopy(byteKey, 0, bytePass, 0, 32); | |
byteKey = bytePass; | |
} | |
byte[] byteIV = Encoding.UTF8.GetBytes(initVector.PadRight(16, '\0')); | |
if (byteIV.Length > 16) | |
{ | |
byte[] byteInit = new byte[16]; | |
Buffer.BlockCopy(byteIV, 0, byteInit, 0, 16); | |
byteIV = byteInit; | |
} | |
aesProvider.Key = byteKey; | |
aesProvider.IV = byteIV; | |
byte[] byteData = aesProvider.CreateEncryptor().TransformFinalBlock(byteText, 0, byteText.Length); | |
return Convert.ToBase64String(byteData); | |
} | |
catch (Exception ex) | |
{ | |
return ex.Message; | |
} | |
} | |
private string decrypt(string data, string secretKey, string initVector) | |
{ | |
try | |
{ | |
AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider(); | |
aesProvider.KeySize = 256; | |
aesProvider.BlockSize = 128; | |
aesProvider.Mode = CipherMode.CBC; | |
aesProvider.Padding = PaddingMode.PKCS7; | |
byte[] byteData = Convert.FromBase64String(data); | |
byte[] byteKey = Encoding.UTF8.GetBytes(secretKey.PadRight(32, '\0')); | |
if (byteKey.Length > 32) | |
{ | |
byte[] bytePass = new byte[32]; | |
Buffer.BlockCopy(byteKey, 0, bytePass, 0, 32); | |
byteKey = bytePass; | |
} | |
byte[] byteIV = Encoding.UTF8.GetBytes(initVector.PadRight(16, '\0')); | |
if (byteIV.Length > 16) | |
{ | |
byte[] byteInit = new byte[16]; | |
Buffer.BlockCopy(byteIV, 0, byteInit, 0, 16); | |
byteIV = byteInit; | |
} | |
aesProvider.Key = byteKey; | |
aesProvider.IV = byteIV; | |
byte[] byteText = aesProvider.CreateDecryptor().TransformFinalBlock(byteData, 0, byteData.Length); | |
return Encoding.UTF8.GetString(byteText); | |
} | |
catch (Exception ex) | |
{ | |
return ex.Message; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
string clearText = "BSプレミアム20日放送"; | |
string secretKey = "SecretKey"; | |
string initVector = "InitVector"; | |
string data = new AES256.Program().encrypt(clearText, secretKey, initVector); | |
clearText = new AES256.Program().decrypt(data, secretKey, initVector); | |
Console.WriteLine("Encrypted String: " + data); | |
Console.WriteLine(clearText); | |
Console.ReadLine(); | |
} | |
} | |
} |
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
#!/usr/bin/env python3 | |
# coding: utf-8 | |
import base64 | |
from Crypto.Cipher import AES | |
def encrypt_aes_256(clear_text, key, iv): | |
key_byte = key.encode('utf-8') | |
key_byte = key_byte.ljust(32, "\0".encode('utf-8')) | |
if len(key_byte) > 32: | |
key_byte = key_byte[:32] | |
iv_byte = iv.encode('utf-8') | |
iv_byte = iv_byte.ljust(16, "\0".encode('utf-8')) | |
if len(iv_byte) > 16: | |
key_byte = key_byte[:16] | |
# PKCS#5 | |
pad_len = 16 - len(clear_text) % 16 | |
padding = chr(pad_len) * pad_len | |
clear_text += padding | |
cryptor = AES.new(key_byte, AES.MODE_CBC, iv_byte) | |
data = cryptor.encrypt(clear_text) | |
return base64.b64encode(data).decode('utf-8') | |
def decrypt_aes_256(data, key, iv): | |
data_byte = base64.b64decode(data.encode('utf-8')) | |
key_byte = key.encode('utf-8') | |
key_byte = key_byte.ljust(32, "\0".encode('utf-8')) | |
if len(key_byte) > 32: | |
key_byte = key_byte[:32] | |
iv_byte = iv.encode('utf-8') | |
iv_byte = iv_byte.ljust(16, "\0".encode('utf-8')) | |
if len(iv_byte) > 16: | |
key_byte = key_byte[:16] | |
cryptor = AES.new(key_byte, AES.MODE_CBC, iv_byte) | |
c_text = cryptor.decrypt(data_byte) | |
# PKCS#5 | |
pad_len = ord(c_text.decode('utf-8')[-1]) | |
clear_text = c_text.decode('utf-8')[:-pad_len] | |
return clear_text | |
def main(): | |
clear_text = "BSプレミアム20日放送" | |
key = "SecretKey" | |
iv = "InitVector" | |
data = encrypt_aes_256(clear_text, key, iv) | |
print("Encrypted String: " + data) | |
print(decrypt_aes_256(data, key, iv)) | |
if __name__ == "__main__": | |
main() |
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
echo -n "BSプレミアム20日放送" | openssl enc -e -aes-256-cbc -K "5365637265744B65790000000000000000000000000000000000000000000000" -iv "496E6974566563746F72000000000000" -nosalt -a | |
echo "uatdkMyXtxSIgbvVthf3mYfYbyAOkZDYy+/eGbw7ukA=" | openssl enc -d -aes-256-cbc -K "5365637265744B65790000000000000000000000000000000000000000000000" -iv "496E6974566563746F72000000000000" -nosalt -a |
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
/* | |
* You need to install | |
* Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files | |
* for using 256-bit key | |
*/ | |
import java.util.Arrays; | |
import java.util.Base64; | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
// import javax.xml.bind.DatatypeConverter; | |
public class AES256Test { | |
private String encrypt(String clearText, String secretKey, String initVector) { | |
try { | |
byte[] bytePass = secretKey.getBytes("utf-8"); | |
byte[] byteV = initVector.getBytes("utf-8"); | |
byte[] byteKey = Arrays.copyOf(bytePass, 32); | |
byte[] byteIV = Arrays.copyOf(byteV, 16); | |
// System.out.println(DatatypeConverter.printHexBinary(byteKey)); | |
// System.out.println(DatatypeConverter.printHexBinary(byteIV)); | |
SecretKeySpec skeySpec = new SecretKeySpec(byteKey, "AES"); | |
IvParameterSpec ivSpec = new IvParameterSpec(byteIV); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); | |
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec); | |
byte[] byteText = clearText.getBytes("utf-8"); | |
byte[] buf = cipher.doFinal(byteText); | |
byte[] byteBase64 = Base64.getEncoder().encode(buf); | |
String data = new String(byteBase64); | |
return data; | |
} | |
catch(Exception ex) { | |
return ex.getMessage(); | |
} | |
} | |
private String decrypt(String data, String secretKey, String initVector) { | |
try { | |
byte[] byteData = Base64.getDecoder().decode(data.getBytes("utf-8")); | |
byte[] bytePass = secretKey.getBytes("utf-8"); | |
byte[] byteV = initVector.getBytes("utf-8"); | |
byte[] byteKey = Arrays.copyOf(bytePass, 32); | |
byte[] byteIV = Arrays.copyOf(byteV, 16); | |
SecretKeySpec skeySpec = new SecretKeySpec(byteKey, "AES"); | |
IvParameterSpec ivSpec = new IvParameterSpec(byteIV); | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); | |
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec); | |
byte[] byteText = cipher.doFinal(byteData); | |
String clearText = new String(byteText); | |
return clearText; | |
} | |
catch(Exception ex) { | |
return ex.getMessage(); | |
} | |
} | |
public static void main(String[] args) { | |
String clearText = "BSプレミアム20日放送"; | |
String secretKey = "SecretKey"; | |
String initVector = "InitVector"; | |
String data = new AES256Test().encrypt(clearText, secretKey, initVector); | |
clearText = new AES256Test().decrypt(data, secretKey, initVector); | |
System.out.println("Encrypted String: " + data); | |
System.out.println(clearText); | |
} | |
} |
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
<?php | |
function encrypt_aes256($clear_text, $key, $iv) { | |
$iv = str_pad($iv, 16, "\0"); | |
$encrypt_text = openssl_encrypt($clear_text, "AES-256-CBC", $key, OPENSSL_RAW_DATA, $iv); | |
$data = base64_encode($encrypt_text); | |
return $data; | |
} | |
function decrypt_aes256($data, $key, $iv) { | |
$iv = str_pad($iv, 16, "\0"); | |
$encrypt_text = base64_decode($data); | |
$clear_text = openssl_decrypt($encrypt_text, "AES-256-CBC", $key, OPENSSL_RAW_DATA, $iv); | |
return $clear_text; | |
} | |
$clear_text = "BSプレミアム20日放送"; | |
$key = "SecretKey"; | |
$iv = "InitVector"; | |
$data = encrypt_aes256($clear_text, $key, $iv); | |
echo "Encrypted String: ".$data."\n"; | |
echo decrypt_aes256($data, $key, $iv)."\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment