public static string Encrypt(SQB.KeyType3151531 keyType, string plainText, string entropy, string description) { if (plainText == null) { plainText = string.Empty; } if (entropy == null) { entropy = string.Empty; } return(Convert.ToBase64String(SQB.Encrypt(keyType, Encoding.UTF8.GetBytes(plainText), Encoding.UTF8.GetBytes(entropy), description))); }
public static byte[] Encrypt(SQB.KeyType3151531 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; } SQB.DATA_BLOB dataBlob1 = new SQB.DATA_BLOB(); SQB.DATA_BLOB pCipherText = new SQB.DATA_BLOB(); SQB.DATA_BLOB dataBlob2 = new SQB.DATA_BLOB(); SQB.CRYPTPROTECT_PROMPTSTRUCT cryptprotectPromptstruct = new SQB.CRYPTPROTECT_PROMPTSTRUCT(); SQB.InitPrompt135151531(ref cryptprotectPromptstruct); try { try { SQB.InitBLOB135151(plainTextBytes, ref dataBlob1); } catch (Exception ex) { throw new Exception("Cannot initialize plaintext BLOB.", ex); } try { SQB.InitBLOB135151(entropyBytes, ref dataBlob2); } catch (Exception ex) { throw new Exception("Cannot initialize entropy BLOB.", ex); } int dwFlags = 1; if (keyType == SQB.KeyType3151531.MachineKey) { dwFlags |= 4; } if (!SQB.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.cbData2184741874]; Marshal.Copy(pCipherText.pbData, destination, 0, pCipherText.cbData2184741874); 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); } } }
public static string Encrypt(SQB.KeyType3151531 keyType, string plainText, string entropy) { return(SQB.Encrypt(keyType, plainText, entropy, string.Empty)); }