AES decrypted message not matching the original message - c#

I am trying to Encrypt and Decrypt a string using AES256. But The decrypted string is not matching the original one. I am not sure, but maybe I am getting the Encoding part wrong.
I am using CSPRNG to generate the IV and PBDKF2 for generating a Key to be used for AES Encryption
Program.cs:
using System;
using System.Text;
namespace AESEncryptionUtility
{
class Program
{
private static string _pass = "MasterPass";
private static string _msg = "Mohit";
private static byte[] key = EncryptionUtility.GenerateKey(_pass, 32);
private static byte[] IV = EncryptionUtility.GenerateSalt(16);
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
byte[] encrypted = Encrypt(_msg);
byte[] decrypted = Decrypt(Encoding.ASCII.GetString(encrypted));
}
public static byte[] Encrypt(string msg)
{
byte[] asciiBytesOriginal = Encoding.ASCII.GetBytes(_msg);
byte[] encrypted = EncryptionUtility.Encrypt(asciiBytesOriginal, key, IV);
Console.WriteLine("encrypted started");
foreach(var b in encrypted)
{
Console.Write(b + " ");
}
Console.WriteLine("\nencrypted ended");
return encrypted;
}
public static byte[] Decrypt(string cipher)
{
byte[] asciiBytes = Encoding.ASCII.GetBytes(cipher);
byte[] originalBytes = EncryptionUtility.Decrypt(asciiBytes, key, IV);
Console.WriteLine("decrypted started");
foreach(var b in originalBytes)
{
Console.Write(b + " ");
}
Console.WriteLine("\ndecrypted ended");
string original = Encoding.ASCII.GetString(originalBytes);
Console.WriteLine("original string: " + original);
return originalBytes;
}
}
}
EncryptionUtility.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace AESEncryptionUtility
{
public static class EncryptionUtility
{
public static byte[] Encrypt(byte[] plainBytes, byte[] key, byte[] IV)
{
byte[] encrypted = null;
using (AesCryptoServiceProvider aesAlgo = new AesCryptoServiceProvider())
{
aesAlgo.Key = key;
aesAlgo.BlockSize = 128;
aesAlgo.Mode = CipherMode.CBC;
//aesAlgo.Padding = PaddingMode.PKCS7;
aesAlgo.Padding = PaddingMode.Zeros;
aesAlgo.IV = IV;
ICryptoTransform encryptor = aesAlgo.CreateEncryptor();
encrypted = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
}
return encrypted;
}
public static byte[] Decrypt(byte[] cipherBytes, byte[] key, byte[] IV)
{
byte[] decrypted = null;
using (AesCryptoServiceProvider aesAlgo = new AesCryptoServiceProvider())
{
aesAlgo.Key = key;
aesAlgo.BlockSize = 128;
aesAlgo.Mode = CipherMode.CBC;
//aesAlgo.Padding = PaddingMode.PKCS7;
aesAlgo.Padding = PaddingMode.Zeros;
aesAlgo.IV = IV;
ICryptoTransform decryptor = aesAlgo.CreateDecryptor();
decrypted = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
}
return decrypted;
}
public static byte[] GenerateKey(string masterPassword, int size) //size in bytes
{
byte[] salt = GenerateSalt(size);
Rfc2898DeriveBytes pbfdk = new Rfc2898DeriveBytes(masterPassword, salt, 20000);
return pbfdk.GetBytes(size);
}
public static byte[] GenerateSalt(int size) //size in bytes
{
RNGCryptoServiceProvider generator = new RNGCryptoServiceProvider();
byte[] salt = new byte[size];
generator.GetNonZeroBytes(salt);
return salt;
}
}
}

