To support compatibility with some old software, I had to encrypt data with PKCS 5 padding and ECB mode with AES encryption. Awkward coder has made this a lot easier by creating a binary for windows phone 7.1 from bouncy castle. In this post I have an example of how I used the binary. Well, most of the code is from the original post by awkward coder.
public static string encrypt(string input, string password)
{
#region encrypt
var byteArray = Encoding.UTF8.GetBytes(input);
var key = Encoding.UTF8.GetBytes(password);
var encryptioncipher = CipherUtilities.GetCipher("AES/ECB/PKCS5PADDING");
encryptioncipher.Init(true, new KeyParameter(key));
//string algo = encryptioncipher.AlgorithmName;
int blocksize = encryptioncipher.GetBlockSize();
byte[] bytes = encryptioncipher.ProcessBytes(byteArray);
byte[] final = encryptioncipher.DoFinal();
string resultBytes = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
MemoryStream encryptedstream = new MemoryStream(bytes.Length + final.Length);
encryptedstream.Write(bytes, 0, bytes.Length);
encryptedstream.Write(final, 0, final.Length);
encryptedstream.Flush();
byte[] encryptedData = new byte[encryptedstream.Length];
encryptedstream.Position = 0;
encryptedstream.Read(encryptedData, 0, encryptedData.Length);
//Convert to hex string
string result = HexHelper.GetHextString(encryptedData);
return result;
#endregion
}
public static string decrypt(string encryptedhexstring, string password)
{
#region decrypt
//Covert from hexstring to bytearray
byte[] encryptedData = HexHelper.GetBytes(encryptedhexstring);
var key = Encoding.UTF8.GetBytes(password);
// AES algorthim with ECB cipher & PKCS5 padding...
var cipher = CipherUtilities.GetCipher("AES/ECB/PKCS5PADDING");
// Initialise the cipher...
cipher.Init(false, new KeyParameter(key));
// Decrypt the data and write the 'final' byte stream...
var decryptionbytes = cipher.ProcessBytes(encryptedData);
var decryptedfinal = cipher.DoFinal();
// Write the decrypt bytes & final to memory...
var decryptedstream = new MemoryStream(decryptionbytes.Length);
decryptedstream.Write(decryptionbytes, 0, decryptionbytes.Length);
decryptedstream.Write(decryptedfinal, 0, decryptedfinal.Length);
decryptedstream.Flush();
var decryptedData = new byte[decryptedstream.Length];
decryptedstream.Position = 0;
decryptedstream.Read(decryptedData, 0, (int)decryptedstream.Length);
// Convert the decrypted data to a string value...
var resultdecrypted = Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);
return resultdecrypted;
#endregion
}





