示例#1
0
        private void performTest(int number, TestCase testCase)
        {
            byte[] plaintext = testCase.Plaintext;
            byte[] output    = new byte[plaintext.Length];

            XSalsa20Engine engine = new XSalsa20Engine();

            engine.Init(false, new ParametersWithIV(new KeyParameter(testCase.Key), testCase.Iv));

            engine.ProcessBytes(testCase.Plaintext, 0, testCase.Plaintext.Length, output, 0);

            if (!Arrays.AreEqual(testCase.Ciphertext, output))
            {
                Fail("mismatch on " + number, Hex.ToHexString(testCase.Ciphertext), Hex.ToHexString(output));
            }
        }
示例#2
0
        private byte[] EncryptXSalsa20Poly1305(byte[] bytes, byte[] key, byte[] nonce)
        {
            var salsa = new XSalsa20Engine();
            var poly  = new Poly1305();

            salsa.Init(true, new ParametersWithIV(new KeyParameter(key), nonce));

            byte[] subKey = new byte[key.Length];
            salsa.ProcessBytes(subKey, 0, key.Length, subKey, 0);

            byte[] output = new byte[bytes.Length + poly.GetMacSize()];

            salsa.ProcessBytes(bytes, 0, bytes.Length, output, poly.GetMacSize());

            poly.Init(new KeyParameter(subKey));
            poly.BlockUpdate(output, poly.GetMacSize(), bytes.Length);
            poly.DoFinal(output, 0);

            return(output);
        }