I know doubleDES is not used sine of Meet-in-the-middle Attack but i need to create a program that does that kind of encryption. I tried this but i think im stuck and just cannot figure out what am i missing..
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.Zeros;
// *****ENKRIPTIMI*****
DES.Key = utf8.GetBytes(textBox1.Text.Substring(0, 8));
StreamReader sr = new StreamReader(textBox2.Text);
string permbajtja = sr.ReadToEnd();
sr.Close();
FileStream fs1 = new FileStream(textBox2.Text, FileMode.Create, FileAccess.Write);
CryptoStream cs = new CryptoStream(fs1, DES.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(permbajtja); sw.Flush();
sw.Close();
StreamReader stream = new StreamReader(textBox2.Text);
string msg = stream.ReadToEnd();
stream.Close();
MessageBox.Show(msg);
//*****DEKRIPTIMI*****
DES.Key = utf8.GetBytes(textBox1.Text.Substring(8, 8));
FileStream fs2 = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read);
CryptoStream cs1 = new CryptoStream(fs2, DES.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader sr1 = new StreamReader(cs1);
string permbajtja1 = sr1.ReadToEnd();
// MessageBox.Show(permbajtja1);
sr1.Close();
fs2.Dispose();
fs2.Close();
StreamWriter sw1 = new StreamWriter(textBox2.Text);
sw1.Write(permbajtja1);
sw1.Flush();
sw1.Close();
**** THE PART ABOVE IS JUST FOR ENCRYPTING *****
The decryption part
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Mode = CipherMode.ECB;
DES.Padding = PaddingMode.Zeros;
// *****ENKRIPTIMI*****
DES.Key = utf8.GetBytes(textBox1.Text.Substring(8,8));
StreamReader sr = new StreamReader(textBox2.Text);
string permbajtja = sr.ReadToEnd();
sr.Close();
FileStream fs1 = new FileStream(textBox2.Text, FileMode.Create, FileAccess.Write);
CryptoStream cs = new CryptoStream(fs1, DES.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(permbajtja); sw.Flush();
sw.Close();
StreamReader lexo = new StreamReader(textBox2.Text);
MessageBox.Show(lexo.ReadToEnd());
lexo.Close();
////*****DEKRIPTIMI*****
DES.Key = utf8.GetBytes(textBox1.Text.Substring(0, 8));
FileStream fs2 = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read);
CryptoStream cs1 = new CryptoStream(fs2, DES.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader sr1 = new StreamReader(cs1);
string permbajtja1 = sr1.ReadToEnd();
MessageBox.Show(permbajtja1);
sr1.Close();
fs2.Dispose();
fs2.Close();
StreamWriter sw1 = new StreamWriter(textBox2.Text);
sw1.Write(permbajtja1);
sw1.Flush();
sw1.Close();
First of all you should write the encrypt and the decrypt method:
public static string Encrypt(string originalString, byte[] key)
{
DESCryptoServiceProvider cryptoServiceProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoServiceProvider.CreateEncryptor(key, key),
CryptoStreamMode.Write);
using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(originalString);
streamWriter.Flush();
cryptoStream.FlushFinalBlock();
streamWriter.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
}
public static string Decrypt(string cryptedString, byte[] key)
{
DESCryptoServiceProvider cryptoServiceProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoServiceProvider.CreateDecryptor(key, key),
CryptoStreamMode.Read);
using (StreamReader streamReader = new StreamReader(cryptoStream))
{
return streamReader.ReadToEnd();
}
}
Now you can apply twice the encryption method (and of course twice the decryption one) to your string:
byte[] key1 = ASCIIEncoding.ASCII.GetBytes("12345678");
byte[] key2 = ASCIIEncoding.ASCII.GetBytes("abcdefgh");
string originalString = "A secret string";
string cryptedString = Encrypt(Encrypt(originalString, key1), key2);
Console.WriteLine("Encrypt message: {0}", cryptedString);
Console.WriteLine("Decrypt message: {0}", Decrypt(Decrypt(cryptedString, key2), key1));
Take a look to my code, it may help you.
Related
I am trying to encrypt and decrypt a stream (a PDF document at base), but I am having issues with this. When I try to open the document after decryption and download, I get the error Failed to load the PDF document.
Do you know why this might be happening?
Here is the code for encryption:
public EncryptResult EncryptStream(Stream dataStream, bool reuseIV = false)
{
RijndaelManaged crypto = new RijndaelManaged();
crypto.Key = _key;
if (!reuseIV || _iv == null)
{
// make a copy of the current IV
_iv = crypto.IV;
}
else
{
// reuse the previous IV
crypto.IV = _iv;
}
var result = new EncryptResult() { IV = crypto.IV };
using (var encryptor = crypto.CreateEncryptor())
{
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
var byteArrayInput = new byte[dataStream.Length];
dataStream.Read(byteArrayInput, 0, byteArrayInput.Length);
csEncrypt.Write(byteArrayInput, 0, byteArrayInput.Length);
dataStream.Close();
result.Cipher = msEncrypt.ToArray();
msEncrypt.Flush();
msEncrypt.Position = 0;
return result;
}
}
}
}
and decryption:
public Stream DecryptStream(byte[] cipher, byte[] iv)
{
RijndaelManaged crypto = new RijndaelManaged();
crypto.Key = _key;
crypto.IV = iv;
crypto.Padding = PaddingMode.Zeros;
using (var decryptor = crypto.CreateDecryptor())
{
using (MemoryStream msDecrypt = new MemoryStream(cipher))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
var sOutputFilename = new MemoryStream();
var fsDecrypted = new StreamWriter(sOutputFilename);
fsDecrypted.Write(new StreamReader(csDecrypt).ReadToEnd());
sOutputFilename.Position = 0;
return sOutputFilename;
}
}
}
}
Thanks in advance.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
//var byteArrayInput = new byte[dataStream.Length];
//dataStream.Read(byteArrayInput, 0, byteArrayInput.Length);
//csEncrypt.Write(byteArrayInput, 0, byteArrayInput.Length);
dataStream.CopyTo(csEncrypt);
dataStream.Close();
//result.Cipher = msEncrypt.ToArray(); // not here - not flushed yet
//msEncrypt.Flush(); // don't need this
//msEncrypt.Position = 0;
}
result.Cipher = msEncrypt.ToArray();
return result;
}
and in the decryptor, get rid of all the StreamReader/StreamWriter stuff.
A PDF file is compressed, ie binary. But this is after the decryption so it can't be your error.
using (var decryptor = crypto.CreateDecryptor())
{
using (MemoryStream msDecrypt = new MemoryStream(cipher))
{
var outputStream = new MemoryStream();
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
csDecrypt.CopyTo(outputStream );
}
outputStream .Position = 0;
return outputStream ;
}
}
One issue is, you are likely encrypting excess bytes at the end of your stream, you need to work out how many bytes are read or use
Stream.CopyTo Method
Reads the bytes from the current stream and writes them to another
stream.
I have an XML file with information about my stringConnection parameters which decrypted with this method:
public void DecryptFile(string sInputFileName, string sOutputFileName, string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
FileStream fsread = new FileStream(sInputFileName, FileMode.Open, FileAccess.Read);
ICryptoTransform desdecrypt = DES.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);
//Print result
StreamWriter fsDecrypted = new StreamWriter(sOutputFileName);
//In this point ReadToEnd return the final xml result decrypted
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
fsread.Close();
fsread.Dispose();
}
The result would be this:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<TU>
<BD>DataBaseName</BD>
<SR>ServerName</SR>
<USR>UserDB</USR>
<CONT>Pass</CONT>
<EMP>codCompany</EMP>
<EMPDES>companyName</EMPDES>
<SUC>codLocal</SUC>
</TU>
</NewDataSet>
But ReadToEnd doesn't return the last label of my XML file, like this:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<TU>
<BD>DataBaseName</BD>
<SR>ServerName</SR>
<USR>UserDB</USR>
<CONT>Pass</CONT>
<EMP>codCompany</EMP>
<EMPDES>companyName</EMPDES>
<SUC>codLocal</SUC>
</TU>
</NewDataSet
What's wrong with my method?
The main issue is that you are not really reading the CryptoStream to the end, because of the way it blocks data (as pointed out in this answer). Here is an approach adapted from this answer which uses a buffer to read 1024 bytes at a time.
public void DecryptFile(string sInputFileName, string sOutputFileName, string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desdecrypt = DES.CreateDecryptor();
using (FileStream fsread = new FileStream(sInputFileName, FileMode.Open, FileAccess.Read))
using (StreamWriter fsDecrypted = new StreamWriter(sOutputFileName))
using (CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read))
{
//Print result
var buffer = new byte[1024];
var read = cryptostreamDecr.Read(buffer, 0, buffer.Length);
while (read > 0)
{
fsDecrypted.Write(buffer, 0, read);
read = cryptostreamDecr.Read(buffer, 0, buffer.Length);
}
}
}
You are also missing using statements, which ensure your streams are disposed correctly whether or not your program has an exception.
guys, I'm new to this website and I'm a really new to programming any help would be awesome! Thanks Below are my code for encryption and decryption. Images and PDF files get encrypted and decrypted no problems, but text files I don't know it's not showing anything in the file after decryption.
private void encrypt (string input, string output, string strHash )
{
FileStream inStream, outStream;
CryptoStream CryStream;
TripleDESCryptoServiceProvider TDC = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteTexto;
inStream = new FileStream(input, FileMode.Open, FileAccess.Read);
outStream = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write);
byteHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash));
byteTexto = File.ReadAllBytes(input);
md5.Clear();enter code here
TDC.Key = byteHash;
TDC.Mode = CipherMode.ECB;
CryStream = new CryptoStream(outStream, TDC.CreateEncryptor(), CryptoStreamMode.Write);
int bytesRead;
long length, position = 0;
length = inStream.Length;
while(position < length)
{
bytesRead = inStream.Read(byteTexto, 0, byteTexto.Length);
position += bytesRead;
CryStream.Write(byteTexto, 0, bytesRead);
}
inStream.Close();
outStream.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnDecrypt_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.ShowDialog();
txtLocalDecFile.Text = open.FileName;
SaveFileDialog save = new SaveFileDialog();
save.ShowDialog();
txtNewDecFile.Text = save.FileName;
decrypt(txtLocalDecFile.Text, txtNewDecFile.Text, key);
}
private void decrypt(string input, string output, string strHash)
{
FileStream inStream, outStream;
CryptoStream CryStream;
TripleDESCryptoServiceProvider TDC = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteTexto;
inStream = new FileStream(input, FileMode.Open, FileAccess.Read);
outStream = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write);
byteHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash));
byteTexto = File.ReadAllBytes(input);
md5.Clear();
TDC.Key = byteHash;
TDC.Mode = CipherMode.ECB;
CryStream = new CryptoStream(outStream, TDC.CreateDecryptor(), CryptoStreamMode.Write);
int bytesRead;
long length, position = 0;
length = inStream.Length;
while (position < length)
{
bytesRead = inStream.Read(byteTexto, 0, byteTexto.Length);
position += bytesRead;
CryStream.Write(byteTexto, 0, bytesRead);
}
inStream.Close();
outStream.Close();
}
You forgot to close your CryptoStream which means that it could not flush all data to the file.
Either close the stream like you do with inStream and outStream or call FlushFinalBlock() on CryStream.
There is an example in the documentation.
Try use AES encryption:
http://www.codeproject.com/Articles/769741/Csharp-AES-bits-Encryption-Library-with-Salt
There is an example of how to encrypt and decrypt
My code below is getting an error stating "Length of the data to decrypt is invalid."
when I try to copy it to MemoryStream. How can I solve this?
var table = (Encoding.Default.GetString(result, 0, result.Length - 1))
.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
MemoryStream encryptedStream = new MemoryStream();
encryptedStream.Write(result, 0, filelength);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desdecrypt = DES.CreateDecryptor();
MemoryStream decryptedStream = new MemoryStream();
encryptedStream.Position = 0;
CryptoStream decryptStream = new CryptoStream(encryptedStream, desdecrypt,
CryptoStreamMode.Read);
decryptStream.CopyTo(decryptedStream);
decryptedStream.Position = 0;
I can read an write my serialized class fine until I try to encrypt / decrypt... here is a snippet of my code:
public class ShelfCache
{
public Shelf Data;
public ShelfCache()
{
Data = new Shelf();
}
public void Write(string Filename)
{
XmlSerializer xsl = new XmlSerializer(typeof(Shelf));
TextWriter xslWriter = new StreamWriter(Filename);
xsl.Serialize(xslWriter, Data);
xslWriter.Flush();
xslWriter.Close();
}
public void Read(string Filename)
{
Data = new Shelf();
XmlSerializer xsl = new XmlSerializer(typeof(Shelf));
TextReader xslReader = new StreamReader(Filename);
Data = (Shelf)xsl.Deserialize(xslReader);
xslReader.Close();
}
public void WriteEncrypted(string Filename, string EncryptionKey = "")
{
string _Key = EncryptionKey + Environment.ExpandEnvironmentVariables("%USERNAME%%COMPUTERNAME%123456789ABCDEF0123456789abcdef").Substring(0, 32);
string _IV = Environment.ExpandEnvironmentVariables("%COMPUTERNAME%123456789abcdef").Substring(0, 16);
byte[] Key = Encoding.UTF8.GetBytes(_Key);
byte[] IV = Encoding.UTF8.GetBytes(_IV);
FileStream CacheStream = new FileStream(Filename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
RijndaelManaged CryptoProvider = new RijndaelManaged();
ICryptoTransform CacheTransform = CryptoProvider.CreateEncryptor(Key, IV);
CryptoStream EncryptionStream = new CryptoStream(CacheStream, CacheTransform, CryptoStreamMode.Write);
XmlSerializer xsl = new XmlSerializer(typeof(Shelf));
xsl.Serialize(EncryptionStream, Data);
EncryptionStream.Flush();
CacheStream.Close();
}
public void ReadEncrypted(string Filename, string EncryptionKey = "")
{
string _Key = EncryptionKey + Environment.ExpandEnvironmentVariables("%USERNAME%%COMPUTERNAME%123456789ABCDEF0123456789abcdef").Substring(0, 32);
string _IV = Environment.ExpandEnvironmentVariables("%COMPUTERNAME%123456789abcdef").Substring(0, 16);
byte[] Key = Encoding.UTF8.GetBytes(_Key);
byte[] IV = Encoding.UTF8.GetBytes(_IV);
FileStream CacheStream = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
RijndaelManaged CryptoProvider = new RijndaelManaged();
ICryptoTransform CacheTransform = CryptoProvider.CreateEncryptor(Key, IV);
CryptoStream DecryptionStream = new CryptoStream(CacheStream, CacheTransform, CryptoStreamMode.Read);
XmlSerializer xsl = new XmlSerializer(typeof(Shelf));
Data = (Shelf)xsl.Deserialize(DecryptionStream);
CacheStream.Close();
}
}
I get the exception "There is an error in XML document (1 1)." with an inner exception "Data at the root level is invalid. Line 1, position 1." on the line:
Data = (Shelf)xsl.Deserialize(DecryptionStream);
This is what I ended up doing (I don't like the solution, but it works)...
public class ShelfCache
{
public Shelf Data;
public ShelfCache()
{
Data = new Shelf();
}
public void Write(string Filename)
{
XmlSerializer CacheSerializer = new XmlSerializer(typeof(Shelf));
TextWriter CacheWriter = new StreamWriter(Filename);
CacheSerializer.Serialize(CacheWriter, Data);
CacheWriter.Flush();
CacheWriter.Close();
}
public void Read(string Filename)
{
Data = new Shelf();
XmlSerializer CacheSerializer = new XmlSerializer(typeof(Shelf));
TextReader CacheReader = new StreamReader(Filename);
Data = (Shelf)CacheSerializer.Deserialize(CacheReader);
CacheReader.Close();
}
public void WriteEncrypted(string Filename, string EncryptionKey = "")
{
string TempFile = System.IO.Path.GetTempFileName();
Write(TempFile);
EncryptFile(TempFile, Filename, EncryptionKey);
File.Delete(TempFile);
}
public void ReadEncrypted(string Filename, string EncryptionKey = "")
{
string TempFile = System.IO.Path.GetTempFileName();
DecryptFile(Filename, TempFile, EncryptionKey);
Read(TempFile);
File.Delete(TempFile);
}
private RijndaelManaged GetEncryptor(string Key = "", string Salt = "")
{
Key += Environment.ExpandEnvironmentVariables("%USERNAME%");
Salt += Environment.ExpandEnvironmentVariables("%COMPUTERNAME%");
Rfc2898DeriveBytes derivedKey = new Rfc2898DeriveBytes(Key, Encoding.Unicode.GetBytes(Salt));
RijndaelManaged rijndaelCSP = new RijndaelManaged();
rijndaelCSP.Key = derivedKey.GetBytes(rijndaelCSP.KeySize / 8);
rijndaelCSP.IV = derivedKey.GetBytes(rijndaelCSP.BlockSize / 8);
return rijndaelCSP;
}
private void EncryptFile(string Source, string Target, string EncryptionKey)
{
RijndaelManaged EncryptionProvider = GetEncryptor(EncryptionKey);
ICryptoTransform Encryptor = EncryptionProvider.CreateEncryptor();
FileStream SourceStream = new FileStream(Source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
byte[] SourceBytes = new byte[(int)SourceStream.Length];
SourceStream.Read(SourceBytes, 0, (int)SourceStream.Length);
FileStream TargetStream = new FileStream(Target, FileMode.Create, FileAccess.Write);
CryptoStream EncryptionStream = new CryptoStream(TargetStream, Encryptor, CryptoStreamMode.Write);
EncryptionStream.Write(SourceBytes, 0, (int)SourceStream.Length);
EncryptionStream.FlushFinalBlock();
EncryptionProvider.Clear();
EncryptionStream.Close();
SourceStream.Close();
TargetStream.Close();
}
private void DecryptFile(string Source, string Target, string EncryptionKey)
{
RijndaelManaged EncryptionProvider = GetEncryptor(EncryptionKey);
ICryptoTransform Decryptor = EncryptionProvider.CreateDecryptor();
FileStream SourceStream = new FileStream(Source, FileMode.Open, FileAccess.Read);
CryptoStream DecryptionStream = new CryptoStream(SourceStream, Decryptor, CryptoStreamMode.Read);
byte[] SourceBytes = new byte[(int)SourceStream.Length];
int DecryptionLength = DecryptionStream.Read(SourceBytes, 0, (int)SourceStream.Length);
FileStream TargetStream = new FileStream(Target, FileMode.Create, FileAccess.Write);
TargetStream.Write(SourceBytes, 0, DecryptionLength);
TargetStream.Flush();
EncryptionProvider.Clear();
DecryptionStream.Close();
SourceStream.Close();
TargetStream.Close();
}
}