Пример #1
0
        private static byte[] OpenSSHPassphraseToKey(SecureString passphrase, byte[] iv, int length) {
            const int HASH_SIZE = 16;
            const int SALT_SIZE = 8;
            var md5 = new MD5CryptoServiceProvider();
            //var pp  = Encoding.UTF8.GetBytes(passphrase);
            var buf = new byte[((length + HASH_SIZE - 1) / HASH_SIZE) * HASH_SIZE];
            var offset = 0;

            while (offset < length) {
                if (offset > 0)
                    md5.TransformBlock(buf, 0, offset, null, 0);
                //md5.TransformBlock(pp, 0, pp.Length, null, 0);
                md5.HashBlock(passphrase, false);
                md5.TransformFinalBlock(iv, 0, SALT_SIZE);
                Buffer.BlockCopy(md5.Hash, 0, buf, offset, HASH_SIZE);
                offset += HASH_SIZE;
                md5.Initialize();
            }
            md5.Clear();

            var key = new byte[length];
            Buffer.BlockCopy(buf, 0, key, 0, length);
            return key;
        }