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 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 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 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); }
private byte[] SerializeObjects(Block block) { byte[] data; using (var ms = new MemoryStream()) { using (var bw = new BinaryWriter(ms, Encoding.UTF8)) { // Version: var context = new BlockSerializeContext(bw, _typeProvider); if (context.typeProvider.SecretKey != null) { // Nonce: var nonce = StreamEncryption.GenerateNonceChaCha20(); context.bw.WriteBuffer(nonce); // Data: using (var ems = new MemoryStream()) { using (var ebw = new BinaryWriter(ems, Encoding.UTF8)) { var ec = new BlockSerializeContext(ebw, _typeProvider, context.Version); block.SerializeObjects(ec); context.bw.WriteBuffer(StreamEncryption.EncryptChaCha20(ems.ToArray(), nonce, ec.typeProvider.SecretKey)); } } } else { // Data: context.bw.Write(false); block.SerializeObjects(context); } data = ms.ToArray(); } } return(data); }
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 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); } } } }
public void GenerateNonceChaCha20Test() { Assert.AreEqual(8, StreamEncryption.GenerateNonceChaCha20().Length); }
/// <summary> /// Generate a secure nonce /// </summary> /// <returns> /// Base64-encoded nonce /// </returns> public static string GenerateNonce() { return(Convert.ToBase64String(StreamEncryption.GenerateNonceChaCha20())); }