2017/6/5 Update: Added C# implement
Triple-DES-ECB (3DES-ECB)
| using System; | |
| using System.Text; | |
| using System.Security.Cryptography; | |
| namespace TripleDES | |
| { | |
| class Program | |
| { | |
| private string encrypt(string clearText, string secretKey) | |
| { | |
| try | |
| { | |
| TripleDESCryptoServiceProvider tripleDESProvider = new TripleDESCryptoServiceProvider(); | |
| byte[] byteKey = Encoding.UTF8.GetBytes(secretKey.PadRight(24, '\0')); | |
| if(byteKey.Length > 24) | |
| { | |
| byte[] bytePass = new byte[24]; | |
| Buffer.BlockCopy(byteKey, 0, bytePass, 0, 24); | |
| byteKey = bytePass; | |
| } | |
| byte[] byteText = Encoding.UTF8.GetBytes(clearText); | |
| tripleDESProvider.Key = byteKey; | |
| tripleDESProvider.Mode = CipherMode.ECB; | |
| byte[] byteMessage = tripleDESProvider.CreateEncryptor().TransformFinalBlock(byteText, 0, byteText.Length); | |
| return Convert.ToBase64String(byteMessage); | |
| } | |
| catch(Exception ex) | |
| { | |
| return ex.Message; | |
| } | |
| } | |
| private string decrypt(string data, string secretKey) | |
| { | |
| try | |
| { | |
| byte[] byteData = Convert.FromBase64String(data); | |
| byte[] byteKey = Encoding.UTF8.GetBytes(secretKey.PadRight(24, '\0')); | |
| if (byteKey.Length > 24) | |
| { | |
| byte[] bytePass = new byte[24]; | |
| Buffer.BlockCopy(byteKey, 0, bytePass, 0, 24); | |
| byteKey = bytePass; | |
| } | |
| TripleDESCryptoServiceProvider tripleDESProvider = new TripleDESCryptoServiceProvider(); | |
| tripleDESProvider.Key = byteKey; | |
| tripleDESProvider.Mode = CipherMode.ECB; | |
| byte[] byteText = tripleDESProvider.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 data = new TripleDES.Program().encrypt(clearText, secretKey); | |
| clearText = new TripleDES.Program().decrypt(data, secretKey); | |
| Console.WriteLine("Encrypted String: " + data); | |
| Console.WriteLine(clearText); | |
| Console.ReadLine(); | |
| } | |
| } | |
| } |
| #!/usr/bin/env python3 | |
| # coding: utf-8 | |
| import base64 | |
| import pyDes | |
| from Crypto.Cipher import DES3 | |
| def encrypt_3des(clear_text, key): | |
| clear_text_byte = clear_text.encode('utf-8') | |
| key_byte = key.encode('utf-8') | |
| key_byte = key_byte.ljust(24, "\0".encode('utf-8')) | |
| if len(key_byte) > 24: | |
| key_byte = key_byte[:24] | |
| k = pyDes.triple_des(key_byte, pyDes.ECB, IV = None, pad = None, padmode = pyDes.PAD_PKCS5) | |
| d = k.encrypt(clear_text_byte) | |
| return base64.b64encode(d).decode('utf-8') | |
| def encrypt_3des_crypto(clear_text, key): | |
| key_byte = key.encode('utf-8') | |
| key_byte = key_byte.ljust(24, "\0".encode('utf-8')) | |
| if len(key_byte) > 24: | |
| key_byte = key_byte[:24] | |
| # PKCS#5 | |
| pad_len = 8 - len(clear_text) % 8 | |
| padding = chr(pad_len) * pad_len | |
| clear_text += padding | |
| cryptor = DES3.new(key_byte, DES3.MODE_ECB) | |
| data = cryptor.encrypt(clear_text) | |
| return base64.b64encode(data).decode('utf-8') | |
| def decrypt_3des(data, key): | |
| data_byte = base64.b64decode(data.encode('utf-8')) | |
| key_byte = key.encode('utf-8') | |
| key_byte = key_byte.ljust(24, "\0".encode('utf-8')) | |
| if len(key_byte) > 24: | |
| key_byte = key_byte[:24] | |
| k = pyDes.triple_des(key_byte, pyDes.ECB, IV = None, pad = None, padmode = pyDes.PAD_PKCS5) | |
| d = k.decrypt(data_byte) | |
| return d.decode('utf-8') | |
| def decrypt_3des_crypto(data, key): | |
| data_byte = base64.b64decode(data.encode('utf-8')) | |
| key_byte = key.encode('utf-8') | |
| key_byte = key_byte.ljust(24, "\0".encode('utf-8')) | |
| if len(key_byte) > 24: | |
| key_byte = key_byte[:24] | |
| cryptor = DES3.new(key_byte, DES3.MODE_ECB) | |
| 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" | |
| data = encrypt_3des(clear_text, key) | |
| print("Encrypted String: " + data) | |
| print(decrypt_3des(data, key)) | |
| data = encrypt_3des_crypto(clear_text, key) | |
| print("Encrypted String: " + data) | |
| print(decrypt_3des_crypto(data, key)) | |
| if __name__ == "__main__": | |
| main() |
| echo -n "BSプレミアム20日放送" | openssl enc -e -des-ede3 -K "5365637265744B6579000000000000000000000000000000" -nosalt -a | |
| echo "tpGP6UtYB5MLZwFG89V+TEIzM2CB8xMC+AFgZr4sezU=" | openssl enc -d -des-ede3 -K "5365637265744B6579000000000000000000000000000000" -nosalt -a |
| import java.util.Arrays; | |
| import java.util.Base64; | |
| import javax.crypto.Cipher; | |
| import javax.crypto.SecretKey; | |
| import javax.crypto.spec.SecretKeySpec; | |
| // import javax.xml.bind.DatatypeConverter; | |
| public class TripleDESTest { | |
| private String encrypt(String clearText, String secretKey) { | |
| try { | |
| byte[] bytePass = secretKey.getBytes("utf-8"); | |
| byte[] byteKey = Arrays.copyOf(bytePass, 24); | |
| // System.out.println(DatatypeConverter.printHexBinary(byteKey)); | |
| SecretKey key = new SecretKeySpec(byteKey, "DESede"); | |
| Cipher cipher = Cipher.getInstance("DESede"); | |
| cipher.init(Cipher.ENCRYPT_MODE, key); | |
| 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) { | |
| try { | |
| byte[] byteData = Base64.getDecoder().decode(data.getBytes("utf-8")); | |
| byte[] bytePass = secretKey.getBytes("utf-8"); | |
| byte[] byteKey = Arrays.copyOf(bytePass, 24); | |
| SecretKey key = new SecretKeySpec(byteKey, "DESede"); | |
| Cipher cipher = Cipher.getInstance("DESede"); | |
| cipher.init(Cipher.DECRYPT_MODE, key); | |
| byte[] byteText = cipher.doFinal(byteData); | |
| String clearText = new String(byteText, "utf-8"); | |
| return clearText; | |
| } | |
| catch(Exception ex) { | |
| return ex.getMessage(); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| String clearText = "BSプレミアム20日放送"; | |
| String secretKey = "SecretKey"; | |
| String data = new TripleDESTest().encrypt(clearText, secretKey); | |
| clearText = new TripleDESTest().decrypt(data, secretKey); | |
| System.out.println("Encrypted String: " + data); | |
| System.out.println(clearText); | |
| } | |
| } |
| <?php | |
| function encrypt_3des($clear_text, $key) { | |
| $encrypt_text = openssl_encrypt($clear_text, "DES-EDE3", $key, OPENSSL_RAW_DATA, ""); | |
| $data = base64_encode($encrypt_text); | |
| return $data; | |
| } | |
| function decrypt_3des($data, $key) { | |
| $encrypt_text = base64_decode($data); | |
| $clear_text = openssl_decrypt($encrypt_text, "DES-EDE3", $key, OPENSSL_RAW_DATA, ""); | |
| return $clear_text; | |
| } | |
| $clear_text = "BSプレミアム20日放送"; | |
| $key = "SecretKey"; | |
| $data = encrypt_3des($clear_text, $key); | |
| echo "Encrypted String: ".encrypt_3des($clear_text, $key)."\n"; | |
| echo decrypt_3des($data, $key)."\n"; | |
| ?> |
Output:
Encrypted String: tpGP6UtYB5MLZwFG89V+TEIzM2CB8xMC+AFgZr4sezU=
BSプレミアム20日放送
AES-128-CBC
| using System; | |
| using System.Text; | |
| using System.Security.Cryptography; | |
| namespace AES128 | |
| { | |
| class Program | |
| { | |
| private string encrypt(string clearText, string secretKey, string initVector) | |
| { | |
| try | |
| { | |
| AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider(); | |
| aesProvider.KeySize = 128; | |
| aesProvider.BlockSize = 128; | |
| aesProvider.Mode = CipherMode.CBC; | |
| aesProvider.Padding = PaddingMode.PKCS7; | |
| byte[] byteText = Encoding.UTF8.GetBytes(clearText); | |
| byte[] byteKey = Encoding.UTF8.GetBytes(secretKey.PadRight(16, '\0')); | |
| if(byteKey.Length > 16) | |
| { | |
| byte[] bytePass = new byte[16]; | |
| Buffer.BlockCopy(byteKey, 0, bytePass, 0, 16); | |
| 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 = 128; | |
| aesProvider.BlockSize = 128; | |
| aesProvider.Mode = CipherMode.CBC; | |
| aesProvider.Padding = PaddingMode.PKCS7; | |
| byte[] byteData = Convert.FromBase64String(data); | |
| byte[] byteKey = Encoding.UTF8.GetBytes(secretKey.PadRight(16, '\0')); | |
| if(byteKey.Length > 16) | |
| { | |
| byte[] bytePass = new byte[16]; | |
| Buffer.BlockCopy(byteKey, 0, bytePass, 0, 16); | |
| 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 AES128.Program().encrypt(clearText, secretKey, initVector); | |
| clearText = new AES128.Program().decrypt(data, secretKey, initVector); | |
| Console.WriteLine("Encrypted String: " + data); | |
| Console.WriteLine(clearText); | |
| Console.ReadLine(); | |
| } | |
| } | |
| } |
| #!/usr/bin/env python3 | |
| # coding: utf-8 | |
| import base64 | |
| from Crypto.Cipher import AES | |
| def encrypt_aes_128(clear_text, key, iv): | |
| key_byte = key.encode('utf-8') | |
| key_byte = key_byte.ljust(16, "\0".encode('utf-8')) | |
| if len(key_byte) > 16: | |
| key_byte = key_byte[:16] | |
| 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_128(data, key, iv): | |
| data_byte = base64.b64decode(data.encode('utf-8')) | |
| key_byte = key.encode('utf-8') | |
| key_byte = key_byte.ljust(16, "\0".encode('utf-8')) | |
| if len(key_byte) > 16: | |
| key_byte = key_byte[:16] | |
| 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_128(clear_text, key, iv) | |
| print("Encrypted String: " + data) | |
| print(decrypt_aes_128(data, key, iv)) | |
| if __name__ == "__main__": | |
| main() |
| echo -n "BSプレミアム20日放送" | openssl enc -e -aes-128-cbc -K "5365637265744B657900000000000000" -iv "496E6974566563746F72000000000000" -nosalt -a | |
| echo "zM+I3ulxLl4Pna0FgwGKZcQHXCBcO1hzTtKmf2n36vk=" | openssl enc -d -aes-128-cbc -K "5365637265744B657900000000000000" -iv "496E6974566563746F72000000000000" -nosalt -a |
| 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 AES128Test { | |
| 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, 16); | |
| 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, 16); | |
| 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 AES128Test().encrypt(clearText, secretKey, initVector); | |
| clearText = new AES128Test().decrypt(data, secretKey, initVector); | |
| System.out.println("Encrypted String: " + data); | |
| System.out.println(clearText); | |
| } | |
| } |
| <?php | |
| function encrypt_aes128($clear_text, $key, $iv) { | |
| $iv = str_pad($iv, 16, "\0"); | |
| $encrypt_text = openssl_encrypt($clear_text, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv); | |
| $data = base64_encode($encrypt_text); | |
| return $data; | |
| } | |
| function decrypt_aes128($data, $key, $iv) { | |
| $iv = str_pad($iv, 16, "\0"); | |
| $encrypt_text = base64_decode($data); | |
| $clear_text = openssl_decrypt($encrypt_text, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv); | |
| return $clear_text; | |
| } | |
| $clear_text = "BSプレミアム20日放送"; | |
| $key = "SecretKey"; | |
| $iv = "InitVector"; | |
| $data = encrypt_aes128($clear_text, $key, $iv); | |
| echo "Encrypted String: ".$data."\n"; | |
| echo decrypt_aes128($data, $key, $iv)."\n"; | |
| ?> |
Output:
Encrypted String: zM+I3ulxLl4Pna0FgwGKZcQHXCBcO1hzTtKmf2n36vk=
BSプレミアム20日放送
AES-256-CBC
| 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(); | |
| } | |
| } | |
| } |
| #!/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() |
| 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 |
| /* | |
| * 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); | |
| } | |
| } |
| <?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"; | |
| ?> |
Output:
Encrypted String: uatdkMyXtxSIgbvVthf3mYfYbyAOkZDYy+/eGbw7ukA=
BSプレミアム20日放送



























































