Forked from jeruyyap/Matching Rijndael-128 in C#.NET, PHP, and Python
Created
March 23, 2017 23:35
-
-
Save tsartsaris/3ae59b9c4899227fc326b38baa050ce3 to your computer and use it in GitHub Desktop.
Revisions
-
jeruyyap revised this gist
Jan 19, 2017 . 1 changed file with 5 additions and 4 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,6 +1,7 @@ DO NOT use these as-is for anything important! These are only very basic examples and they are missing much of what would be needed for a real-world use case. These are snippets for matching encrypt and decrypt (Rijndael-128 in CBC mode with PKCS7 padding) in C#.NET, PHP, and Python. I cobbled these together from various existing examples because at the time it seemed like a lot of existing examples out there for different languages/platforms did not quite match and would require quite a bit more work before they would encrypt/decrypt identically. Each of these take Keys and IVs that are 16 character strings encoded in base64. -
jeruyyap revised this gist
Dec 12, 2014 . 2 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes. -
jeruyyap revised this gist
Dec 12, 2014 . 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 @@ -4,7 +4,7 @@ public class Rijndael128Encryptor * Encrypt method * Both Keys and IVs need to be 16 characters encoded in base64. */ public String AES_encrypt(String Input, String AES_Key, String AES_IV) { // Create encryptor var aes = new RijndaelManaged(); @@ -37,7 +37,7 @@ public class Rijndael128Encryptor * Decrypt method * Both Keys and IVs need to be 16 characters encoded in base64. */ public String AES_decrypt(String Input, String AES_Key, String AES_IV) { // Create decryptor RijndaelManaged aes = new RijndaelManaged(); -
jeruyyap revised this gist
Dec 12, 2014 . 4 changed files with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes.File renamed without changes.File renamed without changes.File renamed without changes. -
jeruyyap renamed this gist
Dec 12, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jeruyyap renamed this gist
Dec 12, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jeruyyap created this gist
Dec 12, 2014 .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,69 @@ public class Rijndael128Encryptor { /* * Encrypt method * Both Keys and IVs need to be 16 characters encoded in base64. */ public static String AES_encrypt(String Input, String AES_Key, String AES_IV) { // Create encryptor var aes = new RijndaelManaged(); aes.KeySize = 128; aes.BlockSize = 128; aes.Padding = PaddingMode.PKCS7; aes.Key = Convert.FromBase64String(AES_Key); aes.IV = Convert.FromBase64String(AES_IV); var encrypt = aes.CreateEncryptor(aes.Key, aes.IV); // Encrypt Input byte[] xBuff = null; using (var ms = new MemoryStream()) { // Convert from UTF-8 String to byte array, write to memory stream and encrypt, then convert to byte array using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write)) { byte[] xXml = Encoding.UTF8.GetBytes(Input); cs.Write(xXml, 0, xXml.Length); } xBuff = ms.ToArray(); } // Convert from byte array to base64 string then return String Output = Convert.ToBase64String(xBuff); return Output; } /* * Decrypt method * Both Keys and IVs need to be 16 characters encoded in base64. */ public static String AES_decrypt(String Input, String AES_Key, String AES_IV) { // Create decryptor RijndaelManaged aes = new RijndaelManaged(); aes.KeySize = 128; aes.BlockSize = 128; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.PKCS7; aes.Key = Convert.FromBase64String(AES_Key); aes.IV = Convert.FromBase64String(AES_IV); var decrypt = aes.CreateDecryptor(); // Decrypt Input byte[] xBuff = null; using (var ms = new MemoryStream()) { // Convert from base64 string to byte array, write to memory stream and decrypt, then convert to byte array. using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write)) { byte[] xXml = Convert.FromBase64String(Input); cs.Write(xXml, 0, xXml.Length); } xBuff = ms.ToArray(); } // Convert from byte array to UTF-8 string then return String Output = Encoding.UTF8.GetString(xBuff); return Output; } } 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,61 @@ <?php class Rijndael128Encryptor{ /* * Adds PKCS7 padding */ private function addpadding($inputstring) { $blocksize = 16; $len = strlen($inputstring); $pad = $blocksize - ($len % $blocksize); $inputstring .= str_repeat(chr($pad), $pad); return $inputstring; } /* * Strips PKCS7 padding */ private function strippadding($inputstring) { $slast = ord(substr($inputstring, -1)); $slastc = chr($slast); if(preg_match("/$slastc{".$slast."}/", $inputstring)){ $inputstring = substr($inputstring, 0, strlen($inputstring)-$slast); return $inputstring; } else { return false; } } /* * Encrypt method * Both Keys and IVs need to be 16 characters encoded in base64. */ public function encrypt($inputstring, $inputkey, $inputiv) { $key = base64_decode($inputkey); $iv = base64_decode($inputiv); // Pad text and encrypt $padded_string = $this->addpadding($inputstring); $encrypted_string = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $padded_string, MCRYPT_MODE_CBC, $iv); // Encode to base64 and return return base64_encode($encrypted_string); } /* * Decrypt method * Both Keys and IVs need to be 16 characters encoded in base64. */ public function decrypt($inputstring, $inputkey, $inputiv) { $key = base64_decode($inputkey); $iv = base64_decode($inputiv); // Decode from base64 and decrypt $decoded_string = base64_decode($inputstring); $decrypted_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $decoded_string, MCRYPT_MODE_CBC, $iv); // Unpad text and return return $this->strippadding($decrypted_string); } } ?> 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,58 @@ from Crypto.Cipher import AES import base64 class RijndaelEncryptor(object): """ Encrypts text using Rijndael 128 in CBC mode and using PKCS7 padding """ def __init__(self, k=16): self.k = k #sets block size of 16 for padding, NOT FOR CIPHER def _pkcs7decode(self, text): """ Remove PKCS7 padding from text """ val = text[-1] if val > self.k: raise ValueError('Input is not padded or padding is corrupt') l = len(text) - val return (text[:l]).decode(encoding="UTF-8") def _pkcs7encode(self, text): """ Add PKCS7 padding to text """ l = len(text) val = self.k - (l % self.k) return text + bytearray([val] * val).decode(encoding="UTF-8") def encrypt(self, text, input_key, input_iv): """ Encrypt method Both Keys and IVs need to be 16 characters encoded in base64. """ # Create aes object key = base64.b64decode(input_key) iv = base64.b64decode(input_iv) aes = AES.new(key, AES.MODE_CBC, iv) # Pad text and encrypt pad_text = self._pkcs7encode(text) cipher_text = aes.encrypt(pad_text) # Encode to base64 and return return base64.b64encode(cipher_text) def decrypt(self, text, input_key, input_iv): """ Decrypt method Both Keys and IVs need to be 16 characters encoded in base64. """ # Create aes object key = base64.b64decode(input_key) iv = base64.b64decode(input_iv) aes = AES.new(key, AES.MODE_CBC, iv) # Decode from base64 and decrypt decode_text = base64.b64decode(text) pad_text = aes.decrypt(decode_text) # Unpad text and return return self._pkcs7decode(pad_text) 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,6 @@ These are snippets for matching encrypt and decrypt (Rijndael-128 in CBC mode with PKCS7 padding) in C#.NET, PHP, and Python. I cobbled these together from various existing examples because it seemed like a lot of existing examples out there for different languages/platforms did not quite match and would require a bit of trial and error before they would encrypt/decrypt identically. I hope this helps someone who may run into the same problem. Each of these take Keys and IVs that are 16 character strings encoded in base64 (this is just to help make sure they're all the same).