RijndaelManaged Decryption - How can I delete the exposition / 0 gracefully?

How to remove a registration from a decrypted string? I use the RijndaelManaged provider for encryption and decryption. When I decrypt, there are a few /0/0/0/0/0/0 0/0/0/0/0/0 at the end of the line. My question is: how can I (correctly) remove characters from the result string?

+7
source share
4 answers

Using TrimEnd() as follows:

 theString.TrimEnd("/0"); 
+5
source

Most likely, you are not using the correct fill and lock modes for the RijndaelManaged instance (called the provider in the code below). Since the symmetric encryption providers in .NET are all block ciphers, these parameters affect the operation of the add-on (as well as how secure the output will be).

The settings below will provide you maximum security when using RijndaelManaged:

 // set the padding algorithm provider.Padding = PaddingMode.ISO10126; // default is PKCS7 // set the block chaining mode provider.Mode = CipherMode.CBC; 

If you are not the one who encrypts the data, and you cannot determine what settings are used for the outgoing side, you will find help in some other answers :)

+9
source

You can prefix the length of the string to the beginning of the string before encrypting it, then, after decrypting, use the length to determine where the string ends.

Or you could base64 encode a string before encrypting it, and then decode it afterwards.

Or encrypt it with a binary or XML serializer before encrypting it.

All these methods have the advantage that they allow you to accurately restore the row that was saved. Any method that accepts the current output and guesses which transformation is being applied does not have this property.

+1
source

I do not see that this is due to encryption. IIUC, you have already done the decryption and have a plaintext line that has something at the end that you want to delete. If so, your question is about string manipulation, and it doesn't matter which encryption algorithm you use. But maybe I misunderstood.?

Suggestion (maybe wrong, but you get the idea):

 string pattern = (i % 2 == 0? "/0" : "0/"); var sb = new StringBuilder(s); int i = s.Length - 1; while (sb[i] == pattern[i % 2]) --i; sb.Length = i; s = sb.ToString(); 
0
source

All Articles