コード例 #1
0
        /// <summary>
        /// Decrypt the specified ciphertext, with key and nonce.
        /// Uses  xchacha20poly1305_ietf_decrypt
        /// </summary>
        /// <returns>The decrypt.</returns>
        /// <param name="ciphertext">Ciphertext.</param>
        /// <param name="key">Key.</param>
        /// <param name="nonce">Nonce.</param>
        public override byte[] Decrypt(byte[] ciphertext, byte[] key, byte[] nonce = null)
        {
            byte[] message;
            if (nonce == null)
            {
                nonce      = GetNonce(ciphertext);
                ciphertext = GetCipherText(ciphertext);
            }

            message = new byte[ciphertext.Length];
            long messageLength = ciphertext.Length;

            int result = NativeLibsodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
                message,
                out messageLength,
                null,
                ciphertext,
                ciphertext.Length,
                null, 0,
                nonce,
                key);

            if (result != 0)
            {
                throw new CoflnetException("decryption_error", "Decryption error");
            }

            return(message);
        }
コード例 #2
0
ファイル: Encrypt.cs プロジェクト: Coflnet/cloud
        public static byte[] DecryptData(byte[] ciphertextWithNonce, byte[] key)
        {
            long messageLength = ciphertextWithNonce.Length - X_NONC_ESIZE;

            byte[] message    = new byte[messageLength];
            byte[] nonce      = GetNonce(ciphertextWithNonce);
            byte[] cipherText = GetCipherText(ciphertextWithNonce);
            int    result     = NativeLibsodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
                message,
                out messageLength,
                null,
                cipherText,
                cipherText.Length,
                null, 0,
                nonce,
                key);

            if (result != 0)
            {
                throw new DecrytpionError();
            }

            return(message);
        }
コード例 #3
0
ファイル: EndToEndEncrypt.cs プロジェクト: Coflnet/cloud
        public static byte[] DecryptData(byte[] ciphertextWithNonce, byte[] key)
        {
            long messageLength = ciphertextWithNonce.Length - X_NONC_ESIZE;

            byte[] message    = new byte[messageLength];
            byte[] nonce      = ReadBytes(ciphertextWithNonce, 0, X_NONC_ESIZE);
            byte[] cipherText = ReadBytes(ciphertextWithNonce, X_NONC_ESIZE, ciphertextWithNonce.Length - X_NONC_ESIZE);
            int    result     = NativeLibsodium.crypto_aead_xchacha20poly1305_ietf_decrypt(
                message,
                out messageLength,
                null,
                cipherText,
                cipherText.Length,
                null, 0,
                nonce,
                key);

            if (result != 0)
            {
                throw new Exception("Decryption error");
            }

            return(message);
        }