3 namespace SharpCompress.Common.Zip
5 internal class WinzipAesEncryptionData
7 private const int RFC2898_ITERATIONS = 1000;
10 private WinzipAesKeySize keySize;
11 private byte[] passwordVerifyValue;
12 private string password;
14 private byte[] generatedVerifyValue;
16 internal WinzipAesEncryptionData(WinzipAesKeySize keySize, byte[] salt, byte[] passwordVerifyValue,
19 this.keySize = keySize;
21 this.passwordVerifyValue = passwordVerifyValue;
22 this.password = password;
26 internal byte[] IvBytes { get; set; }
27 internal byte[] KeyBytes { get; set; }
29 private int KeySizeInBytes
31 get { return KeyLengthInBytes(keySize); }
34 internal static int KeyLengthInBytes(WinzipAesKeySize keySize)
38 case WinzipAesKeySize.KeySize128:
40 case WinzipAesKeySize.KeySize192:
42 case WinzipAesKeySize.KeySize256:
45 throw new InvalidOperationException();
48 private void Initialize()
50 System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 =
51 new System.Security.Cryptography.Rfc2898DeriveBytes(password, salt, RFC2898_ITERATIONS);
53 KeyBytes = rfc2898.GetBytes(KeySizeInBytes); // 16 or 24 or 32 ???
54 IvBytes = rfc2898.GetBytes(KeySizeInBytes);
55 generatedVerifyValue = rfc2898.GetBytes(2);
57 short verify = BitConverter.ToInt16(passwordVerifyValue, 0);
60 short generated = BitConverter.ToInt16(generatedVerifyValue, 0);
61 if (verify != generated)
62 throw new InvalidFormatException("bad password");