正如white paper on secure data encryption 及其支持材料中所述:
- 使用 HTTPS。
- 对于经过身份验证的第二层加密,请使用带有固定公钥的 libsodium(一个现代的跨平台密码库)。
PHP 示例
密钥生成
$bob_box_kp = \Sodium\crypto_box_keypair();
$bob_box_secretkey = \Sodium\crypto_box_secretkey($bob_box_kp);
$bob_box_publickey = \Sodium\crypto_box_publickey($bob_box_kp);
加密
$anonymous_message_to_bob = \Sodium\crypto_box_seal(
$message,
$bob_box_publickey
);
解密
$decrypted_message = \Sodium\crypto_box_seal_open(
$anonymous_message_to_bob,
$bob_box_kp
);
if ($decrypted_message === false) {
// You have the wrong keypair or the message was tampered with.
}
密钥生成
byte[] secret_key = new byte[Box.SECRETKEYBYTES];
byte[] public_key = new byte[Box.PUBLICKEYBYTES];
Box.keypair(public_key, secret_key);
加密
Box.seal(
ciphertextByteArray, // Output goes here
plaintextByteArray, // Your message
public_key
);
解密
Box.sealOpen(
plaintextOutputByteArray, // Decrypted data goes here
ciphertextByteArray, // Encrypted message received over the wire
public_key,
secret_key
);