• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# DATA_BLOB类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中DATA_BLOB的典型用法代码示例。如果您正苦于以下问题:C# DATA_BLOB类的具体用法?C# DATA_BLOB怎么用?C# DATA_BLOB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



DATA_BLOB类属于命名空间,在下文中一共展示了DATA_BLOB类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Encrypt

        public sealed override byte[] Encrypt(CmsRecipientCollection recipients, ContentInfo contentInfo, AlgorithmIdentifier contentEncryptionAlgorithm, X509Certificate2Collection originatorCerts, CryptographicAttributeObjectCollection unprotectedAttributes)
        {
            using (SafeCryptMsgHandle hCryptMsg = EncodeHelpers.CreateCryptMsgHandleToEncode(recipients, contentInfo.ContentType, contentEncryptionAlgorithm, originatorCerts, unprotectedAttributes))
            {
                byte[] encodedContent;
                if (contentInfo.ContentType.Value.Equals(Oids.Pkcs7Data, StringComparison.OrdinalIgnoreCase))
                {
                    unsafe
                    {
                        byte[] content = contentInfo.Content;
                        fixed (byte* pContent = content)
                        {
                            DATA_BLOB blob = new DATA_BLOB((IntPtr)pContent, (uint)(content.Length));
                            encodedContent = Interop.Crypt32.CryptEncodeObjectToByteArray(CryptDecodeObjectStructType.X509_OCTET_STRING, &blob);
                        }
                    }
                }
                else
                {
                    encodedContent = contentInfo.Content;
                }

                if (encodedContent.Length > 0)
                {
                    if (!Interop.Crypt32.CryptMsgUpdate(hCryptMsg, encodedContent, encodedContent.Length, fFinal: true))
                        throw Marshal.GetLastWin32Error().ToCryptographicException();
                }

                byte[] encodedMessage = hCryptMsg.GetMsgParamAsByteArray(CryptMsgParamType.CMSG_CONTENT_PARAM);
                return encodedMessage;
            }
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:32,代码来源:PkcsPalWindows.Encrypt.cs


示例2: CryptUnprotectData

 bool CryptUnprotectData(ref DATA_BLOB pCipherText,
                         ref string pszDescription,
                         ref DATA_BLOB pEntropy,
                             IntPtr pReserved,
                         ref CRYPTPROTECT_PROMPTSTRUCT pPrompt,
                             int dwFlags,
                         ref DATA_BLOB pPlainText);
开发者ID:philippthiele,项目名称:ChromePasswordDump,代码行数:7,代码来源:DPAPI.cs


示例3: CryptProtectData

        internal static unsafe bool CryptProtectData(SafeBSTRHandle uncryptedBuffer, out SafeBSTRHandle cryptedBuffer)
        {
            byte* uncryptedBufferPtr = null;
            DATA_BLOB pDataOut = default(DATA_BLOB);
            try
            {
                uncryptedBuffer.AcquirePointer(ref uncryptedBufferPtr);
                DATA_BLOB pDataIn = new DATA_BLOB((IntPtr)uncryptedBufferPtr, uncryptedBuffer.Length * 2);
                if (CryptProtectData(new IntPtr(&pDataIn), String.Empty, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, CRYPTPROTECTMEMORY_SAME_PROCESS, new IntPtr(&pDataOut)))
                {
                    SafeBSTRHandle newHandle = SafeBSTRHandle.Allocate(pDataOut.pbData, pDataOut.cbData);
                    cryptedBuffer = newHandle;
                    return true;
                }
                else
                {
                    cryptedBuffer = SafeBSTRHandle.Allocate(null, 0);
                    return false;
                }
            }
            finally
            {
                if (uncryptedBufferPtr != null)
                    uncryptedBuffer.ReleasePointer();

                if (pDataOut.pbData != IntPtr.Zero)
                {
                    NtDll.ZeroMemory(pDataOut.pbData, (UIntPtr)pDataOut.cbData);
                    Marshal.FreeHGlobal(pDataOut.pbData);
                }
            }
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:32,代码来源:Interop.CryptProtectData.cs


示例4: CryptProtectData

 private static extern bool CryptProtectData(
     ref DATA_BLOB pPlainText,
     [MarshalAs(UnmanagedType.LPWStr)]string szDescription,
     IntPtr pEntroy,
     IntPtr pReserved,
     IntPtr pPrompt,
     int dwFlags,
     ref DATA_BLOB pCipherText);
开发者ID:trendSWM,项目名称:rdesktopce_rdp5,代码行数:8,代码来源:rdp_password.cs


示例5: InitBLOB

    private static void InitBLOB(byte[] data, ref DATA_BLOB blob)
    {
        blob.pbData = Marshal.AllocHGlobal(data.Length);
        if (blob.pbData == IntPtr.Zero) throw new Exception("Unable to allocate buffer for BLOB data.");

        blob.cbData = data.Length;
        Marshal.Copy(data, 0, blob.pbData, data.Length);
    }
开发者ID:denistorresan,项目名称:AutoPuTTY,代码行数:8,代码来源:cryptDPAPI.cs


示例6: CryptUnprotectData

 private static extern bool CryptUnprotectData(
                                   ref DATA_BLOB pDataIn,
                                   String szDataDescr,
                                   ref DATA_BLOB pOptionalEntropy,
                                   IntPtr pvReserved,
                                   ref CRYPTPROTECT_PROMPTSTRUCT
                                     pPromptStruct,
                                   int dwFlags,
                                   ref DATA_BLOB pDataOut);
开发者ID:Nullstr1ng,项目名称:MultiRDPClient.NET,代码行数:9,代码来源:DataProtector.cs


示例7: EncodeOctetString

 public sealed override byte[] EncodeOctetString(byte[] octets)
 {
     unsafe
     {
         fixed (byte* pOctets = octets)
         {
             DATA_BLOB blob = new DATA_BLOB((IntPtr)pOctets, (uint)(octets.Length));
             return Interop.Crypt32.CryptEncodeObjectToByteArray(CryptDecodeObjectStructType.X509_OCTET_STRING, &blob);
         }
     }
 }
开发者ID:ESgarbi,项目名称:corefx,代码行数:11,代码来源:PkcsPalWindows.cs


示例8: Encrypt

    public static string Encrypt(string plainText)
    {
        byte[] plainTextBytes = Encoding.Unicode.GetBytes(plainText);
        DATA_BLOB plainTextBlob = new DATA_BLOB();
        DATA_BLOB cipherTextBlob = new DATA_BLOB();
        StringBuilder cipherString = new StringBuilder();
        try
        {
            try
            {
                InitBLOB(plainTextBytes, ref plainTextBlob);
            }
            catch (Exception ex)
            {
                throw new Exception("Cannot initialize dataIn BLOB.", ex);
            }

            bool success = CryptProtectData(
            ref plainTextBlob,
            "psw",
            NullPtr,
            NullPtr,
            NullPtr,
            CRYPTPROTECT_UI_FORBIDDEN,
            ref cipherTextBlob);

            if (!success)
            {
                int errCode = Marshal.GetLastWin32Error();
                throw new Exception("CryptProtectData failed.", new Win32Exception(errCode));
            }

            byte[] cipherTextBytes = new byte[cipherTextBlob.cbData];
            Marshal.Copy(cipherTextBlob.pbData, cipherTextBytes, 0, cipherTextBlob.cbData);
            // Convert hex data to hex characters (suitable for a string)
            for (int i = 0; i < cipherTextBlob.cbData; i++) cipherString.Append(Convert.ToString(cipherTextBytes[i], 16).PadLeft(2, '0').ToUpper());
        }
        catch (Exception ex)
        {
            throw new Exception("unable to encrypt data.", ex);
        }
        finally
        {
            if (plainTextBlob.pbData != IntPtr.Zero) Marshal.FreeHGlobal(plainTextBlob.pbData);

            if (cipherTextBlob.pbData != IntPtr.Zero) Marshal.FreeHGlobal(cipherTextBlob.pbData);
        }
        return cipherString.ToString();
    }
开发者ID:denistorresan,项目名称:AutoPuTTY,代码行数:49,代码来源:cryptDPAPI.cs


示例9: Decrypt

 public static byte[] Decrypt(byte[] cipherTextBytes, byte[] entropyBytes, out string description)
 {
     DATA_BLOB pPlainText = new DATA_BLOB();
     DATA_BLOB dataBlob1 = new DATA_BLOB();
     DATA_BLOB dataBlob2 = new DATA_BLOB();
     CRYPTPROTECT_PROMPTSTRUCT cryptprotectPromptstruct = new CRYPTPROTECT_PROMPTSTRUCT();
     DataProtection.InitPrompt(ref cryptprotectPromptstruct);
     description = string.Empty;
     try
     {
         try
         {
             DataProtection.InitBLOB(cipherTextBytes, ref dataBlob1);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize ciphertext BLOB.", ex);
         }
         try
         {
             DataProtection.InitBLOB(entropyBytes, ref dataBlob2);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize entropy BLOB.", ex);
         }
         int dwFlags = 1;
         if (!Advent.Common.Interop.NativeMethods.CryptUnprotectData(ref dataBlob1, ref description, ref dataBlob2, IntPtr.Zero, ref cryptprotectPromptstruct, dwFlags, ref pPlainText))
             throw new Exception("CryptUnprotectData failed.", (Exception)new Win32Exception(Marshal.GetLastWin32Error()));
         byte[] destination = new byte[pPlainText.cbData];
         Marshal.Copy(pPlainText.pbData, destination, 0, pPlainText.cbData);
         return destination;
     }
     catch (Exception ex)
     {
         throw new Exception("Unable to decrypt data.", ex);
     }
     finally
     {
         if (pPlainText.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(pPlainText.pbData);
         if (dataBlob1.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(dataBlob1.pbData);
         if (dataBlob2.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(dataBlob2.pbData);
     }
 }
开发者ID:kingpin2k,项目名称:MCS,代码行数:47,代码来源:DataProtection.cs


示例10: Encrypt

        public static string Encrypt(string unencrypted)
        {
            CryptProtectFlags flags = CryptProtectFlags.CRYPTPROTECT_UI_FORBIDDEN;
            DATA_BLOB unencryptedBlob = ConvertData(Encoding.Unicode.GetBytes(unencrypted));
            DATA_BLOB encryptedBlob = new DATA_BLOB();
            DATA_BLOB dataOption = new DATA_BLOB();

            try
            {
                CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
                if (!CryptProtectData(ref unencryptedBlob, "psw", ref dataOption, IntPtr.Zero, ref prompt, flags, ref encryptedBlob))
                {
                    int errCode = Marshal.GetLastWin32Error();
                    throw new AmazonClientException("CryptProtectData failed. Error Code: " + errCode);
                }

                byte[] outData = new byte[encryptedBlob.cbData];
                Marshal.Copy(encryptedBlob.pbData, outData, 0, outData.Length);


                StringBuilder encrypted = new StringBuilder();
                for (int i = 0; i <= outData.Length - 1; i++)
                {
                    encrypted.Append(
                        Convert.ToString(outData[i], 16).PadLeft(2, '0').ToUpper(CultureInfo.InvariantCulture));
                }

                string encryptedPassword = encrypted.ToString().ToUpper(CultureInfo.InvariantCulture);
                return encryptedPassword;
            }
            finally
            {
                if (unencryptedBlob.pbData != IntPtr.Zero)
                    Marshal.FreeHGlobal(unencryptedBlob.pbData);
                if (encryptedBlob.pbData != IntPtr.Zero)
                    Marshal.FreeHGlobal(encryptedBlob.pbData);
            }
        }
开发者ID:aws,项目名称:aws-sdk-net,代码行数:38,代码来源:UserCrypto.cs


示例11: Decrypt

        public static string Decrypt(string encrypted)
        {
            List<Byte> dataIn = new List<byte>();
            for (int i = 0; i < encrypted.Length; i = i + 2)
            {
                byte data = Convert.ToByte(encrypted.Substring(i, 2), 16);
                dataIn.Add(data);
            }

            CryptProtectFlags flags = CryptProtectFlags.CRYPTPROTECT_UI_FORBIDDEN;
            DATA_BLOB encryptedBlob = ConvertData(dataIn.ToArray());
            DATA_BLOB unencryptedBlob = new DATA_BLOB();
            DATA_BLOB dataOption = new DATA_BLOB();

            try
            {

                CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
                if (!CryptUnprotectData(ref encryptedBlob, "psw", ref dataOption, IntPtr.Zero, ref prompt, flags, ref unencryptedBlob))
                {
                    int errCode = Marshal.GetLastWin32Error();
                    throw new AmazonClientException("CryptProtectData failed. Error Code: " + errCode);
                }

                byte[] outData = new byte[unencryptedBlob.cbData];
                Marshal.Copy(unencryptedBlob.pbData, outData, 0, outData.Length);

                string unencrypted = Encoding.Unicode.GetString(outData);
                return unencrypted;
            }
            finally
            {
                if (encryptedBlob.pbData != IntPtr.Zero)
                    Marshal.FreeHGlobal(encryptedBlob.pbData);
                if (unencryptedBlob.pbData != IntPtr.Zero)
                    Marshal.FreeHGlobal(unencryptedBlob.pbData);
            }
        }
开发者ID:aws,项目名称:aws-sdk-net,代码行数:38,代码来源:UserCrypto.cs


示例12: decrypt

 public static byte[] decrypt(byte[] cipherTextBytes)
 {
     try
     {
         DATA_BLOB plainTextBlob = new DATA_BLOB();
         DATA_BLOB cipherTextBlob = new DATA_BLOB();
         DATA_BLOB entropyBlob = new DATA_BLOB();
         CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
         string description = String.Empty;
         InitPrompt(ref prompt);
         try
         {
             InitBLOB(cipherTextBytes, ref cipherTextBlob);
         }
         catch { }
         int flags = 0x1;
         bool success = CryptUnprotectData(ref cipherTextBlob,
                                           ref description,
                                           ref entropyBlob,
                                               IntPtr.Zero,
                                           ref prompt,
                                               flags,
                                           ref plainTextBlob);
         if (success)
         {
             byte[] plainTextBytes = new byte[plainTextBlob.cbData];
             Marshal.Copy(plainTextBlob.pbData,
                      plainTextBytes,
                      0,
                      plainTextBlob.cbData);
             return plainTextBytes;
         }
     }
     catch { }
     return null;
 }
开发者ID:jimbojetset,项目名称:GetChromePasswords,代码行数:36,代码来源:DPAPI.cs


示例13: Decrypt

        /// <summary>
        /// Decrypt byte data
        /// </summary>
        /// <param name="cipherText">Data to be decoded</param>
        /// <param name="optionalEntropy">Additional entropy, recommended for machine-specific case</param>
        /// <returns>Returns a byte array with the encoded data</returns>
        internal byte[] Decrypt(byte[] cipherText, byte[] optionalEntropy)
        {
            bool retVal = false;

            DATA_BLOB plainTextBlob = new DATA_BLOB();
            DATA_BLOB cipherBlob = new DATA_BLOB();
            CRYPTPROTECT_PROMPTSTRUCT prompt = new CRYPTPROTECT_PROMPTSTRUCT();
            InitPromptstruct(ref prompt);

            try
            {
                try
                {
                    int cipherTextSize = cipherText.Length;
                    cipherBlob.pbData = Marshal.AllocHGlobal(cipherTextSize);
                    if(IntPtr.Zero == cipherBlob.pbData)
                    {
                        throw new Exception("Unable to allocate cipherText buffer.");
                    }
                    cipherBlob.cbData = cipherTextSize;
                    Marshal.Copy(cipherText, 0, cipherBlob.pbData,
                        cipherBlob.cbData);
                }
                catch(Exception ex)
                {
                    throw new Exception("Exception marshalling data. " +
                        ex.Message);
                }
                DATA_BLOB entropyBlob = new DATA_BLOB();
                int dwFlags;
                if(Store.USE_MACHINE_STORE == store)
                {
                    //Using the machine store, should be providing entropy.
                    dwFlags =
                        CRYPTPROTECT_LOCAL_MACHINE|CRYPTPROTECT_UI_FORBIDDEN;
                    //Check to see if the entropy is null
                    if(null == optionalEntropy)
                    {
                        //Allocate something
                        optionalEntropy = new byte[0];
                    }
                    try
                    {
                        int bytesSize = optionalEntropy.Length;
                        entropyBlob.pbData = Marshal.AllocHGlobal(bytesSize);
                        if(IntPtr.Zero == entropyBlob.pbData)
                        {
                            throw new Exception("Unable to allocate entropy buffer.");
                        }
                        entropyBlob.cbData = bytesSize;
                        Marshal.Copy(optionalEntropy, 0, entropyBlob.pbData,
                            bytesSize);
                    }
                    catch(Exception ex)
                    {
                        throw new Exception("Exception marshalling entropy data. " +
                            ex.Message);
                    }
                }
                else
                {
                    //Using the user store
                    dwFlags = CRYPTPROTECT_UI_FORBIDDEN;
                }
                retVal = CryptUnprotectData(ref cipherBlob, null, ref
                    entropyBlob,
                    IntPtr.Zero, ref prompt, dwFlags,
                    ref plainTextBlob);
                if(false == retVal)
                {
                    throw new Exception("Decryption failed. " +
                        Win32Message.GetMessage(Marshal.GetLastWin32Error()));
                }
                //Free the blob and entropy.
                if(IntPtr.Zero != cipherBlob.pbData)
                {
                    Marshal.FreeHGlobal(cipherBlob.pbData);
                }
                if(IntPtr.Zero != entropyBlob.pbData)
                {
                    Marshal.FreeHGlobal(entropyBlob.pbData);
                }
            }
            catch(Exception ex)
            {
                throw new Exception("Exception decrypting. " + ex.Message);
            }
            byte[] plainText = new byte[plainTextBlob.cbData];
            Marshal.Copy(plainTextBlob.pbData, plainText, 0, plainTextBlob.cbData);
            Marshal.FreeHGlobal(plainTextBlob.pbData);
            return plainText;
        }
开发者ID:paladin74,项目名称:Dapple,代码行数:98,代码来源:DataProtection.cs


示例14: DecryptProtectedData

        /// <summary>
        /// CryptUnprotectDataで暗号化されたデータを復号化します。
        /// </summary>
        /// <param name="encryptedData">暗号化されたデータ</param>
        /// <returns>復号化されたデータ</returns>
        public static byte[] DecryptProtectedData(byte[] encryptedData)
        {
            //リソース確保
            var input = new DATA_BLOB();
            var output = new DATA_BLOB();
            try
            {
                input.pbData = Marshal.AllocHGlobal(encryptedData.Length);
                input.cbData = (uint)encryptedData.Length;
                Marshal.Copy(encryptedData, 0, input.pbData, encryptedData.Length);

                //復号化
                var dammy = new DATA_BLOB();
                var isSucc = Win32Api.CryptUnprotectData(ref input, null, ref dammy, IntPtr.Zero, IntPtr.Zero, 0, ref output);
                if (isSucc == false)
                {
                    Trace.TraceError("SnkLib.App.CookieGetter.dll:\r\n"
                        + "DecryptProtectedData()でエラーが発生しました。データ復号化で予期せぬ失敗が発生しています。\r\n"
                        + "output.cbData: " + output.cbData);
                    return null;
                }

                var decryptedBytes = new byte[output.cbData];
                Marshal.Copy(output.pbData, decryptedBytes, 0, (int)output.cbData);
                return decryptedBytes;
            }
            catch (DllNotFoundException e)
            {
                Trace.TraceError("SnkLib.App.CookieGetter.dll:\r\n"
                    + "DecryptProtectedData()でエラーが発生しました。Win32API呼び出しで対象のdllが存在しませんでした。\r\n"
                    + e.ToString());
                return null;
            }
            finally
            {
                if (input.pbData != null)
                    Marshal.FreeHGlobal(input.pbData);
                if (output.pbData != null)
                    Marshal.FreeHGlobal(output.pbData);
            }
        }
开发者ID:rokugasenpai,项目名称:SnkLib.App.CookieGetter,代码行数:46,代码来源:Utility.cs


示例15: ConvertData

        static DATA_BLOB ConvertData(byte[] data)
        {
            DATA_BLOB blob = new DATA_BLOB();
            blob.pbData = Marshal.AllocHGlobal(data.Length);
            blob.cbData = data.Length;
            Marshal.Copy(data, 0, blob.pbData, data.Length);

            return blob;
        }
开发者ID:aws,项目名称:aws-sdk-net,代码行数:9,代码来源:UserCrypto.cs


示例16: Encrypt

 public static byte[] Encrypt(DataProtection.KeyType keyType, byte[] plainTextBytes, byte[] entropyBytes, string description)
 {
     if (plainTextBytes == null)
         plainTextBytes = new byte[0];
     if (entropyBytes == null)
         entropyBytes = new byte[0];
     if (description == null)
         description = string.Empty;
     DATA_BLOB dataBlob1 = new DATA_BLOB();
     DATA_BLOB pCipherText = new DATA_BLOB();
     DATA_BLOB dataBlob2 = new DATA_BLOB();
     CRYPTPROTECT_PROMPTSTRUCT cryptprotectPromptstruct = new CRYPTPROTECT_PROMPTSTRUCT();
     DataProtection.InitPrompt(ref cryptprotectPromptstruct);
     try
     {
         try
         {
             DataProtection.InitBLOB(plainTextBytes, ref dataBlob1);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize plaintext BLOB.", ex);
         }
         try
         {
             DataProtection.InitBLOB(entropyBytes, ref dataBlob2);
         }
         catch (Exception ex)
         {
             throw new Exception("Cannot initialize entropy BLOB.", ex);
         }
         int dwFlags = 1;
         if (keyType == DataProtection.KeyType.MachineKey)
             dwFlags |= 4;
         if (!Advent.Common.Interop.NativeMethods.CryptProtectData(ref dataBlob1, description, ref dataBlob2, IntPtr.Zero, ref cryptprotectPromptstruct, dwFlags, ref pCipherText))
             throw new Exception("CryptProtectData failed.", (Exception)new Win32Exception(Marshal.GetLastWin32Error()));
         byte[] destination = new byte[pCipherText.cbData];
         Marshal.Copy(pCipherText.pbData, destination, 0, pCipherText.cbData);
         return destination;
     }
     catch (Exception ex)
     {
         throw new Exception("DPAPI was unable to encrypt data.", ex);
     }
     finally
     {
         if (dataBlob1.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(dataBlob1.pbData);
         if (pCipherText.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(pCipherText.pbData);
         if (dataBlob2.pbData != IntPtr.Zero)
             Marshal.FreeHGlobal(dataBlob2.pbData);
     }
 }
开发者ID:kingpin2k,项目名称:MCS,代码行数:54,代码来源:DataProtection.cs


示例17: Encrypt

    public static byte[] Encrypt(KeyType keyType, byte[] plainTextBytes, byte[] entropyBytes, string description)
    {
        // Make sure that parameters are valid.
        if (plainTextBytes == null) plainTextBytes = new byte[0];
        if (entropyBytes == null) entropyBytes = new byte[0];
        if (description == null) description = String.Empty;

        // Create BLOBs to hold data.
        DATA_BLOB plainTextBlob = new DATA_BLOB();
        DATA_BLOB cipherTextBlob = new DATA_BLOB();
        DATA_BLOB entropyBlob = new DATA_BLOB();

        // We only need prompt structure because it is a required
        // parameter.
        CRYPTPROTECT_PROMPTSTRUCT prompt =
                                  new CRYPTPROTECT_PROMPTSTRUCT();
        InitPrompt(ref prompt);

        try
        {
            // Convert plaintext bytes into a BLOB structure.
            try
            {
                InitBLOB(plainTextBytes, ref plainTextBlob);
            }
            catch (Exception ex)
            {
                throw new Exception(
                    "Cannot initialize plaintext BLOB.", ex);
            }

            // Convert entropy bytes into a BLOB structure.
            try
            {
                InitBLOB(entropyBytes, ref entropyBlob);
            }
            catch (Exception ex)
            {
                throw new Exception(
                    "Cannot initialize entropy BLOB.", ex);
            }

            // Disable any types of UI.
            int flags = CRYPTPROTECT_UI_FORBIDDEN;

            // When using machine-specific key, set up machine flag.
            if (keyType == KeyType.MachineKey)
                flags |= CRYPTPROTECT_LOCAL_MACHINE;

            // Call DPAPI to encrypt data.
            bool success = CryptProtectData(ref plainTextBlob,
                                                description,
                                            ref entropyBlob,
                                                IntPtr.Zero,
                                            ref prompt,
                                                flags,
                                            ref cipherTextBlob);
            // Check the result.
            if (!success)
            {
                // If operation failed, retrieve last Win32 error.
                int errCode = Marshal.GetLastWin32Error();

                // Win32Exception will contain error message corresponding
                // to the Windows error code.
                throw new Exception(
                    "CryptProtectData failed.", new Win32Exception(errCode));
            }

            // Allocate memory to hold ciphertext.
            byte[] cipherTextBytes = new byte[cipherTextBlob.cbData];

            // Copy ciphertext from the BLOB to a byte array.
            Marshal.Copy(cipherTextBlob.pbData,
                            cipherTextBytes,
                            0,
                            cipherTextBlob.cbData);

            // Return the result.
            return cipherTextBytes;
        }
        catch (Exception ex)
        {
            throw new Exception("DPAPI was unable to encrypt data.", ex);
        }
        // Free all memory allocated for BLOBs.
        finally
        {
            if (plainTextBlob.pbData != IntPtr.Zero)
                Marshal.FreeHGlobal(plainTextBlob.pbData);

            if (cipherTextBlob.pbData != IntPtr.Zero)
                Marshal.FreeHGlobal(cipherTextBlob.pbData);

            if (entropyBlob.pbData != IntPtr.Zero)
                Marshal.FreeHGlobal(entropyBlob.pbData);
        }
    }
开发者ID:Xiryl,项目名称:ChromeRec,代码行数:98,代码来源:DPAPI.cs


示例18: Decrypt

 public byte[] Decrypt(byte[] cipherText, byte[] optionalEntropy)
 {
     DATA_BLOB pDataOut = new DATA_BLOB();
     DATA_BLOB pDataIn = new DATA_BLOB();
     CRYPTPROTECT_PROMPTSTRUCT ps = new CRYPTPROTECT_PROMPTSTRUCT();
     this.InitPromptstruct(ref ps);
     try
     {
         int num2;
         try
         {
             int length = cipherText.Length;
             pDataIn.pbData = Marshal.AllocHGlobal(length);
             if (IntPtr.Zero == pDataIn.pbData)
             {
                 throw new Exception("Unable to allocate cipherText buffer.");
             }
             pDataIn.cbData = length;
             Marshal.Copy(cipherText, 0, pDataIn.pbData, pDataIn.cbData);
         }
         catch (Exception exception)
         {
             throw new Exception("Exception marshalling data. " + exception.Message);
         }
         DATA_BLOB pOptionalEntropy = new DATA_BLOB();
         if (Store.Machine == this.store)
         {
             num2 = 5;
             if (optionalEntropy == null)
             {
                 optionalEntropy = new byte[0];
             }
             try
             {
                 int cb = optionalEntropy.Length;
                 pOptionalEntropy.pbData = Marshal.AllocHGlobal(cb);
                 if (IntPtr.Zero == pOptionalEntropy.pbData)
                 {
                     throw new Exception("Unable to allocate entropy buffer.");
                 }
                 pOptionalEntropy.cbData = cb;
                 Marshal.Copy(optionalEntropy, 0, pOptionalEntropy.pbData, cb);
                 goto Label_0113;
             }
             catch (Exception exception2)
             {
                 throw new Exception("Exception entropy marshalling data. " + exception2.Message);
             }
         }
         num2 = 1;
     Label_0113:
         if (!CryptUnprotectData(ref pDataIn, null, ref pOptionalEntropy, IntPtr.Zero, ref ps, num2, ref pDataOut))
         {
             throw new Exception("Decryption failed. " + GetErrorMessage(Marshal.GetLastWin32Error()));
         }
         if (IntPtr.Zero != pDataIn.pbData)
         {
             Marshal.FreeHGlobal(pDataIn.pbData);
         }
         if (IntPtr.Zero != pOptionalEntropy.pbData)
         {
             Marshal.FreeHGlobal(pOptionalEntropy.pbData);
         }
     }
     catch (Exception exception3)
     {
         throw new Exception("Exception decrypting. " + exception3.Message);
     }
     byte[] destination = new byte[pDataOut.cbData];
     Marshal.Copy(pDataOut.pbData, destination, 0, pDataOut.cbData);
     return destination;
 }
开发者ID:jokingzhou,项目名称:AnJi-DevZoneGIS,代码行数:72,代码来源:DataProtector.cs


示例19: CryptUnprotectData

 static extern bool CryptUnprotectData(ref DATA_BLOB pDataIn, string ppszDataDescr, ref  DATA_BLOB pOptionalEntropy, IntPtr pvReserved, IntPtr pPromptStruct, uint dwFlags, [In, Out]ref DATA_BLOB pDataOut);
开发者ID:rokugasenpai,项目名称:SnkLib.App.CookieGetter,代码行数:1,代码来源:Utility.cs


示例20: InitBLOB

    private static void InitBLOB(byte[] data, ref DATA_BLOB blob)
    {
        // Use empty array for null parameter.
        if (data == null)
            data = new byte[0];

        // Allocate memory for the BLOB data.
        blob.pbData = Marshal.AllocHGlobal(data.Length);

        // Make sure that memory allocation was successful.
        if (blob.pbData == IntPtr.Zero)
            throw new Exception(
                "Unable to allocate data buffer for BLOB structure.");

        // Specify number of bytes in the BLOB.
        blob.cbData = data.Length;

        // Copy data from original source to the BLOB structure.
        Marshal.Copy(data, 0, blob.pbData, data.Length);
    }
开发者ID:Xiryl,项目名称:ChromeRec,代码行数:20,代码来源:DPAPI.cs



注:本文中的DATA_BLOB类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# DB类代码示例发布时间:2022-05-24
下一篇:
C# DAT类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap