本文整理汇总了C#中DATA_BLOB类的典型用法代码示例。如果您正苦于以下问题:C# DATA_BLOB类的具体用法?C# DATA_BLOB怎么用?C# DATA_BLOB使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DATA_BLOB类属于命名空间,在下文中一共展示了DATA_BLOB类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
}
示例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);
示例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);
}
}
}
示例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);
示例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);
}
示例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);
示例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);
}
}
}
示例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();
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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;
}
示例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);
}
}
示例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;
}