You can't convert arbitrary binary data to a string:
byte[] decrypted = Decrypt(Encoding.ASCII.GetString(encrypted));
And expect it to, just by chance, make sense as the particular character encoding you have chosen. It doesn't work that way. Encryption algorithms operate on bytes, not strings. If you change the following, your code will work:
...
public static byte[] Decrypt(byte[] cipher)
{
byte[] asciiBytes = cipher;
...

Have you tried changing the second last line to:
byte [] decrypted = Decrypt ( encrypted);

Related

How to generate Key and Key IV aes in C#

how to generate a key and key IV and not write them explicitly?
public sealed class MyCryptoClass
{
protected RijndaelManaged myRijndael;
private static string encryptionKey = "142eb4a7ab52dbfb971e18daed7056488446b4b2167cf61187f4bbc60fc9d96d";
private static string initialisationVector ="26744a68b53dd87bb395584c00f7290a";
I try generate encryptionKey and initialisationVector
protected static readonly MyCryptoClass _instance = new MyCryptoClass();
public static MyCryptoClass Instance
{
get { return _instance; }
}
public string EncryptText(string plainText)
{
using (myRijndael = new RijndaelManaged())
{
myRijndael.Key = HexStringToByte(encryptionKey);
myRijndael.IV = HexStringToByte(initialisationVector);
myRijndael.Mode = CipherMode.CBC;
myRijndael.Padding = PaddingMode.PKCS7;
byte[] encrypted = EncryptStringToBytes(plainText, myRijndael.Key, myRijndael.IV);
string encString = Convert.ToBase64String(encrypted);
return encString;
}
}
Let's do it step by step to keep things simple.
You need two methods to achieve your goal. I'll start with encryption method:
static byte[] Encrypt(string input, byte[] Key, byte[] IV)
{
byte[] encryptedBytes;
using (RijndaelManaged rijndael = new RijndaelManaged())
{
rijndael.Key = Key;
rijndael.IV = IV;
ICryptoTransform encryptor = rijndael.CreateEncryptor(rijndael.Key, rijndael.IV);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream,
encryptor, CryptoStreamMode.Write))
{
using (var streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(input);
}
encryptedBytes = memoryStream.ToArray();
}
}
}
return encryptedBytes;
}
Then we need a Decrypt method subsequently:
static string Decrypt(byte[] cipher, byte[] Key, byte[] IV)
{
string plaintext = null;
using (RijndaelManaged rijndael = new RijndaelManaged())
{
rijndael.Key = Key;
rijndael.IV = IV;
ICryptoTransform decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV);
using (var memoryStream = new MemoryStream(cipher))
{
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
using (var streamReader = new StreamReader(cryptoStream))
{
plaintext = streamReader.ReadToEnd();
}
}
}
}
return plaintext;
}
Note: It's better to wrap Encrypt and Decrypt methods in a class and then use them.
You can call the methods like below:
string original = "This is what would be encrypted!";
using (RijndaelManaged myRijndael = new RijndaelManaged())
{
myRijndael.GenerateKey(); // this line generates key
myRijndael.GenerateIV(); // this line generates initialization vektor
// This line returns encrypted text
byte[] encryptedBytes = Encrypt(original, myRijndael.Key, myRijndael.IV);
// You can decrypt the encrypted text like so
string decryptedString = Decrypt(encryptedBytes, myRijndael.Key, myRijndael.IV);
}

Best Practice For Exception Handling When Writing a Utility Class c#

