Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ! So i need to encrypt an AOB(array of bytes), but the thing is that from what read about encryption... is that the output will be bigger than the input. Is it possible the length of the encrypted AOB to be the same with the unencrypted AOB ?

What I have tried:

I couldn't find a solution to this particular problem.
Posted
Updated 12-May-17 6:47am
v2
Comments
F-ES Sitecore 12-May-17 7:10am    
If you need the encrypted data to be the same length as the original data then that requirement has come from bad design and it is your design that needs to change.
xXxRevolutionxXx 12-May-17 7:19am    
Well... since it's not a big project.. and it's only for me... i want to write as little code as possible... and if there is a way to have the same length... then it will save me from some trouble.
F-ES Sitecore 12-May-17 7:24am    
You can use a substitution algorithm (always substitute one letter for another, eg "a" becomes "m", and to decrypt you change all "m"s to "a"s) but it's probably one of the weakest forms of encryption there is.
Wessel Beulink 12-May-17 7:12am    
1-way incryption or 2-way incryption. 1-way is possible. 2-way isn't possible your encryption is always longer(if save encrypted)
xXxRevolutionxXx 12-May-17 7:23am    
It's two way encryption. Is it then possible to calculate exact output length based on the amount of the unencrypted data ?

1 solution

Yes it is. If you think about it, to encrypt an array of bytes what you want to do is flip roughly half the bits of those bytes to make the numbers completely different. You can do this by XORing your array with an equal size array of nicely distributed pseudo random numbers which are reproducible by using some secret as the seed. The same process decrypts them again - this is symmetric encryption.

So as a simple insecure example, consider this:

C#
Random r = new Random(<your secret seed, hashed from a password or like>);
byte[] yourData = .....
byte[] flips = new byte[yourData.Length];
r.NextBytes(flips);

// encrypt/decrypt
for (int i = 0; i < yourData.Length; i++) yourData[i] ^= flips[i];


The Random class is not random enough for encryption, you need to delve into the crypto namespace for proper encryption strength encoding.

And, you might want to insert some redundant bytes on the end to make the encrypted representation a different length to avoid people being able to match encrypted and unencrypted data by their lengths alone.
 
Share this answer
 
Comments
xXxRevolutionxXx 15-May-17 2:42am    
@Rob Philpott i did something like that. My encrypt function asks for a (string)password. Then it gets the bytes for the password, and then it uses the bytes of the password to encrypt the message. The good about my solution is that there isn't a need to have a key with the same length as the data. The key can be whatever you want, and it doesn't change the size of the file. I'll accept your answer because it's almost what i did, and it's the only solution to this problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900