I'm trying to encrypt and decrypt a file using RijndaelManaged().
I've created a key and a vector which is stored in a file key.txt. I've encrypted a file using:
StreamReader re = new StreamReader(sKeyFile);
string s = re.ReadToEnd();
int iKeyEndPoint;
iKeyEndPoint = s.IndexOf("KEY");
string sKey = s.Substring(0, (iKeyEndPoint));
string sIV = s.Remove(0, iKeyEndPoint);
sIV = sIV.Replace("KEY", "");
//convert strings to byte arrays
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] aKey = encoding.GetBytes(sKey);
byte[] aIV = encoding.GetBytes(sIV);
// open file to encrypt
FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
byte[] encryptedData;
byte[] aData = new byte[fsInput.Length];
fsInput.Read(aData, 0, aData.Length);
RijndaelManaged rijndael = new RijndaelManaged();
//rijndael.Padding = PaddingMode.None;
rijndael.Padding = PaddingMode.ANSIX923;
ICryptoTransform encryptor = rijndael.CreateEncryptor(aKey, aIV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(aData, 0, aData.Length);
cryptoStream.FlushFinalBlock();
}
encryptedData = memoryStream.ToArray();
}
//convert from byte array to string
String sData = System.Text.ASCIIEncoding.ASCII.GetString(encryptedData);
FileStream fsEncypt = new FileStream(sEncryptedFile, FileMode.OpenOrCreate, FileAccess.Write);
// Create a StreamWriter using the CryptoStream.
StreamWriter sWriter = new StreamWriter(fsEncypt);
try
{
//write the encrypted string
sWriter.WriteLine(sData);
}
This seems to work fine (at least a get some text in the sEncryptedFile file).
when i try to decrypt it i get a "Length of the data to decrypt is invalid." error. Code is as follows:
RijndaelManaged rijndael = new RijndaelManaged();
rijndael.Padding = PaddingMode.ANSIX923;
//Get the Key and Vector from File
StreamReader re = new StreamReader("C:\\key.txt");
string s = re.ReadToEnd();
int iKeyEndPoint;
iKeyEndPoint = s.IndexOf("KEY");
string sKey = s.Substring(0, (iKeyEndPoint));
string sIV = s.Remove(0, iKeyEndPoint);
sIV = sIV.Replace("KEY", "");
//convert strings to byte arrays
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] aKey = encoding.GetBytes(sKey);
byte[] aIV = encoding.GetBytes(sIV);
ICryptoTransform decryptor = rijndael.CreateDecryptor(aKey, aIV);
FileStream fEncryptStream = new FileStream("C:\\Encrypted.txt", FileMode.Open, FileAccess.Read);
byte[] aData = new byte[fEncryptStream.Length];
fEncryptStream.Read(aData, 0, aData.Length);
string str = System.Text.ASCIIEncoding.ASCII.GetString(aData);
byte[] decrypted;
using (MemoryStream memoryStream = new MemoryStream(aData))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
int length = aData.Length;
decrypted = new byte[length];
cryptoStream.Read(decrypted, 0, length);
}
}
str = System.Text.ASCIIEncoding.ASCII.GetString(decrypted);
I've tried adding rijndael.Padding = none and removing this live all together. If anyone can point out anything that i've done wrong that would help a great deal!
I've created a key and a vector which is stored in a file key.txt. I've encrypted a file using:
StreamReader re = new StreamReader(sKeyFile);
string s = re.ReadToEnd();
int iKeyEndPoint;
iKeyEndPoint = s.IndexOf("KEY");
string sKey = s.Substring(0, (iKeyEndPoint));
string sIV = s.Remove(0, iKeyEndPoint);
sIV = sIV.Replace("KEY", "");
//convert strings to byte arrays
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] aKey = encoding.GetBytes(sKey);
byte[] aIV = encoding.GetBytes(sIV);
// open file to encrypt
FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
byte[] encryptedData;
byte[] aData = new byte[fsInput.Length];
fsInput.Read(aData, 0, aData.Length);
RijndaelManaged rijndael = new RijndaelManaged();
//rijndael.Padding = PaddingMode.None;
rijndael.Padding = PaddingMode.ANSIX923;
ICryptoTransform encryptor = rijndael.CreateEncryptor(aKey, aIV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(aData, 0, aData.Length);
cryptoStream.FlushFinalBlock();
}
encryptedData = memoryStream.ToArray();
}
//convert from byte array to string
String sData = System.Text.ASCIIEncoding.ASCII.GetString(encryptedData);
FileStream fsEncypt = new FileStream(sEncryptedFile, FileMode.OpenOrCreate, FileAccess.Write);
// Create a StreamWriter using the CryptoStream.
StreamWriter sWriter = new StreamWriter(fsEncypt);
try
{
//write the encrypted string
sWriter.WriteLine(sData);
}
This seems to work fine (at least a get some text in the sEncryptedFile file).
when i try to decrypt it i get a "Length of the data to decrypt is invalid." error. Code is as follows:
RijndaelManaged rijndael = new RijndaelManaged();
rijndael.Padding = PaddingMode.ANSIX923;
//Get the Key and Vector from File
StreamReader re = new StreamReader("C:\\key.txt");
string s = re.ReadToEnd();
int iKeyEndPoint;
iKeyEndPoint = s.IndexOf("KEY");
string sKey = s.Substring(0, (iKeyEndPoint));
string sIV = s.Remove(0, iKeyEndPoint);
sIV = sIV.Replace("KEY", "");
//convert strings to byte arrays
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] aKey = encoding.GetBytes(sKey);
byte[] aIV = encoding.GetBytes(sIV);
ICryptoTransform decryptor = rijndael.CreateDecryptor(aKey, aIV);
FileStream fEncryptStream = new FileStream("C:\\Encrypted.txt", FileMode.Open, FileAccess.Read);
byte[] aData = new byte[fEncryptStream.Length];
fEncryptStream.Read(aData, 0, aData.Length);
string str = System.Text.ASCIIEncoding.ASCII.GetString(aData);
byte[] decrypted;
using (MemoryStream memoryStream = new MemoryStream(aData))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
int length = aData.Length;
decrypted = new byte[length];
cryptoStream.Read(decrypted, 0, length);
}
}
str = System.Text.ASCIIEncoding.ASCII.GetString(decrypted);
I've tried adding rijndael.Padding = none and removing this live all together. If anyone can point out anything that i've done wrong that would help a great deal!