KeePassLib.Keys.CompositeKey.GenerateKey32 C# (CSharp) Method

GenerateKey32() public method

Generate a 32-bit wide key out of the composite key.
public GenerateKey32 ( byte pbKeySeed32, ulong uNumRounds ) : KeePassLib.Security.ProtectedBinary
pbKeySeed32 byte Seed used in the key transformation /// rounds. Must be a byte array containing exactly 32 bytes; must /// not be null.
uNumRounds ulong Number of key transformation rounds.
return KeePassLib.Security.ProtectedBinary
        public ProtectedBinary GenerateKey32(byte[] pbKeySeed32, ulong uNumRounds)
        {
            Debug.Assert(pbKeySeed32 != null);
            if(pbKeySeed32 == null) throw new ArgumentNullException("pbKeySeed32");
            Debug.Assert(pbKeySeed32.Length == 32);
            if(pbKeySeed32.Length != 32) throw new ArgumentException("pbKeySeed32");

            byte[] pbRaw32 = CreateRawCompositeKey32();
            if((pbRaw32 == null) || (pbRaw32.Length != 32))
                { Debug.Assert(false); return null; }

            byte[] pbTrf32 = TransformKey(pbRaw32, pbKeySeed32, uNumRounds);
            if((pbTrf32 == null) || (pbTrf32.Length != 32))
                { Debug.Assert(false); return null; }

            ProtectedBinary pbRet = new ProtectedBinary(true, pbTrf32);
            MemUtil.ZeroByteArray(pbTrf32);
            MemUtil.ZeroByteArray(pbRaw32);

            return pbRet;
        }

Usage Example

Beispiel #1
0
    public static void Main(string[] args)
    {
        CompositeKey key = new CompositeKey();
        KcpPassword pw = new KcpPassword("12345");
        key.AddUserKey(pw);
        byte[] pwdata = pw.KeyData.ReadData();
        Console.WriteLine("PW data:");
        Console.WriteLine(string.Join(",", pwdata.Select(x => "0x" + x.ToString("x"))));
        byte[] keydata = key.GenerateKey32(pwdata, 6000).ReadData();
        Console.WriteLine("Key data:");
        Console.WriteLine(string.Join(",", keydata.Select(x => "0x" + x.ToString("x"))));

        PwDatabase db = new PwDatabase();
        db.MasterKey = key;
        KdbxFile kdbx = new KdbxFile(db);
        kdbx.Load(@"..\resources\test.kdbx", KdbxFormat.Default, null);

        var groups = db.RootGroup.GetGroups(true);
        Console.WriteLine("Group count: " + groups.UCount);
        var entries = db.RootGroup.GetEntries(true);
        Console.WriteLine("Entry count: " + entries.UCount);

        CompositeKey key2 = new CompositeKey();
        key2.AddUserKey(pw);
        KcpKeyFile keyfile = new KcpKeyFile(@"..\resources\keyfile.key");
        key2.AddUserKey(keyfile);
        byte[] keyfiledata = keyfile.KeyData.ReadData();
        Console.WriteLine("Key file data:");
        Console.WriteLine(string.Join(",", keyfiledata.Select(x => "0x" + x.ToString("x"))));
        Console.WriteLine("Composite Key data:");
        byte[] key2data = key2.GenerateKey32(keyfiledata, 6000).ReadData();
        Console.WriteLine(string.Join(",", key2data.Select(x => "0x" + x.ToString("x"))));
    }