In my solution, I am making a Utility class for Encryption. It exposes methods for encryption, decryption and few other things. Any other project can use this utility wherever encryption is needed. All the methods are static.
I am not handling any Exception in the Utility. Instead I am leaving it to the caller to handle Exceptions.
What is the Best Practice for Exception Handling when designing small utilities like this.
CODE:
public class AESEncryptionUtility
{
public static byte[] Encrypt(byte[] plainBytes, byte[] key, byte[] IV, CipherMode cipherMode, PaddingMode padding)
{
byte[] encrypted = null;
using (AesCryptoServiceProvider aesAlgo = GetAesCryptoServiceProvider(key, IV, cipherMode, padding))
{
ICryptoTransform encryptor = aesAlgo.CreateEncryptor();
encrypted = encryptor.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
}
return encrypted;
}
public static string EncryptUrlSafe(byte[] plainText, byte[] key, byte[] IV, CipherMode cipherMode, PaddingMode padding) // This will return safe Base64 Url Encoded string
{
byte[] encrypted = Encrypt(plainText, key, IV, cipherMode, padding);
return Convert.ToBase64String(encrypted).Replace('+', '-').Replace('/', '_').Replace('=', ',');
}
private static AesCryptoServiceProvider GetAesCryptoServiceProvider(byte[] key, byte[] IV, CipherMode cipherMode, PaddingMode padding)
{
AesCryptoServiceProvider aesAlgo = new AesCryptoServiceProvider();
aesAlgo.Key = key;
aesAlgo.IV = IV;
aesAlgo.Mode = cipherMode;
aesAlgo.Padding = padding;
return aesAlgo;
}
public static byte[] Decrypt(byte[] cipherBytes, byte[] key, byte[] IV, CipherMode cipherMode, PaddingMode padding)
{
byte[] decrypted = null;
using (AesCryptoServiceProvider aesAlgo = GetAesCryptoServiceProvider(key, IV, cipherMode, padding))
{
ICryptoTransform decryptor = aesAlgo.CreateDecryptor();
decrypted = decryptor.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
}
return decrypted;
}
public static byte[] GenerateKey(string masterPassword, int size) //size in bytes
{
byte[] salt = GenerateSalt(size);
Rfc2898DeriveBytes pbfdk = new Rfc2898DeriveBytes(masterPassword, salt, 20000);
return pbfdk.GetBytes(size);
}
public static byte[] GenerateSalt(int size) //size in bytes
{
RNGCryptoServiceProvider generator = new RNGCryptoServiceProvider();
byte[] salt = new byte[size];
generator.GetNonZeroBytes(salt);
return salt;
}
}

Only Able to Decrypt Encrypted Data First Time

I have the following code to Encrypt/Decrypt PII data.
public static class Encryption
{
public static readonly int KeyLengthBits = 256; //AES Key Length in bits
public static readonly int SaltLength = 8; //Salt length in bytes
private static readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
private const int PBKDF2IterCount = 1000; // default for Rfc2898DeriveBytes
private const int PBKDF2SubkeyLength = 256 / 8; // 256 bits
private const int SaltSize = 128 / 8; // 128 bits
public static string DecryptString(string ciphertext, string passphrase)
{
var inputs = ciphertext.Split(":".ToCharArray(), 3);
var iv = Convert.FromBase64String(inputs[0]); // Extract the IV
var salt = Convert.FromBase64String(inputs[1]); // Extract the salt
var ciphertextBytes = Convert.FromBase64String(inputs[2]); // Extract the ciphertext
// Derive the key from the supplied passphrase and extracted salt
byte[] key = DeriveKeyFromPassphrase(passphrase, salt);
// Decrypt
byte[] plaintext = DoCryptoOperation(ciphertextBytes, key, iv, false);
// Return the decrypted string
return Encoding.UTF8.GetString(plaintext);
}
public static string EncryptString(string plaintext, string passphrase)
{
var salt = GenerateRandomBytes(SaltLength); // Random salt
var iv = GenerateRandomBytes(16); // AES is always a 128-bit block size
var key = DeriveKeyFromPassphrase(passphrase, salt); // Derive the key from the passphrase
// Encrypt
var ciphertext = DoCryptoOperation(Encoding.UTF8.GetBytes(plaintext), key, iv, true);
// Return the formatted string
return String.Format("{0}:{1}:{2}", Convert.ToBase64String(iv), Convert.ToBase64String(salt), Convert.ToBase64String(ciphertext));
}
private static byte[] DeriveKeyFromPassphrase(string passphrase, byte[] salt, int iterationCount = 2000)
{
var keyDerivationFunction = new Rfc2898DeriveBytes(passphrase, salt, iterationCount); //PBKDF2
return keyDerivationFunction.GetBytes(KeyLengthBits / 8);
}
private static byte[] GenerateRandomBytes(int lengthBytes)
{
var bytes = new byte[lengthBytes];
rng.GetBytes(bytes);
return bytes;
}
// This function does both encryption and decryption, depending on the value of the "encrypt" parameter
private static byte[] DoCryptoOperation(byte[] inputData, byte[] key, byte[] iv, bool encrypt)
{
byte[] output;
using (var aes = new AesCryptoServiceProvider())
using (var ms = new MemoryStream())
{
var cryptoTransform = encrypt ? aes.CreateEncryptor(key, iv) : aes.CreateDecryptor(key, iv);
using (var cs = new CryptoStream(ms, cryptoTransform, CryptoStreamMode.Write))
cs.Write(inputData, 0, inputData.Length);
output = ms.ToArray();
}
return output;
}
}
If I publish this code and encrypt data and then decrypt it, there's no problem. I correctly receive the unencrypted data. If I rebuild my application without making any changes, then publish it again without re-encrypting the data, (it's already encrypted at this point), I'm unable to decrypt it. It just returns the encrypted strings.
Why would re-publishing to the same server to this? Should I be using a different approach? If so, please recommend that different approach.
Thanks

