我创建了一个使用AES加密文件的代码,并使用RSA加密新的AES密钥,然后使用lsb steganography将所有内容嵌入到图像中。当我在相同的调试中进行加密和解密时,代码运行正常,但是当我在一个调试中进行加密然后关闭应用程序并在另一个调试中运行解密时,解密代码无法正常运行,当我跟踪代码时这种情况我发现问题出在RSAdecryption()
函数中,因为在这种情况下它返回null。
加密代码是
EncryptFile(pass_txt.Text, loadedFilePath, output);
fileContainer = File.ReadAllBytes(output);
hashcode = SHA256.Create().ComputeHash(fileContainer);
Newpassword = CreateRandomPassword(pass_txt.Text.Length);
Newpasswordbytes = Byteconverter.GetBytes(Newpassword);
RSAplain= Combine(hashcode,Newpasswordbytes);
RSAcipher = RSAencryption(RSAplain, RSA.ExportParameters(false), false);
byte[] header = new byte[3];
int fileLength = fileContainer.Length;
header[0] = (byte)((fileLength >> 16) & 0xff);
header[1] = (byte)((fileLength >> 8) & 0xff);
header[2] = (byte)(fileLength & 0xff);
bytestobehidden = Combine(header, fileContainer);
//byte[] enchashandnewpass = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
bytestobehidden = Combine(bytestobehidden, RSAcipher);
fileSize = bytestobehidden.Length;
if (8 * ((height * (width / 3) * 3) / 3 - 1) < fileSize + fileNameSize)
{
MessageBox.Show("File size is too large!\nPlease use a larger image to hide this file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
StegoLayer();
,解密代码是
byte[] bytestobedecrypted= ExtractLayer();
int fileLength = (int)(bytestobedecrypted[0] << 16) +
(int)(bytestobedecrypted[1] << 8) +
(int)bytestobedecrypted[2];
byte[] filebytes = new byte[fileLength];
byte[] RSACipher = new byte[bytestobedecrypted.Length - fileLength - 3];
System.Array.Copy(bytestobedecrypted, 3, filebytes, 0, fileLength);
System.Array.Copy(bytestobedecrypted, fileLength + 3, RSACipher, 0, bytestobedecrypted.Length - fileLength - 3);
byte[] hashplusnewpass = RSAdecryption(RSACipher, RSA.ExportParameters(true), false);
byte[] newpass = new byte[hashplusnewpass.Length-32];
byte[] hash=new byte[32];
Array.Copy(hashplusnewpass, 0, hash, 0, 32);
Array.Copy(hashplusnewpass, 32, newpass, 0, newpass.Length);
string newpasswrd = Byteconverter.GetString(newpass);
//Array.Copy(bytestobedecrypted, 0, filebytes, 0, filebytes.Length - 1);
if (File.Exists(DSaveFilePath + "\\ext" + resFName))
{
MessageBox.Show("File \"ext" + resFName + "\" already exist please choose another path to save file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
File.WriteAllBytes(DSaveFilePath + "\\ext" + resFName, filebytes);
string infile = DSaveFilePath + "\\ext" + resFName;
string outfile = DSaveFilePath + "\\dec" + resFName;
DecryptFile(decpass_txt.Text, infile, outfile);
toolStripStatusLabel1.Text = "Decrypted file has been successfully saved.";
MessageBox.Show("The password for next session is" + newpasswrd, "information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.DoEvents();
}
RSAencryption()
和RSAdecryption()
public static byte[] RSAencryption(byte[] Data, RSAParameters RSAkey, bool DoOAEPPadding)
{
try
{
byte[] EncryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAkey);
EncryptedData = RSA.Encrypt(Data, DoOAEPPadding);
}
return EncryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
public static byte[] RSAdecryption(byte[] Data, RSAParameters RSAkey, bool DoOAEPPadding)
{
try
{
byte[] DecryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.ImportParameters(RSAkey);
DecryptedData = RSA.Decrypt(Data, DoOAEPPadding);
}
return DecryptedData;
}
catch (CryptographicException e)
{
Console.WriteLine(e.ToString());
return null;
}
}
我在公共变量中定义RSA提供者
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();