Last active
March 3, 2024 06:57
-
-
Save jiavictor/28c2708d5aefcb32bde511ca895e44d1 to your computer and use it in GitHub Desktop.
Revisions
-
jiavictor revised this gist
Jun 5, 2017 . 1 changed file with 99 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,99 @@ 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(); } } } -
jiavictor revised this gist
May 26, 2017 . No changes.There are no files selected for viewing
-
jiavictor revised this gist
May 26, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,3 @@ 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 -
jiavictor created this gist
May 26, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ #!/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() 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ $ echo -n "BSプレミアム20日放送" | /usr/local/opt/openssl/bin/openssl enc -e -aes-128-cbc -K "5365637265744B657900000000000000" -iv "496E6974566563746F72000000000000" -nosalt -a $ echo "zM+I3ulxLl4Pna0FgwGKZcQHXCBcO1hzTtKmf2n36vk=" | /usr/local/opt/openssl/bin/openssl enc -d -aes-128-cbc -K "5365637265744B657900000000000000" -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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ 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); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ <?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"; ?>