Rijndael Padding Error

Hello I am trying to encrypt / decrypt a string via Rijaendal.
I simply can't figure out why the decryption blows up. I always end up with an incorrect padding error. One thing that throws me off is the result of my encryption which I return as HEX array. It has a length of 14 bytes. In my decryption function, the same byte array ends up having 16 bytes upon conversion from HEX.
Any help would be appreciated:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace rjandal
{
class Program
{
static void Main(string[] args)
{
string DataForEncrypting = "this is a test";
string key = string.Empty;
string iv = string.Empty;
using (System.Security.Cryptography.RijndaelManaged rmt = new System.Security.Cryptography.RijndaelManaged())
{
rmt.KeySize = 256;
rmt.BlockSize = 128;
rmt.Mode = System.Security.Cryptography.CipherMode.CBC;
rmt.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
rmt.GenerateKey();
rmt.GenerateIV();
key = Convert.ToBase64String(rmt.Key);
iv = Convert.ToBase64String(rmt.IV);
}
string encryptedData = _encrypt(DataForEncrypting, key, iv);
string unencryptedData = _decrypt(key, iv, HexString2Ascii(encryptedData));
Console.WriteLine(unencryptedData);
Console.WriteLine(encryptedData);
Console.ReadKey();
}
private static string _encrypt(string value, string key, string initVector)
{
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(value);
byte[] encBuffer;
using (System.Security.Cryptography.RijndaelManaged rmt = new System.Security.Cryptography.RijndaelManaged())
{
rmt.KeySize = 256;
rmt.BlockSize = 128;
rmt.Mode = System.Security.Cryptography.CipherMode.CBC;
rmt.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
encBuffer = rmt.CreateEncryptor(Convert.FromBase64String(key),
Convert.FromBase64String(initVector)).TransformFinalBlock(buffer, 0, buffer.Length);
}
string encryptValue = ConvertToHex(ASCIIEncoding.ASCII.GetString(encBuffer));
return encryptValue;
}
private static string _decrypt(string key, string initVector, string value)
{
byte[] hexBuffer = ASCIIEncoding.ASCII.GetBytes(value);
byte[] decBuffer;
using (System.Security.Cryptography.RijndaelManaged rmt = new System.Security.Cryptography.RijndaelManaged())
{
rmt.KeySize = 256;
rmt.BlockSize = 128;
rmt.Mode = System.Security.Cryptography.CipherMode.CBC;
rmt.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
decBuffer = rmt.CreateDecryptor(Convert.FromBase64String(key),
Convert.FromBase64String(initVector)).TransformFinalBlock(hexBuffer, 0, hexBuffer.Length);
}
return System.Text.ASCIIEncoding.ASCII.GetString(decBuffer);
}
private static string ConvertToHex(string asciiString)
{
string hex = "";
foreach (char c in asciiString)
{
int tmp = c;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
}
return hex;
}
private static string HexString2Ascii(string hexString)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= hexString.Length - 2; i += 2)
{
sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))));
}
return sb.ToString();
}
}
}
You're doing way too much conversion between text and data, basically. Look at this, for example:
string encryptValue = ConvertToHex(ASCIIEncoding.ASCII.GetString(encBuffer));
Once you've got an ASCII string, why would you need to convert that into hex? It's already text! But by then you'll already have lost the data. Unless you really need it in hex (in which case follow Adam's suggestion and change your HexToAscii method to take a byte[] instead of a string) you should just use Convert.ToBase64String:
string encryptValue = Convert.ToBase64String(encBuffer);
Use Convert.FromBase64String at the other end when decrypting. You can then get rid of your hex methods completely.
Oh, and in general I wouldn't use Encoding.ASCII to start with... I'd almost always use Encoding.UTF8 instead. Currently you'll fail to encrypt (correctly) any strings containing non-ASCII characters such as accents.
Here's a rejigged version of your test program, with a few of those changes made. Note that the names "cipher text" and "plain text" are in terms of encryption... they're still binary data rather than text!
using System;
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main(string[] args)
{
string DataForEncrypting = "this is a test";
string key = string.Empty;
string iv = string.Empty;
using (RijndaelManaged rmt = new RijndaelManaged())
{
rmt.KeySize = 256;
rmt.BlockSize = 128;
rmt.Mode = CipherMode.CBC;
rmt.Padding = PaddingMode.ISO10126;
rmt.GenerateKey();
rmt.GenerateIV();
key = Convert.ToBase64String(rmt.Key);
iv = Convert.ToBase64String(rmt.IV);
}
string encryptedData = _encrypt(DataForEncrypting, key, iv);
string unencryptedData = _decrypt(key, iv, encryptedData);
Console.WriteLine(unencryptedData);
Console.WriteLine(encryptedData);
Console.ReadKey();
}
private static string _encrypt(string value, string key, string initVector)
{
using (RijndaelManaged rmt = new RijndaelManaged())
{
rmt.KeySize = 256;
rmt.BlockSize = 128;
rmt.Mode = CipherMode.CBC;
rmt.Padding = PaddingMode.ISO10126;
byte[] plainText = Encoding.UTF8.GetBytes(value);
byte[] cipherText = rmt.CreateEncryptor(Convert.FromBase64String(key),
Convert.FromBase64String(initVector))
.TransformFinalBlock(plainText, 0, plainText.Length);
return Convert.ToBase64String(cipherText);
}
}
private static string _decrypt(string key, string initVector, string value)
{
using (RijndaelManaged rmt = new RijndaelManaged())
{
rmt.KeySize = 256;
rmt.BlockSize = 128;
rmt.Mode = CipherMode.CBC;
rmt.Padding = PaddingMode.ISO10126;
byte[] cipherText = Convert.FromBase64String(value);
byte[] plainText = rmt.CreateDecryptor(Convert.FromBase64String(key),
Convert.FromBase64String(initVector))
.TransformFinalBlock(cipherText, 0, cipherText.Length);
return Encoding.UTF8.GetString(plainText);
}
}
}
You shouldn't be using ASCII character encoding as an intermediate step; you should change your functions that go from hex to ASCII (and back again) to go from a byte[] to hex (and back again) instead.
private static string ConvertToHex(byte[] data)
{
string hex = "";
foreach (byte b in data)
{
hex += b.ToString("X2");
}
return hex;
}
private static byte[] HexString2ByteArray(string hexString)
{
byte[] output = new byte[hexString.Length / 2];
for (int i = 0; i <= hexString.Length - 2; i += 2)
{
output[i/2] = Convert.ToByte(hexString.Substring(i, 2), 16);
}
return output;
}
As a side note, is there a reason that you're looking for a hex representation of the array versus something more compact like Base64? You're using Base64 in your example to transfer the key and IV, so I'm just curious about what makes you want to return the encrypted data as hex here.
In any case, here's something that should work for you:
private static string _encrypt(string value, string key, string initVector)
{
byte[] buffer = Encoding.Unicode.GetBytes(value);
byte[] encBuffer;
using (System.Security.Cryptography.RijndaelManaged rmt = new System.Security.Cryptography.RijndaelManaged())
{
rmt.KeySize = 256;
rmt.BlockSize = 128;
rmt.Mode = System.Security.Cryptography.CipherMode.CBC;
rmt.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
encBuffer = rmt.CreateEncryptor(Convert.FromBase64String(key),
Convert.FromBase64String(initVector)).TransformFinalBlock(buffer, 0, buffer.Length);
}
string encryptValue = ConvertToHex(encBuffer);
return encryptValue;
}
private static string _decrypt(string key, string initVector, string value)
{
byte[] hexBuffer = HexString2ByteArray(value);
byte[] decBuffer;
using (System.Security.Cryptography.RijndaelManaged rmt = new System.Security.Cryptography.RijndaelManaged())
{
rmt.KeySize = 256;
rmt.BlockSize = 128;
rmt.Mode = System.Security.Cryptography.CipherMode.CBC;
rmt.Padding = System.Security.Cryptography.PaddingMode.ISO10126;
decBuffer = rmt.CreateDecryptor(Convert.FromBase64String(key),
Convert.FromBase64String(initVector)).TransformFinalBlock(hexBuffer, 0, hexBuffer.Length);
}
return Encoding.Unicode.GetString(decBuffer);
}
You may avoid the issues with Decypting/Encrypting and usign System.Text.Encoding and avoid using Base64 encoding work around, by adding a few methods that completely bypass microsoft's mismatched conversions in the System.Text.Encoding, by allowing you to encrypt the real bytes in memory without any translations.
Since using these I have avoided padding errors caused by System.Text.Encoding methods, without using the Base64 conversions either.
private static Byte[] GetBytes(String SomeString)
{
Char[] SomeChars = SomeString.ToCharArray();
Int32 Size = SomeChars.Length * 2;
List<Byte> TempList = new List<Byte>(Size);
foreach (Char Character in SomeChars)
{
TempList.AddRange(BitConverter.GetBytes(Character));
}
return TempList.ToArray();
}
private static String GetString(Byte[] ByteArray)
{
Int32 Size = ByteArray.Length / 2;
List<Char> TempList = new List<Char>(Size);
for (Int32 i = 0; i < ByteArray.Length; i += 2)
{
TempList.Add(BitConverter.ToChar(ByteArray, i));
}
return new String(TempList.ToArray());
}
And how they are used with encryption
private static String Encrypt(String Test1, Byte[] Key, Byte[] IV)
{
Byte[] Encrypted;
using (AesCryptoServiceProvider AesMan = new AesCryptoServiceProvider())
{
AesMan.Mode = CipherMode.CBC;
AesMan.Padding = PaddingMode.ISO10126;
ICryptoTransform EncThis = AesMan.CreateEncryptor(Key, IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, EncThis, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(Test1);
}
Encrypted = msEncrypt.ToArray();
}
}
};
return GetString(Encrypted);
}
private static String Decrypt(String Data, Byte[] Key, Byte[] IV)
{
String Decrypted;
using (AesCryptoServiceProvider AesMan = new AesCryptoServiceProvider())
{
AesMan.Mode = CipherMode.CBC;
AesMan.Padding = PaddingMode.ISO10126;
ICryptoTransform EncThis = AesMan.CreateDecryptor(Key, IV);
using (MemoryStream msDecrypt = new MemoryStream(GetBytes(Data)))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, EncThis, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
Decrypted = srDecrypt.ReadToEnd();
}
}
}
}
return Decrypted;
}

