在 Windows 上,您最好的解决方案是使用 Chrome、IE、远程桌面连接和许多其他技术使用的 数据保护 API 来加密数据。
优点是数据是用用户自己的 Windows 密码加密的(以一种迂回的方式)。当用户在 Windows 中输入密码时,所有“受保护”数据都可用。
特点:
- 数据已加密
- 用户不必输入密码来加密数据
- 只有用户才能解密它
- 用户无需输入密码即可解密数据
示例伪代码
你想要的API是CryptProtectData和CryptUnprotectData:
public bytes[] ProtectBytes(bytes[] plaintext)
{
DATA_BLOB dataIn;
dataIn.cbData = plaintext.Length;
dataIn.pbData = Addr(plaintext[0]);
DATA_BLOB dataOut;
BOOL bRes = CryptProtectData(
dataIn,
null, //data description (optional PWideChar)
null, //optional entropy (PDATA_BLOB)
null, //reserved
null, //prompt struct
CRYPTPROTECT_UI_FORBIDDEN,
ref dataOut);
if (!bRes) then
{
DWORD le = GetLastError();
throw new Win32Error(le, "Error calling CryptProtectData");
}
//Copy ciphertext from dataOut blob into an actual array
bytes[] result;
SetLength(result, dataOut.cbData);
CopyMemory(dataOut.pbData, Addr(result[0]), dataOut.cbData);
//When you have finished using the DATA_BLOB structure, free its pbData member by calling the LocalFree function
LocalFree(HANDLE(dataOut.pbData)); //LocalFree takes a handle, not a pointer. But that's what the SDK says.
}
稍后,当您需要解密 blob 时,您使用 CryptProtectData。
数据使用用户的 Windows 密码(有效)加密;只有拥有 Windows 密码的人才能解密。
注意:任何发布到公共领域的代码。无需署名。