c# - Using SharpAESCrypt to encrypt strings -


i decided use sharpaescrypt implementation of aes encryption in c#. according documentation (https://www.aescrypt.com/sharp_aes_crypt.html) should able use static method, providing password string, plain-text input stream , output stream. data out of output stream appears zero's.

i suspect doing wrong converting strings stream , back. can see wrong code below? (it compiles , runs, newbytearray filled 0's @ end)

    private void encryptmemstream()     {         byte[] bytearray = encoding.utf8.getbytes(decryptedtb.text);         using (memorystream plaintext = new memorystream(bytearray))         {             using (memorystream encrypteddata = new memorystream())             {                 //plaintext.write(bytearray, 0, bytearray.length);                 sharpaescrypt.encrypt(passwordtb.text, plaintext, encrypteddata);                 encrypteddata.position = 0;                 byte[] newbytearray = new byte[encrypteddata.length];                 newbytearray = encrypteddata.toarray();                 encryptedtb.text = encoding.utf8.getstring(newbytearray);             }         }     } 

edit: native implementation of code uses filestreams, should work memorystreams too. test filestreams , add results here.

more edits:

so when use file stream call code

    //code aes crypt library     public static void encrypt(string password, string inputfile, string outputfile)     {         using (filestream infs = file.openread(inputfile))         using (filestream outfs = file.create(outputfile))             encrypt(password, infs, outfs);     } 

which calls function have been calling directly

    //code aes crypt library     public static void encrypt(string password, stream input, stream output)     {         int a;         byte[] buffer = new byte[1024 * 4];         sharpaescrypt c = new sharpaescrypt(password, output, operationmode.encrypt);         while ((a = input.read(buffer, 0, buffer.length)) != 0)             c.write(buffer, 0, a);         c.flushfinalblock();     } 

there subtle difference in using memorystream , filestream don't understand. filestream works fine, memorystream returns blank array...

a quick code review reveals few things including what, discussion, seems main problem facing. problematic line

encryptedtb.text = encoding.utf8.getstring(newbytearray); 

this line taking binary data in newbytearray , telling code valid utf8 byte array. chances not going valid utf8. parts of may (such header) encrypted data not going , shouldn't use method string.

if need printable string put in textbox of sort correct way binary data through base64 encoding.

encryptedtb.text = convert.tobase64string(newbytearray); 

base64 encoding takes groups of 3 bytes (24 bits) , converts them groups of 4 characters.

a further note while i'm here in these 2 lines:

byte[] newbytearray = new byte[encrypteddata.length]; newbytearray = encrypteddata.toarray(); 

you declare byte array , allocate new array it. discard in favour of array encrypteddata.toarray(). not put data array allocated new byte[encrypteddata.length] creates new array , puts newbytearray variable. better do:

byte[] newbytearray = encrypteddata.toarray(); 

full working code below:

byte[] bytearray = encoding.utf8.getbytes(sourcetext); byte[] newbytearray; using (memorystream plaintext = new memorystream(bytearray)) {     using (memorystream encrypteddata = new memorystream())     {         sharpaescrypt.encrypt(password, plaintext, encrypteddata);         newbytearray = encrypteddata.toarray();     } } encryptedtb.text = convert.tobase64string(newbytearray); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -