Aes Encryption For Custom(User defined type) class C# -


can use aes encryption technique encrypt/decrypt user defined type. in this link encryption done simple string. suppose want encrypt below user defined type.

public class person {     public string firstname { get; set; }     public string lastname { get; set; } } 

how can done.

or there other alternative way it.

p.s - have tried following after jon's answer while getting exception "length of data decrypt invalid" while decrypting data. using following code encrypt , decrypt data.

[serializable] public class person:iserializable  {     public string firstname { get; set; }     public string lastname { get; set; }      public void getobjectdata(serializationinfo info, streamingcontext context)     {         info.addvalue("firstname", firstname);         info.addvalue("lastname", lastname);     }      public person()     {      }      public person(serializationinfo info, streamingcontext ctxt)     {         firstname = (string)info.getvalue("firstname", typeof(string));         lastname = (string)info.getvalue("lastname", typeof(string));     } }     static void main(string[] args)         {             person per = new person() { firstname = "vikram", lastname = "chaudhary" };                                using (aesmanaged aes = new aesmanaged())             {                 byte[] encrypted = encryptstringtobytes_aes(per, aes.key, aes.iv);                  byte[] decrypted = decryptstringfrombytes_aes(encrypted, aes.key, aes.iv);             }         }    static byte[] encryptstringtobytes_aes(person plaintext, byte[] key, byte[] iv)     {         memorystream stream = new memorystream();         binaryformatter bformatter = new binaryformatter();         bformatter.serialize(stream, plaintext);          byte[] encrypted;         // create aesmanaged object          // specified key , iv.          using (aesmanaged aesalg = new aesmanaged())         {             aesalg.key = key;             aesalg.iv = iv;              // create decrytor perform stream transform.             icryptotransform encryptor = aesalg.createencryptor(aesalg.key, aesalg.iv);              // create streams used encryption.              using (memorystream msencrypt = new memorystream())             {                 using (cryptostream csencrypt = new cryptostream(msencrypt, encryptor, cryptostreammode.write))                 {                                             csencrypt.write(stream.toarray(), 0, stream.toarray().length);                     csencrypt.close();                 }             }         }         // return encrypted bytes memory stream.          byte[] encrypteddata = stream.toarray();         return encrypteddata;      }      static byte[] decryptstringfrombytes_aes(byte[] ciphertext, byte[] key, byte[] iv)     {         byte[] decrypteddata;                      // declare string used hold          // decrypted text.          string plaintext = null;         person decryptedobject = null;          // create aesmanaged object          // specified key , iv.          using (aesmanaged aesalg = new aesmanaged())         {             aesalg.key = key;             aesalg.iv = iv;              // create decrytor perform stream transform.             icryptotransform decryptor = aesalg.createdecryptor(aesalg.key, aesalg.iv);              // create streams used decryption.              using (memorystream msdecrypt = new memorystream())             {                 using (cryptostream csdecrypt = new cryptostream(msdecrypt, decryptor, cryptostreammode.write))                 {                                         csdecrypt.write(ciphertext, 0, ciphertext.length);                                      }                   decrypteddata = msdecrypt.toarray();             }         }         return decrypteddata;     } 

please let me know doing wrong.

you're combining 2 separable concepts:

  • how convert between instance of person , stream of binary data (or byte[])?
  • how encrypt/decrypt arbitrary stream of binary data (or byte[])?

those 2 concepts should separated far possibly can in code , in mind.

  • work out how want serialize object - native .net binary serialization? text format (json, xml, else)? 3rd party binary serialization format (e.g. protocol buffers, thrift)? can build , test without encryption being involved.

  • work out how want encrypt "some data". can build , test without serialization being involved, using hard-coded data.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -