我花了最近几天根据RijndaelManaged类提供的Rijndael加密标准创建了一个文件加密/解密类,并且已经搜索了我能找到的所有资源和示例。这些例子要么已经过时,要么已经过时,要么已经过时,但至少可以学到很多东西,并且在确保它是强大的并且过去你的批评后,我会发布一个最新版本。
到目前为止我发现的唯一问题是,需要知道salt,因为除非将每个字节的读/写转换为缓冲区,否则无法将其存储在加密文件中,就像对字符串一样基于读/写然后你需要在解密时满足它,并且还需要至少4个字节的数据来加密(虽然我不认为这是一个问题,但确实需要提及)。
我也不完全确定1个盐是否足以满足密钥和初始化向量,或者出于安全原因两个是否更好?
任何其他观察和/或优化也将非常感激
class FileEncDec
{
private int keySize;
private string passPhrase;
internal FileEncDec( int keySize = 256, string passPhrase = @"This is pass phrase key to use for testing" )
{
this.keySize = keySize;
this.passPhrase = passPhrase; // Can be user selected and must be kept secret
}
private static byte[] GenerateSalt( int length )
{
byte[] salt = new byte[ length ];
// Populate salt with cryptographically strong bytes.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetNonZeroBytes( salt );
// Split salt length (always one byte) into four two-bit pieces and store these pieces in the first four bytes
// of the salt array.
salt[ 0 ] = (byte)( ( salt[ 0 ] & 0xfc ) | ( length & 0x03 ) );
salt[ 1 ] = (byte)( ( salt[ 1 ] & 0xf3 ) | ( length & 0x0c ) );
salt[ 2 ] = (byte)( ( salt[ 2 ] & 0xcf ) | ( length & 0x30 ) );
salt[ 3 ] = (byte)( ( salt[ 3 ] & 0x3f ) | ( length & 0xc0 ) );
return salt;
}
internal bool EncryptFile( string inputFile, string outputFile )
{
try
{
byte[] salt = GenerateSalt( 16 ); // Salt needs to be known for decryption (can be safely stored in the file)
Rfc2898DeriveBytes derivedBytes = new Rfc2898DeriveBytes( passPhrase, salt, 10000 );
int bytesRead, bufferSize = keySize / 8;
byte[] data = new byte[ bufferSize ];
RijndaelManaged cryptor = new RijndaelManaged();
cryptor.Key = derivedBytes.GetBytes( keySize / 8 );
cryptor.IV = derivedBytes.GetBytes( cryptor.BlockSize / 8 );
using ( var fsIn = new FileStream( inputFile, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan ) )
{
using ( var fsOut = new FileStream( outputFile, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.SequentialScan ) )
{
// Add the salt to the file
fsOut.Write( salt, 0, salt.Length );
using ( CryptoStream cs = new CryptoStream( fsOut, cryptor.CreateEncryptor(), CryptoStreamMode.Write ) )
{
while ( ( bytesRead = fsIn.Read( data, 0, bufferSize ) ) > 0 )
{
cs.Write( data, 0, bytesRead );
}
}
}
}
return true;
}
catch ( Exception )
{
return false;
}
}
internal bool DecryptFile( string inputFile, string outputFile )
{
try
{
int bytesRead = 0, bufferSize = keySize / 8, saltLen;
byte[] data = new byte[ bufferSize ], salt;
Rfc2898DeriveBytes derivedBytes;
RijndaelManaged cryptor = new RijndaelManaged(); // Create new cryptor so it's thread safe and don't need to use locks
using ( var fsIn = new FileStream( inputFile, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan ) )
{
// Retrieve the salt length from the file
fsIn.Read( data, 0, 4 );
saltLen = ( data[ 0 ] & 0x03 ) |
( data[ 1 ] & 0x0c ) |
( data[ 2 ] & 0x30 ) |
( data[ 3 ] & 0xc0 );
salt = new byte[ saltLen ];
Array.Copy( data, salt, 4 );
// Retrieve the remaining salt from the file and create the cryptor
fsIn.Read( salt, 4, saltLen - 4 );
derivedBytes = new Rfc2898DeriveBytes( passPhrase, salt, 10000 );
cryptor.Key = derivedBytes.GetBytes( keySize / 8 );
cryptor.IV = derivedBytes.GetBytes( cryptor.BlockSize / 8 );
using ( var fsOut = new FileStream( outputFile, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.SequentialScan ) )
{
using ( var cs = new CryptoStream( fsIn, cryptor.CreateDecryptor(), CryptoStreamMode.Read ) )
{
while ( ( bytesRead = cs.Read( data, 0, bufferSize ) ) > 0 )
{
fsOut.Write( data, 0, bytesRead );
}
}
}
}
return true;
}
catch ( Exception )
{
return false;
}
}
}
编辑:
1.添加盐生成器。
2.重构为单salt
和Rfc2898DerivedBytes
,现在从IV
+ password
推断出salt
。
3.使加密/解密线程安全(如果我没有正确地做,请告诉我)。
编辑2:
1.重构,以便读/写使用缓冲区而不是单字节读/写。
2.嵌入式盐在加密文件中并清理变量(但仍然允许passPhrase
默认为“复制/粘贴”示例。
3.重构文件句柄。
答案 0 :(得分:1)
每次你可能应该使用不同的IV。如果您使用具有相同数据的相同IV,则结果相同。攻击者现在可以推断出文件是(部分)相同的是泄漏。您可以生成16个强随机字节,并将它们用作Rfc2898DeriveBytes
的盐。将这些字节前置到文件中。仅使用一个Rfc2898DeriveBytes
生成IV和密钥。或者,您可以根本不使用盐作为密钥并随机生成IV。 salt可用于使密钥派生对您的用例而言是独特的,或者例如为您的应用程序的每个用户提供不同的密钥派生算法。
请注意,按字节顺序处理流的速度非常慢。使用缓冲区。您可能应该使用Stream.Copy
。