示例#1
0
        /**
         * Encrypts clearBytes and prints encrypted data (base64 encoded)
         * @param byte[] clearBytes : Clear data to encrypt
         * @param bool base64 : Tells if clearBytes contains base64-encoded data (true) or raw data (false)
         */
        static void Encrypt(byte[] clearBytes, bool base64)
        {
            // base64 decode clearBytes
            if (base64)
            {
                string base64string = Encoding.UTF8.GetString(clearBytes);
                try
                {
                    clearBytes = Convert.FromBase64String(base64string);
                }
                catch (FormatException)
                {
                    Console.Error.WriteLine("Input data cannot be read as base64");
                    return;
                }
            }

            // Encryption
            byte[] entropy   = null;
            byte[] encrypted = DPAPI.Encrypt(DPAPI.KeyType.UserKey, clearBytes, entropy, "Encrypted with Windows DPAPI through dpapibridge");

            // Print result
            string encryptedBase64 = Convert.ToBase64String(encrypted);

            Console.Out.WriteLine(encryptedBase64);
        }
示例#2
0
        /**
         * Decrypts encryptedBytes and prints clear data
         * @param byte[] encryptedBytes : Encrypted data to decrypt, base64-encoded
         * @param bool base64 : Encode output as base64 if true (useful when clear data contains non ASCII bytes)
         * @param string outputFile : File path to send output
         */
        static void Decrypt(byte[] encryptedBytes, bool base64, string outputFile)
        {
            // base64 decode encryptedBytes
            string base64string = Encoding.UTF8.GetString(encryptedBytes);

            try
            {
                encryptedBytes = Convert.FromBase64String(base64string);
            }
            catch (FormatException)
            {
                Console.Error.WriteLine("Cannot base64-decode input");
                return;
            }


            // Decryption
            byte[] entropy = null;
            string description;

            byte[] decrypted;
            try
            {
                decrypted = DPAPI.Decrypt(encryptedBytes, entropy, out description);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                return;
            }

            // Save output to file
            string output;

            if (outputFile != null)
            {
                if (base64)
                {
                    output    = Convert.ToBase64String(decrypted);
                    decrypted = Encoding.UTF8.GetBytes(output);
                }
                File.WriteAllBytes(outputFile, decrypted);
                Console.WriteLine("output saved to" + outputFile);
                return;
            }

            // Print result
            if (base64)
            {
                output = Convert.ToBase64String(decrypted);
            }
            else
            {
                output = Encoding.UTF8.GetString(decrypted);
            }
            Console.Out.WriteLine(output);
        }