J
jp2msft
I have a routine that can encrypt or decrypt a file.
What I want is to add a small buffer (the public key) to the start of the
file. This way, I can check a file before performing my operation on it to
ensure I am not about to encrypt something that has already been encrypted.
Unfortunately, the CryptoStream does not allow me to position the starting
position.
I can write my key to the new buffer, but I can not add the encrypted data
to it afterwards.
Is there a way around this? Should I encrypt the file and append the file
with my key? I suppose I could check the end of each file for my encryption
key, but I have not tested that - and adding it to the first of the file
would certainly be faster!
Here is my chopped down (untested) code:
// int m_MB = 1024 * 1024; // 1 MB buffer
// ICryptoTransform m_Decrypter
// byte[] m_bEncHeader ... is my public key!
// byte[] m_HeadBuf = new byte[m_bEncHeader.Length];
// bool BufferMatch(byte[] A, byte[] B) ... returns true if each byte matches
if (File.Exists(file) == true) {
int len = -1;
string activeFile = "~tmp";
byte[] buf = new byte[m_MB];
FileStream m_copyFS = null;
FileStream m_workFS = null;
File.Copy(file, activeFile, true);
m_copyFs = new FileStream(activeFile, FileMode.Open, FileAccess.Read);
len = m_copyFs.Read(m_HeadBuf, 0, m_HeadBuf.Length);
if ((0 < len) && (BufferMatch(m_HeadBuf, m_bEncHeader) == true)) {
m_workFs = new FileStream(file, FileMode.Create, FileAccess.Write);
CryptoStream cs = new CryptoStream(m_workFs, m_Decrypter,
CryptoStreamMode.Write);
cs.Position = len; // this line throws my exception
do {
len = m_copyFs.Read(buf, 0, m_MB);
cs.Write(buf, 0, len);
} while (0 < len);
cs.Close();
}
m_copyFs.Close();
m_workFs.Close();
File.Delete(activeFile);
}
What I want is to add a small buffer (the public key) to the start of the
file. This way, I can check a file before performing my operation on it to
ensure I am not about to encrypt something that has already been encrypted.
Unfortunately, the CryptoStream does not allow me to position the starting
position.
I can write my key to the new buffer, but I can not add the encrypted data
to it afterwards.
Is there a way around this? Should I encrypt the file and append the file
with my key? I suppose I could check the end of each file for my encryption
key, but I have not tested that - and adding it to the first of the file
would certainly be faster!
Here is my chopped down (untested) code:
// int m_MB = 1024 * 1024; // 1 MB buffer
// ICryptoTransform m_Decrypter
// byte[] m_bEncHeader ... is my public key!
// byte[] m_HeadBuf = new byte[m_bEncHeader.Length];
// bool BufferMatch(byte[] A, byte[] B) ... returns true if each byte matches
if (File.Exists(file) == true) {
int len = -1;
string activeFile = "~tmp";
byte[] buf = new byte[m_MB];
FileStream m_copyFS = null;
FileStream m_workFS = null;
File.Copy(file, activeFile, true);
m_copyFs = new FileStream(activeFile, FileMode.Open, FileAccess.Read);
len = m_copyFs.Read(m_HeadBuf, 0, m_HeadBuf.Length);
if ((0 < len) && (BufferMatch(m_HeadBuf, m_bEncHeader) == true)) {
m_workFs = new FileStream(file, FileMode.Create, FileAccess.Write);
CryptoStream cs = new CryptoStream(m_workFs, m_Decrypter,
CryptoStreamMode.Write);
cs.Position = len; // this line throws my exception
do {
len = m_copyFs.Read(buf, 0, m_MB);
cs.Write(buf, 0, len);
} while (0 < len);
cs.Close();
}
m_copyFs.Close();
m_workFs.Close();
File.Delete(activeFile);
}