php - How to encrypt in C# that is compatible with Laravel's Encryption? -


i know how encrypt in c#, laravel (php) can decrypt it's encryption?

this c# encryption:

    private static readonly encoding encoding = encoding.utf8;      public static void main(string[] args)     {         string key = "yswzkxsnb1as38qzj5cza01wd3wt1234";         string text = "here data encrypt!";          string encrypted = encrypt(text, key);          // display original data , encrypted data.         console.writeline("original: {0}", text);         console.writeline("key: {0}", key);         console.writeline("encrypted: {0}", encrypted);     }      private static string encrypt(string plaintext, string key)     {         rijndaelmanaged aes = new rijndaelmanaged();         aes.keysize = 256;         aes.blocksize = 128;         aes.padding = paddingmode.pkcs7;         aes.mode = ciphermode.cbc;          aes.key = encoding.getbytes(key);         aes.generateiv();          icryptotransform aesencrypt = aes.createencryptor(aes.key, aes.iv);         byte[] buffer = encoding.ascii.getbytes(phpserialize(plaintext));          string encryptedtext = convert.tobase64string(encoding.default.getbytes(encoding.default.getstring(aesencrypt.transformfinalblock(buffer, 0, buffer.length))));           string mac = "";          mac = bitconverter.tostring(hmacsha256(convert.tobase64string(aes.iv) + encryptedtext, key)).replace("-", "").tolower();          var keyvalues = new dictionary<string, object>         {             { "iv", convert.tobase64string(aes.iv) },             { "value", encryptedtext },             { "mac", mac },         };          javascriptserializer serializer = new javascriptserializer();         return convert.tobase64string(encoding.ascii.getbytes(serializer.serialize(keyvalues)));     } 

the code encrypts, bu laravel returns "could not decrypt data." when trying decrypt code output here.

here's gist code wrote solved problem:

using system; using system.text; using system.security.cryptography; using system.web.script.serialization; using system.collections.generic;  namespace aes256cbcencrypterapp {     class mainclass     {         public static void main(string[] args)         {             console.writeline("hello, world!");              // sample encryption key. must 32 characters.             string key = "8uhjpgxzzxcgkhxv2qcnooyjexuzvjro";              // sample text encrypt , decrypt.             string text = "here text encrypt!";              // encrypt , decrypt sample text via aes256cbcencrypter class.             string encrypted = aes256cbcencrypter.encrypt(text, key);             string decrypted = aes256cbcencrypter.decrypt(encrypted, key);              // show encrypted , decrypted data , key used.             console.writeline("original: {0}", text);             console.writeline("key: {0}", key);             console.writeline("encrypted: {0}", encrypted);             console.writeline("decrypted: {0}", decrypted);         }     }      /**      * class encrypt , decrypt strings using cipher aes-256-cbc used in laravel.      */     class aes256cbcencrypter     {         private static readonly encoding encoding = encoding.utf8;          public static string encrypt(string plaintext, string key)         {             try             {                 rijndaelmanaged aes = new rijndaelmanaged();                 aes.keysize = 256;                 aes.blocksize = 128;                 aes.padding = paddingmode.pkcs7;                 aes.mode = ciphermode.cbc;                  aes.key = encoding.getbytes(key);                 aes.generateiv();                  icryptotransform aesencrypt = aes.createencryptor(aes.key, aes.iv);                 byte[] buffer = encoding.getbytes(plaintext);                  string encryptedtext = convert.tobase64string(aesencrypt.transformfinalblock(buffer, 0, buffer.length));                  string mac = "";                  mac = bitconverter.tostring(hmacsha256(convert.tobase64string(aes.iv) + encryptedtext, key)).replace("-", "").tolower();                  var keyvalues = new dictionary<string, object>                 {                     { "iv", convert.tobase64string(aes.iv) },                     { "value", encryptedtext },                     { "mac", mac },                 };                  javascriptserializer serializer = new javascriptserializer();                  return convert.tobase64string(encoding.getbytes(serializer.serialize(keyvalues)));             }             catch (exception e)             {                 throw new exception("error encrypting: " + e.message);             }         }          public static string decrypt(string plaintext, string key)         {             try             {                 rijndaelmanaged aes = new rijndaelmanaged();                 aes.keysize = 256;                 aes.blocksize = 128;                 aes.padding = paddingmode.pkcs7;                 aes.mode = ciphermode.cbc;                 aes.key = encoding.getbytes(key);                  // base 64 decode                 byte[] base64decoded = convert.frombase64string(plaintext);                 string base64decodedstr = encoding.getstring(base64decoded);                  // json decode base64str                 javascriptserializer serializer = new javascriptserializer();                 var payload = serializer.deserialize<dictionary<string, string>>(base64decodedstr);                  aes.iv = convert.frombase64string(payload["iv"]);                  icryptotransform aesdecrypt = aes.createdecryptor(aes.key, aes.iv);                 byte[] buffer = convert.frombase64string(payload["value"]);                  return encoding.getstring(aesdecrypt.transformfinalblock(buffer, 0, buffer.length));             }             catch (exception e)             {                 throw new exception("error decrypting: " + e.message);             }         }          static byte[] hmacsha256(string data, string key)         {             using (hmacsha256 hmac = new hmacsha256(encoding.getbytes(key)))             {                 return hmac.computehash(encoding.getbytes(data));             }         }     } } 

the program encrypt given text using aes-256-cbc:

hello, world! original: here text encrypt! key: 8uhjpgxzzxcgkhxv2qcnooyjexuzvjro encrypted: eyjpdii6iknyvzrszgprt05yemi0umhzk0x4rfe9psisinzhbhvlijoidgziehpiv2htbvvjkzhnztd6adk2wlvibe1jumdsyjbkmzh0vtr5dvhkwt0ilcjtywmioiizmzbjyzcyotg4zjk1yjflywi4zgy2ztuymjllotkxndexnzrjm2q2ymixowi2ndk2y2i1ngezmdbin2e3ymnlin0= decrypted: here text encrypt! 

i hope helps other people may need implement aes-256-cbc encryption in c# compatible laravel.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -