使用后端的公共密钥(PHP sodium)在前端(sodium-plus.js)上加密 [英] Encrypt on frontend (sodium-plus.js) with public-key from backend (PHP sodium)

查看:274
本文介绍了使用后端的公共密钥(PHP sodium)在前端(sodium-plus.js)上加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用sodium-plus.js在PHP浏览器中生成的密钥在Web浏览器中实现匿名公钥加密,如下所示:

$keyPair = sodium_crypto_box_keypair();

$privateKey = sodium_crypto_box_secretkey($keyPair);
$publicKey = sodium_crypto_box_publickey($keyPair);

使用此方法生成的密钥在PHP中使用sodium_crypto_box_sealsodium_crypto_box_seal_open方法都可以正常工作,但是,我无法使其在前端工作.我的方法:

<script type='text/javascript' src='js/sodium-plus.min.js?v=0.4.2'></script>
<script>
async function getPublicKey() {

    return X25519PublicKey.from(
        '<?php echo sodium_bin2hex($publicKey); ?>', // 6a00b1550ccdeff3886a469b9cd4e5dc9aecd30f5deb3dd3e29fd01f8a32103f
        'hex'
    );

}

async function encryptString(clearText, publicKey) {

    if (!window.sodium) window.sodium = await SodiumPlus.auto();

    let cipherText = await sodium.crypto_box_seal(clearText, publicKey);

    return cipherText.toString('hex');

}

(async function () {

    let clearText = "String that contains secret.";
    let publicKey = await getPublicKey();

    console.log(await encryptString(clearText,publicKey));

})();
</script>

这将返回 TypeError:参数2必须是控制台中X25519PublicKey的实例.

注意:

  1. 从前端的sodium.crypto_box_keypair()派生的公共密钥.
  2. 尝试使用CryptographyKey.from()而不是X25519PublicKey.from() –无效.
  3. getPublicKey()函数返回带有buffer: Uint8Array(32) [ … ]的对象,而派生自sodium.crypto_box_keypair()的公钥返回带有buffer: Uint8Array(32) [ … ], keyType: "x25519", publicKey: true的对象.

概念基于:

  1. https://github.com/paragonie/sodium-plus/blob/master/docs/SodiumPlus/sealed-boxes.md
  2. https://dev.to/paragonie/message -encryption-in-javascript-and-php-cg9
  3. https://stackoverflow.com/a/34058638

解决方案

简短的答案是:升级到0.5.0或更高版本.

长答案是:等待0.6.0版发布,然后升级到那个,因为它也会放宽您遇到的某些类型学究限制./p>

I would like to achieve an anonymous public-key encryption in a web browser using sodium-plus.js with keys generated in PHP sodium like this:

$keyPair = sodium_crypto_box_keypair();

$privateKey = sodium_crypto_box_secretkey($keyPair);
$publicKey = sodium_crypto_box_publickey($keyPair);

The keys generated with this method work fine in PHP with the sodium_crypto_box_seal and sodium_crypto_box_seal_open methods, but however, I am unable to make it work on the frontend. My approach:

<script type='text/javascript' src='js/sodium-plus.min.js?v=0.4.2'></script>
<script>
async function getPublicKey() {

    return X25519PublicKey.from(
        '<?php echo sodium_bin2hex($publicKey); ?>', // 6a00b1550ccdeff3886a469b9cd4e5dc9aecd30f5deb3dd3e29fd01f8a32103f
        'hex'
    );

}

async function encryptString(clearText, publicKey) {

    if (!window.sodium) window.sodium = await SodiumPlus.auto();

    let cipherText = await sodium.crypto_box_seal(clearText, publicKey);

    return cipherText.toString('hex');

}

(async function () {

    let clearText = "String that contains secret.";
    let publicKey = await getPublicKey();

    console.log(await encryptString(clearText,publicKey));

})();
</script>

This returns TypeError: Argument 2 must be an instance of X25519PublicKey in the console.

Notes:

  1. A public-key that is derived from sodium.crypto_box_keypair() on the frontend works.
  2. Tried with CryptographyKey.from() instead of X25519PublicKey.from() – did not work.
  3. The getPublicKey() function returns an object wit buffer: Uint8Array(32) [ … ], while the public-key derived from sodium.crypto_box_keypair() returns an object with buffer: Uint8Array(32) [ … ], keyType: "x25519", publicKey: true.

Concept is based on:

  1. https://github.com/paragonie/sodium-plus/blob/master/docs/SodiumPlus/sealed-boxes.md
  2. https://dev.to/paragonie/message-encryption-in-javascript-and-php-cg9
  3. https://stackoverflow.com/a/34058638

解决方案

The short answer is: Upgrade to version 0.5.0 or later.

The long answer is: Wait until version 0.6.0 is out, and then upgrade to that instead, since it also loosens some of the type-pedantic restrictions that you're running into.

这篇关于使用后端的公共密钥(PHP sodium)在前端(sodium-plus.js)上加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