Jan 2, 2020 06:50 AM
(This post was last modified: Jan 2, 2020 11:32 AM by Dhoroty.
Edit Reason: Applied MavenGradle's code simplify
)
This won't be a tutorial, so you should have basic knowledge in C# and about what is AES, and what is symmetric encryption, etc.
If you want to do AES, you probably googled it up, and found the M$ site with that great code example:
https://docs.microsoft.com/en-us/dotnet/...mework-4.8
There are 2 functions, one for encrypt ( EncryptStringToBytes_Aes ) , one for decrypt (DecryptStringFromBytes_Aes )
Both will requires 2 byte arrays, named byte[] Key, byte[] IV.
You can create those with this:
and reuse the key and IV like this:
But what if, you don't want to create or store key + iv in a byte array, nor you want your users to enter or copy it to your app?
Users like passwords, so if you want to use AES for example in an encrypted chat app, where users exchange the 'password' before using the app, and they'll use it for communication, that would be easier, than byte[] exchange in a paper, right?
So here is a function, that will generate KEY and IV for you from a single UTF8 string:
I hope you'll use it in great projects!
Edit: to clarify this is not my original code! But it is free to use and modify in any projects, so posted here to help you when doing projects that maybe needs it.
Edit #2: Applied MavenGradle's code simplify. Thanks for that
If you want to do AES, you probably googled it up, and found the M$ site with that great code example:
https://docs.microsoft.com/en-us/dotnet/...mework-4.8
There are 2 functions, one for encrypt ( EncryptStringToBytes_Aes ) , one for decrypt (DecryptStringFromBytes_Aes )
Both will requires 2 byte arrays, named byte[] Key, byte[] IV.
You can create those with this:
Code
using (Aes myAes = Aes.Create())Code
myAes.Key = Key;
myAes.IV = IV;But what if, you don't want to create or store key + iv in a byte array, nor you want your users to enter or copy it to your app?
Users like passwords, so if you want to use AES for example in an encrypted chat app, where users exchange the 'password' before using the app, and they'll use it for communication, that would be easier, than byte[] exchange in a paper, right?
So here is a function, that will generate KEY and IV for you from a single UTF8 string:
Code
static byte[][] GetHashKeys(string key)
{
if (key == null) throw new ArgumentNullException(nameof(key));
byte[][] result = new byte[2][];
byte[] hashKey = new SHA256CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(key));
byte[] iv = new byte[16];
for (int i = 0; i < 16; i++) iv[i] = hashKey[i];
result[0] = hashKey;
result[1] = iv;
return result;
}I hope you'll use it in great projects!
Edit: to clarify this is not my original code! But it is free to use and modify in any projects, so posted here to help you when doing projects that maybe needs it.
Edit #2: Applied MavenGradle's code simplify. Thanks for that
Get into great crypto pumps with an online bot.



![[Image: vp6iypF.gif]](https://i.imgur.com/vp6iypF.gif)