Example #1
0
        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());
        }
Example #2
0
        public static String CryptBinaryToString(Byte[] inputArray, CryptEncoding outputEncoding, CryptFormatting outputFormatting)
        {
            if (
                outputEncoding == CryptEncoding.CRYPT_STRING_BASE64_ANY ||
                outputEncoding == CryptEncoding.CRYPT_STRING_ANY ||
                outputEncoding == CryptEncoding.CRYPT_STRING_HEX_ANY
                )
            {
                throw new ArgumentException("Invalid encoding is specified.");
            }
            UInt32 flags      = (UInt32)outputEncoding | (UInt32)outputFormatting;
            UInt32 pcchString = 0;

            if (Crypt32.CryptBinaryToString(inputArray, (UInt32)inputArray.Length, flags, null, ref pcchString))
            {
                StringBuilder SB = new StringBuilder((Int32)pcchString);
                Crypt32.CryptBinaryToString(inputArray, (UInt32)inputArray.Length, flags, SB, ref pcchString);
                return(SB.ToString());
            }
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }