public void StreamEncryptionDecryptChaCha20BadKey()
 {
     StreamEncryption.DecryptChaCha20(
         Utilities.HexToBinary("a6ce598d8b865fb328581bcd"),
         Encoding.UTF8.GetBytes("ABCDEFGH"),
         Encoding.UTF8.GetBytes("123456789012345678901234567890"));
 }
示例#2
0
    private void Start()
    {
        int x = NativeLibsodium.sodium_init();

        Debug.Log(x);


        const string MESSAGE = "Test message to encrypt";

        byte[] nonce = StreamEncryption.GenerateNonceChaCha20();
        byte[] key   = StreamEncryption.GenerateKey();

        //encrypt it
        byte[] encrypted = StreamEncryption.EncryptChaCha20(MESSAGE, nonce, key);


        //decrypt it
        byte[] decrypted = StreamEncryption.DecryptChaCha20(encrypted, nonce, key);

        string str_origin    = MESSAGE;
        string str_encrypted = Encoding.UTF8.GetString(encrypted);
        string str_decrypted = Encoding.UTF8.GetString(decrypted);

        Debug.Log(str_origin);
        Debug.Log(str_encrypted);
        Debug.Log(str_decrypted);

        txt_origin.text    = str_origin;
        txt_encrypted.text = str_encrypted;
        txt_decrypted.text = str_decrypted;
    }
 public void OpenSecretBoxChaCha20()
 {
     const string EXPECTED = "Adam Caudill";
     var actual = Encoding.UTF8.GetString(StreamEncryption.DecryptChaCha20(
       Utilities.HexToBinary("a6ce598d8b865fb328581bcd"),
       Encoding.UTF8.GetBytes("ABCDEFGH"),
       Encoding.UTF8.GetBytes("12345678901234567890123456789012")));
     Assert.AreEqual(EXPECTED, actual);
 }
        public void Stream_Encryption_Basic()
        {
            var nonce         = StreamEncryption.GenerateNonceChaCha20();
            var key           = StreamEncryption.GenerateKey();
            var messageString = "Test message to encrypt";
            var encrypted     = StreamEncryption.EncryptChaCha20(messageString, nonce, key);
            var decrypted     = StreamEncryption.DecryptChaCha20(encrypted, nonce, key);

            Assert.AreEqual(messageString, Encoding.UTF8.GetString(decrypted));
        }
 public void StreamEncryptionDecryptChaCha20BadNonce()
 {
     Assert.Throws <NonceOutOfRangeException>(() =>
     {
         StreamEncryption.DecryptChaCha20(
             Utilities.HexToBinary("a6ce598d8b865fb328581bcd"),
             Encoding.UTF8.GetBytes("ABC"),
             Encoding.UTF8.GetBytes("12345678901234567890123456789012"));
     });
 }
        public void ChaCha20Test()
        {
            var    key     = StreamEncryption.GenerateKey();
            var    nonce   = StreamEncryption.GenerateNonceChaCha20();
            string message = "Hello, World!";

            var cipherText = StreamEncryption.EncryptChaCha20(message, nonce, key);
            var decrypted  = StreamEncryption.DecryptChaCha20(cipherText, nonce, key);

            Assert.AreEqual(message, System.Text.Encoding.UTF8.GetString(decrypted));

            byte[] byteMessage = System.Text.Encoding.UTF8.GetBytes(message);
            cipherText = StreamEncryption.EncryptChaCha20(byteMessage, nonce, key);
            decrypted  = StreamEncryption.DecryptChaCha20(cipherText, nonce, key);
            Assert.AreEqual(Convert.ToBase64String(byteMessage), Convert.ToBase64String(decrypted));
        }
    public byte[] Decrypt(byte[] encrypted, long nonce)
    {
        // var nonce = StreamEncryption.GenerateNonceChaCha20();
        // var key = StreamEncryption.GenerateKey();
        // var messageString = "Test message to encrypt";
        // var encrypted = StreamEncryption.EncryptChaCha20(messageString, nonce, key);
        // var decrypted = StreamEncryption.DecryptChaCha20(encrypted, nonce, key);

        var nonceByteArray = BitConverter.GetBytes(nonce);

        return(StreamEncryption.DecryptChaCha20(encrypted, nonceByteArray, _encryptionKey));

        // public const string ChaChaEncryptionKey = "f2ZWDvhBXFSxKXpCl0wg9aEZnuXfA+B2c+5RU8wWbfQ=" ;
        // public const string ChaChaSigPublicKey = "6T7iL581iPWn1X/Nm8AD2PNRFDqGyGlRfslNy0SD63M=";
        // public const string ChaChaSigPrivateKey =
    }
示例#8
0
        /// <summary>
        /// Decrypt a message using provided key and nonce
        /// </summary>
        /// <param name="encrypted">Ciphertext message</param>
        /// <param name="key">Plaintext/Base64-encoded key</param>
        /// <param name="nonce">Base64-encoded nonce (generate using GenerateNonce())</param>
        /// <returns>
        /// Plaintext UTF-8 message
        /// </returns>
        public static string Decrypt(string encrypted, string key, string nonce)
        {
            // Validate parameters
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (string.IsNullOrEmpty(encrypted))
            {
                throw new ArgumentNullException(nameof(encrypted));
            }
            if (string.IsNullOrEmpty(nonce))
            {
                throw new ArgumentNullException(nameof(nonce));
            }
            // Check nonce is Base64
            if (!Utils.IsBase64(nonce))
            {
                throw new ArgumentException($"{nameof(nonce)} must be base64-encoded string");
            }
            byte[] nonce_bytes = Convert.FromBase64String(nonce);
            // Convert key to bytes
            byte[] key_bytes;
            if (Utils.IsBase64(key))
            {
                // Key is Base64, convert to raw bytes
                key_bytes = Convert.FromBase64String(key);
            }
            else
            {
                // Key is plaintext string, fallback to raw ASCII bytes
                key_bytes = Encoding.ASCII.GetBytes(key);
            }
            // Check if encrypted is Base64
            if (!Utils.IsBase64(encrypted))
            {
                throw new ArgumentException($"{nameof(encrypted)} must be base64-encoded string");
            }
            byte[] encrypted_bytes = Convert.FromBase64String(encrypted);
            // Decrypt the message
            var raw_msg = StreamEncryption.DecryptChaCha20(encrypted_bytes, nonce_bytes, key_bytes);

            return(Encoding.UTF8.GetString(raw_msg));
        }
示例#9
0
        public void TestChaCha20()
        {
            int x = NativeLibsodium.sodium_init();

            Assert.True(x == 0 || x == 1);

            const string MESSAGE = "Test message to encrypt";

            byte[] nonce = StreamEncryption.GenerateNonceChaCha20();
            byte[] key   = StreamEncryption.GenerateKey();

            //encrypt it
            byte[] encrypted = StreamEncryption.EncryptChaCha20(MESSAGE, nonce, key);


            //decrypt it
            byte[] decrypted = StreamEncryption.DecryptChaCha20(encrypted, nonce, key);

            Assert.AreEqual(MESSAGE, decrypted);
        }
示例#10
0
    private void Start()
    {
        int x = NativeLibsodium.sodium_init();

        Debug.Log(x);


        const string MESSAGE = "Test message to encrypt";

        byte[] nonce = StreamEncryption.GenerateNonceChaCha20();
        byte[] key   = StreamEncryption.GenerateKey();

        //encrypt it
        byte[] encrypted = StreamEncryption.EncryptChaCha20(MESSAGE, nonce, key);


        //decrypt it
        byte[] decrypted = StreamEncryption.DecryptChaCha20(encrypted, nonce, key);

        Debug.Log(MESSAGE);
        Debug.Log(Encoding.UTF8.GetString(encrypted));
        Debug.Log(Encoding.UTF8.GetString(decrypted));
    }
 public byte[] Decrypt(byte[] encryptedStr, long nonce)
 {
     byte[] decryptedVar = new byte[] {};
     decryptedVar = StreamEncryption.DecryptChaCha20(encryptedStr, BitConverter.GetBytes(nonce), Convert.FromBase64String(ChaChaEncryptionKey));
     return(decryptedVar);
 }
 public byte[] Decrypt(byte[] cipherText, long nonce)
 {
     return(StreamEncryption.DecryptChaCha20(cipherText, GetNonceBytes(nonce), _crypteKey));
 }
    public byte[] Decrypt(byte[] encrypted, long nonce)
    {
        var nonceByte = BitConverter.GetBytes(nonce);

        return(StreamEncryption.DecryptChaCha20(encrypted, nonceByte, ChaChaEncryptionKey));
    }
 public byte[] Decrypt_Fake(byte[] message, long tmpNonce)
 {
     return(StreamEncryption.DecryptChaCha20(message, BitConverter.GetBytes(tmpNonce), Convert.FromBase64String(ChaChaEncryptionKeyFake)));
 }