Fast Lightweight .NET Client Encryption -> Server Decryption

I have a simple client/server setup. The client and the server both have a private key.
What does .NET offer me in the way of
ClientData-> ClientEncrypt with KEY-> Transmit to Server-> ServerDecrypt with KEY-> ClientData
Can anyone suggest any fast simple libraries to read up on?
Thanks
RijndaelManaged:
Here's an example:
private static string CreateEncryptedString(string myString, string hexiv, string key)
{
RijndaelManaged alg = new RijndaelManaged();
alg.Padding = PaddingMode.Zeros;
alg.Mode = CipherMode.CBC;
alg.BlockSize = 16 * 8;
alg.Key = ASCIIEncoding.UTF8.GetBytes(key);
alg.IV = StringToByteArray(hexiv);
ICryptoTransform encryptor = alg.CreateEncryptor(alg.Key, alg.IV);
MemoryStream msStream = new MemoryStream();
CryptoStream mCSWriter = new CryptoStream(msStream, encryptor, CryptoStreamMode.Write);
StreamWriter mSWriter = new StreamWriter(mCSWriter);
mSWriter.Write(myString);
mSWriter.Flush();
mCSWriter.FlushFinalBlock();
var EncryptedByte = new byte[msStream.Length];
msStream.Position = 0;
msStream.Read(EncryptedByte, 0, (int)msStream.Length);
return ByteArrayToHexString(EncryptedByte);
}
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
public static string ByteArrayToHexString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
You can easily come out with a decryption algorithm and the examples ( or just google it around!)
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace ServiceConsole
{
public class Obfuscation
{
public static byte[] Encrypt(string data)
{
return Encrypt(data, SecurityCredentials.KeyString, SecurityCredentials.IvString);
}
public static byte[] Encrypt(string data, string key, string iv)
{
return Encrypt(data, key, iv, SecurityCredentials.PaddingString);
}
public static byte[] Encrypt(string data, string key, string iv, char paddingCharacter)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.PadLeft(32, paddingCharacter).Substring(0, 32));
byte[] ivBytes = Encoding.UTF8.GetBytes(iv.PadLeft(32, paddingCharacter).Substring(0, 32));
RijndaelManaged rijndaelManaged = new RijndaelManaged();
rijndaelManaged.BlockSize = 256;
rijndaelManaged.KeySize = 256;
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform iCryptoTransform = rijndaelManaged.CreateEncryptor(keyBytes, ivBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, iCryptoTransform, CryptoStreamMode.Write);
StreamWriter streamWriter = new StreamWriter(cryptoStream);
streamWriter.Write(data);
streamWriter.Flush();
cryptoStream.FlushFinalBlock();
byte[] returnBytes = memoryStream.ToArray();
/// Disposal
streamWriter.Dispose();
cryptoStream.Dispose();
iCryptoTransform.Dispose();
memoryStream.Dispose();
rijndaelManaged.Clear();
///
return returnBytes;
}
public static string Decrypt(byte[] data)
{
return Decrypt(data, SecurityCredentials.KeyString, SecurityCredentials.IvString);
}
public static string Decrypt(byte[] data, string key, string iv)
{
return Decrypt(data, key, iv, SecurityCredentials.PaddingString);
}
public static string Decrypt(byte[] data, string key, string iv, char paddingCharacter)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.PadLeft(32, paddingCharacter).Substring(0, 32));
byte[] ivBytes = Encoding.UTF8.GetBytes(iv.PadLeft(32, paddingCharacter).Substring(0, 32));
RijndaelManaged rijndaelManaged = new RijndaelManaged();
rijndaelManaged.BlockSize = 256;
rijndaelManaged.KeySize = 256;
MemoryStream memoryStream = new MemoryStream(data);
ICryptoTransform iCryptoTransform = rijndaelManaged.CreateDecryptor(keyBytes, ivBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, iCryptoTransform, CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(cryptoStream);
string returnString = streamReader.ReadLine();
/// Disposal
streamReader.Dispose();
cryptoStream.Dispose();
iCryptoTransform.Dispose();
memoryStream.Dispose();
rijndaelManaged.Clear();
///
return returnString;
}
}
}

Categories