private unsafe static string DecryptStringUsingLocalUser(string encryptedString)
        {
            Crypto.DataBlob dataBlob = default(Crypto.DataBlob);
            Crypto.CryptProtectPromptStruct cryptProtectPromptStruct = default(Crypto.CryptProtectPromptStruct);
            if (string.IsNullOrEmpty(encryptedString))
            {
                return(string.Empty);
            }
            dataBlob.Size = 0;
            cryptProtectPromptStruct.Size = 0;
            byte[]          array = System.Convert.FromBase64String(encryptedString);
            Crypto.DataBlob dataBlob2;
            dataBlob2.Size = array.Length;
            Crypto.DataBlob dataBlob3;
            string          result;

            fixed(byte *ptr = array)
            {
                dataBlob2.Data = (System.IntPtr)((void *)ptr);
                if (!Crypto.CryptUnprotectData(ref dataBlob2, null, ref dataBlob, (System.IntPtr)null, ref cryptProtectPromptStruct, 0, out dataBlob3))
                {
                    //throw new System.Exception("Failed to decrypt using {0} credential".InvariantFormat(new object[]
                    //{
                    //    CredentialsUI.GetLoggedInUser()
                    //}));
                }
                char *ptr2 = (char *)((void *)dataBlob3.Data);

                char[] array2 = new char[dataBlob3.Size / 2];
                for (int i = 0; i < array2.Length; i++)
                {
                    array2[i] = ptr2[i];
                }
                result = new string(array2);
            }

            Crypto.LocalFree(dataBlob3.Data);
            return(result);
        }
Example #2
0
        private unsafe static string DecryptStringUsingLocalUser(string encryptedString)
        {
            Crypto.DataBlob optionalEntropy = default(Crypto.DataBlob);
            Crypto.CryptProtectPromptStruct promptStruct = default(Crypto.CryptProtectPromptStruct);
            if (string.IsNullOrEmpty(encryptedString))
            {
                return(string.Empty);
            }
            optionalEntropy.Size = 0;
            promptStruct.Size    = 0;
            byte[]          array  = Convert.FromBase64String(encryptedString);
            Crypto.DataBlob dataIn = default(Crypto.DataBlob);
            dataIn.Size = array.Length;
            Crypto.DataBlob dataOut;
            string          result;

            fixed(byte *value = array)
            {
                dataIn.Data = (IntPtr)(void *)value;
                if (!Crypto.CryptUnprotectData(ref dataIn, null, ref optionalEntropy, (IntPtr)null, ref promptStruct, 0, out dataOut))
                {
                    throw new Exception("使用{0}证书解密失败".InvariantFormat(CredentialsUI.GetLoggedInUser()));
                }
                char *ptr = (char *)(void *)dataOut.Data;

                char[] array2 = new char[dataOut.Size / 2];
                for (int i = 0; i < array2.Length; i++)
                {
                    array2[i] = ptr[i];
                }
                result = new string(array2);
            }

            Crypto.LocalFree(dataOut.Data);
            return(result);
        }
Example #3
0
        private unsafe static string EncryptStringUsingLocalUser(string plaintext)
        {
            Crypto.DataBlob optionalEntropy = default(Crypto.DataBlob);
            Crypto.CryptProtectPromptStruct promptStruct = default(Crypto.CryptProtectPromptStruct);
            if (string.IsNullOrEmpty(plaintext))
            {
                return(null);
            }
            optionalEntropy.Size = 0;
            promptStruct.Size    = 0;
            char[]          array  = plaintext.ToCharArray();
            Crypto.DataBlob dataIn = default(Crypto.DataBlob);
            dataIn.Size = array.Length * 2;
            Crypto.DataBlob dataOut;
            fixed(char *value = array)
            {
                dataIn.Data = (IntPtr)(void *)value;
                if (!Crypto.CryptProtectData(ref dataIn, null, ref optionalEntropy, (IntPtr)null, ref promptStruct, 0, out dataOut))
                {
                    FormTools.ErrorDialog("无法加密密码");
                    return(null);
                }
            }

            byte *ptr = (byte *)(void *)dataOut.Data;

            byte[] array2 = new byte[dataOut.Size];
            for (int i = 0; i < array2.Length; i++)
            {
                array2[i] = ptr[i];
            }
            string result = Convert.ToBase64String(array2);

            Crypto.LocalFree(dataOut.Data);
            return(result);
        }