示例#15
0
        public void RoundTripCheck(IHashProvider hashProvider, IBlockObjectTypeProvider typeProvider)
        {
            // Serialize a first time
            var firstMemoryStream     = new MemoryStream();
            var firstSerializeContext = new BlockSerializeContext(new BinaryWriter(firstMemoryStream), typeProvider);

            byte[] nonce;
            if (typeProvider.SecretKey != null)
            {
                nonce = StreamEncryption.GenerateNonceChaCha20();
                firstSerializeContext.bw.WriteBuffer(nonce);
                using (var ems = new MemoryStream())
                {
                    using (var ebw = new BinaryWriter(ems))
                    {
                        var ec = new BlockSerializeContext(ebw, typeProvider, firstSerializeContext.Version);
                        Serialize(ec);
                        firstSerializeContext.bw.WriteBuffer(StreamEncryption.EncryptChaCha20(ems.ToArray(), nonce, ec.typeProvider.SecretKey));
                    }
                }
            }
            else
            {
                firstSerializeContext.bw.Write(false);
                Serialize(firstSerializeContext);
            }

            byte[] originalData = firstMemoryStream.ToArray();

            // Then deserialize that data
            {
                var br = new BinaryReader(new MemoryStream(originalData));
                var deserializeContext = new BlockDeserializeContext(br, typeProvider);
                nonce = deserializeContext.br.ReadBuffer();

                Block deserialized;
                if (nonce != null)
                {
                    using (var dms = new MemoryStream(StreamEncryption.DecryptChaCha20(deserializeContext.br.ReadBuffer(), nonce, typeProvider.SecretKey)))
                    {
                        using (var dbr = new BinaryReader(dms))
                        {
                            var dc = new BlockDeserializeContext(dbr, typeProvider);
                            deserialized = new Block(dc);
                        }
                    }
                }
                else
                {
                    deserialized = new Block(deserializeContext);
                }

                // Then serialize that deserialized data and see if it matches
                {
                    var secondMemoryStream     = new MemoryCompareStream(originalData);
                    var secondSerializeContext = new BlockSerializeContext(new BinaryWriter(secondMemoryStream), typeProvider);
                    if (typeProvider.SecretKey != null)
                    {
                        secondSerializeContext.bw.WriteBuffer(nonce);
                        using (var ems = new MemoryStream())
                        {
                            using (var ebw = new BinaryWriter(ems))
                            {
                                var ec = new BlockSerializeContext(ebw, typeProvider, secondSerializeContext.Version);
                                deserialized.Serialize(ec);
                                secondSerializeContext.bw.WriteBuffer(StreamEncryption.EncryptChaCha20(ems.ToArray(), nonce, ec.typeProvider.SecretKey));
                            }
                        }
                    }
                    else
                    {
                        secondSerializeContext.bw.Write(false);
                        deserialized.Serialize(secondSerializeContext);
                    }
                }
            }
        }
示例#16
0
    public byte[] Decrypt(byte[] message, long nonce)
    {
        var byteNonce = BitConverter.GetBytes(nonce);

        return(StreamEncryption.DecryptChaCha20(message, byteNonce, ChaChaEncryptionKey));
    }