public static byte[] Decrypt(byte[] CipherText, byte[] Nonce, byte[] PublicKey, byte[] SecretKey)
        {
            var PlainText = new byte[CipherText.Length - 16];

            var Result = SodiumLibrary.crypto_box_open_easy(PlainText, CipherText, CipherText.Length, Nonce, PublicKey, SecretKey);

            return(Result == 0 ? PlainText : throw new CryptographicException());
        }
Esempio n. 2
0
        /// <summary>
        /// This function can be used to compute a shared secret given a user's secret key and another user's public key.
        /// </summary>
        /// <see cref="https://libsodium.gitbook.io/doc/advanced/scalar_multiplication#usage"/>
        public static byte[] ScalarMultiplication(byte[] SecretKey, byte[] PublicKey)
        {
            var SharedKey = new byte[SecretKey.Length];

            var Result = SodiumLibrary.crypto_scalarmult(SharedKey, SecretKey, PublicKey);

            return(Result == 0 ? SharedKey : throw new CryptographicException());
        }
Esempio n. 3
0
        public static byte[] KeyExchange(byte[] PublicKey, byte[] SecretKey)
        {
            var SharedKey = new byte[32];

            var Result = SodiumLibrary.crypto_box_beforenm(SharedKey, PublicKey, SecretKey);

            return(Result == 0 ? SharedKey : throw new CryptographicException());
        }
        public static byte[] Decrypt(byte[] CipherText, byte[] Nonce, byte[] SharedKey)
        {
            var PlainText = new byte[CipherText.Length - 16];

            var Result = SodiumLibrary.crypto_box_curve25519xchacha20poly1305_open_easy_afternm(PlainText, CipherText, CipherText.Length, Nonce, SharedKey);

            return(Result == 0 ? PlainText : throw new CryptographicException());
        }
        public static byte[] Encrypt(byte[] Message, byte[] Nonce, byte[] PublicKey, byte[] SecretKey)
        {
            var CipherText = new byte[Message.Length + 16];

            var Result = SodiumLibrary.crypto_box_curve25519xchacha20poly1305_easy(CipherText, Message, Message.Length, Nonce, PublicKey, SecretKey);

            return(Result == 0 ? CipherText : throw new CryptographicException());
        }
        public static byte[] Encrypt(byte[] Message, byte[] Nonce, byte[] SharedKey)
        {
            var CipherText = new byte[Message.Length + 16];

            var Result =
                SodiumLibrary.crypto_box_easy_afternm(CipherText, Message, Message.Length,
                                                      Nonce, SharedKey);

            return(Result == 0 ? CipherText : throw new CryptographicException());
        }
Esempio n. 7
0
        public static byte[] Hash(byte[] Data, byte[] Key, byte[] Nonce)
        {
            if (Key.Length < 32)
            {
                throw new ArgumentException("Key Length Must Be 32 Bytes.", nameof(Key));
            }

            if (Nonce.Length < 16)
            {
                throw new ArgumentException("Nonce Length Must Be 16 Bytes.", nameof(Nonce));
            }

            var Hash = new byte[Data.Length];

            var Result = SodiumLibrary.crypto_core_hchacha20(Hash, Data, Key, Nonce);

            return(Result == 0 ? Hash : throw new CryptographicException());
        }