internal static byte[] Decode(string input, Crypt32.CrypBinaryFlags flags, int inputSize) { int outputSize = 0; int pdwSkip; int pdwFlags; if (!Crypt32.CryptStringToBinary(input, inputSize, flags, IntPtr.Zero, ref outputSize, out pdwSkip, out pdwFlags)) { int nativeError = Marshal.GetLastWin32Error(); throw new InteropException(nativeError); } IntPtr nativeBuffer = IntPtr.Zero; try { nativeBuffer = Marshal.AllocCoTaskMem(outputSize); if (!Crypt32.CryptStringToBinary(input, inputSize, flags, nativeBuffer, ref outputSize, out pdwSkip, out pdwFlags)) { int nativeError = Marshal.GetLastWin32Error(); throw new InteropException(nativeError); } byte[] result = new byte[outputSize]; Marshal.Copy(nativeBuffer, result, 0, outputSize); return(result); } finally { if (IntPtr.Zero != nativeBuffer) { Marshal.FreeCoTaskMem(nativeBuffer); } } }
public static Byte[] CryptStringToBinary(String inputString, CryptEncoding inputEncoding = CryptEncoding.CRYPT_STRING_ANY) { UInt32 pcbBinary = 0; if (Crypt32.CryptStringToBinary(inputString, (UInt32)inputString.Length, (UInt32)inputEncoding, null, ref pcbBinary, 0, 0)) { Byte[] pbBinary = new Byte[pcbBinary]; Crypt32.CryptStringToBinary(inputString, (UInt32)inputString.Length, (UInt32)inputEncoding, pbBinary, ref pcbBinary, 0, 0); return(pbBinary); } throw new Win32Exception(Marshal.GetLastWin32Error()); }