c# - How to Encrypt/Decrypt using AES in WinRT? -


i newbie in cryptography.

edited: seems wrong using right encrypt/decrypt algorithm change question to:

how convert these lines of codes winrt ?

the code based on code @ http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.aspx

i need same in winrt:

update:

there cryptography , certificate sample winrt (windows 8.1) solved problem.

please check: http://code.msdn.microsoft.com/windowsapps/cryptography-and-3305467b

public static byte[] encrypt(string plaintext)     {         if (plaintext == null || plaintext.length <= 0)             throw new argumentnullexception("plaintext");         if (key == null || key.length <= 0)             throw new argumentnullexception("key");         if (iv == null || iv.length <= 0)             throw new argumentnullexception("key");         byte[] encrypted;         using (aesmanaged aesalg = new aesmanaged())         {             aesalg.key = key;             aesalg.iv = iv;              // create decrytor perform stream transform.             icryptotransform encryptor = aesalg.createencryptor(aesalg.key, aesalg.iv);              using (memorystream msencrypt = new memorystream())             {                 using (cryptostream csencrypt = new cryptostream(msencrypt, encryptor, cryptostreammode.write))                 {                     using (streamwriter swencrypt = new streamwriter(csencrypt))                     {                         //write data stream.                         swencrypt.write(plaintext);                     }                     encrypted = msencrypt.toarray();                 }             }         }         return encrypted;     } 

and

public static string decrypt(byte[] ciphertext)     {         if (ciphertext == null || ciphertext.length <= 0)             throw new argumentnullexception("ciphertext");         if (key == null || key.length <= 0)             throw new argumentnullexception("key");         if (iv == null || iv.length <= 0)             throw new argumentnullexception("key");          string plaintext = null;          using (aesmanaged aesalg = new aesmanaged())         {             aesalg.key = key;             aesalg.iv = iv;              // create decrytor perform stream transform.             icryptotransform decryptor = aesalg.createdecryptor(aesalg.key, aesalg.iv);              using (memorystream msdecrypt = new memorystream(ciphertext))             {                 using (cryptostream csdecrypt = new cryptostream(msdecrypt, decryptor, cryptostreammode.read))                 {                     using (streamreader srdecrypt = new streamreader(csdecrypt))                     {                          // read decrypted bytes decrypting stream // , place them in string.                         plaintext = srdecrypt.readtoend();                     }                 }             }          }          return plaintext;      } 

yes possible, should use same:

and of course correct cipher (aes instead of rijndael),symmetric key size , value , iv. make sure check each of io these functions separately. not rely on defaults, set each value explicitly.

note using ecb mode insecure. mixing cbc , ecb mode, won't work. secure communications should use authenticated encryption or mac (using second key).


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

c++ - End of file on pipe magic during open -