예제 #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
        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);
        }