Пример #1
0
    public void Encrypt_Decrypt_Stuff()
    {
        var saltSeed  = "salty";
        var password  = "******";
        var saltBytes = KpEncoding.NormalizeEncode(saltSeed);
        var passBytes = KpEncoding.NormalizeEncode(password);

        // Must be 16 bytes
        var masterSalt = Sodium.GenericHash.Hash(saltBytes, null, 16);
        // Must be 32 bytes
        var masterKey = Sodium.PasswordHash.ArgonHashBinary(passBytes, masterSalt, outputLength: 32);

        var pRec = new P.Record
        {
            Id = Guid.NewGuid(),
            CreatedDateTime = DateTime.Now,
        };

        var recS = new I.RecordSummary();
        var recC = new I.RecordContent();

        recS.Type     = "kypr:password";
        recS.Label    = "First Secret";
        recS.Username = "******";
        recS.Address  = "https://example.com/";
        recS.Tags     = "tag-1 tag-2 tag-3";

        recC.Password = "******";
        recC.Memo     = "A little note to myself";
        recC.Fields   = new()
        {
            new() { Type = "text", Name = "F1", Value = "V1", },
            new() { Type = "text", Name = "F2", Value = "V2", },
            new() { Type = "text", Name = "F3", Value = "V3", },
        };

        var symm = new Krypto.SecretKeyEncryption();

        var summarySer = KpMsgPack.Ser(recS);
        var contentSer = KpMsgPack.Ser(recC);

        pRec.SummaryEnc = symm.Encrypt(summarySer, masterKey);
        pRec.ContentEnc = symm.Encrypt(contentSer, masterKey);

        var recSSer = symm.Decrypt(pRec.SummaryEnc, masterKey);
        var recCSer = symm.Decrypt(pRec.ContentEnc, masterKey);

        // Make sure Encryption/Decryption works
        Assert.Equal(summarySer, recSSer);
        Assert.Equal(contentSer, recCSer);

        var summaryEnc2 = symm.Encrypt(summarySer, masterKey);
        var contentEnc2 = symm.Encrypt(contentSer, masterKey);

        // Make sure 2 encryptions produce different outputs
        Assert.NotEqual(pRec.SummaryEnc, summaryEnc2);
        Assert.NotEqual(pRec.ContentEnc, contentEnc2);
    }
Пример #2
0
    public byte[] VerifiedEncrypt(byte[] clearData, byte[] recvPublicKey, byte[] sendPrivateKey)
    {
        var nonce     = Sodium.PublicKeyBox.GenerateNonce();
        var cryptData = Sodium.PublicKeyBox.Create(clearData, nonce, sendPrivateKey, recvPublicKey);
        var total     = new NonceAndCrypt
        {
            Nonce = nonce,
            Crypt = cryptData,
        };

        return(KpMsgPack.Ser(total));
    }
Пример #3
0
    public byte[] Encrypt(byte[] clearData, byte[] key)
    {
        var nonce = Sodium.SecretBox.GenerateNonce();
        var crypt = Sodium.SecretBox.Create(clearData, nonce, key);
        var total = new NonceAndCrypt
        {
            Nonce = nonce,
            Crypt = crypt,
        };

        return(KpMsgPack.Ser(total));
    }
Пример #4
0
    public byte[] VerifiedDecrypt(byte[] cryptData, byte[] sendPublicKey, byte[] recvPrivateKey)
    {
        var total = KpMsgPack.Des <NonceAndCrypt>(cryptData);

        return(Sodium.PublicKeyBox.Open(total.Crypt, total.Nonce, recvPrivateKey, sendPublicKey));
    }
Пример #5
0
    public byte[] Decrypt(byte[] cryptData, byte[] key)
    {
        var total = KpMsgPack.Des <NonceAndCrypt>(cryptData);

        return(SecretBox.Open(total.Crypt, total.Nonce, key));
    }