Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Web加密API JWK在Python中的使用_Javascript_Python_Pycrypto_Webcrypto Api_Jwk - Fatal编程技术网

Javascript Web加密API JWK在Python中的使用

Javascript Web加密API JWK在Python中的使用,javascript,python,pycrypto,webcrypto-api,jwk,Javascript,Python,Pycrypto,Webcrypto Api,Jwk,我正在开发一个P2P基础设施,它将拥有来自一组不同应用程序的数据,并通过网络进行分发。这个P2P覆盖由一组Python Twisted服务器组成 我需要为每个应用程序的每个用户保证存储数据的安全性和隐私性。因此,我在web应用程序的客户端使用生成RSA密钥对。RSA密钥对也将存储在P2P覆盖中。因此,我在客户端对私钥进行加密,并派生出用户密码 此外,我正在使用模块将JWK公钥转换为PEM密钥,用于Python加密库(PyCrypt或m2Crypto) 最后,我必须保证包含这些凭证的消息以及用户数

我正在开发一个P2P基础设施,它将拥有来自一组不同应用程序的数据,并通过网络进行分发。这个P2P覆盖由一组Python Twisted服务器组成

我需要为每个应用程序的每个用户保证存储数据的安全性和隐私性。因此,我在web应用程序的客户端使用生成RSA密钥对。RSA密钥对也将存储在P2P覆盖中。因此,我在客户端对私钥进行加密,并派生出用户密码

此外,我正在使用模块将JWK公钥转换为PEM密钥,用于Python加密库(PyCrypt或m2Crypto)

最后,我必须保证包含这些凭证的消息以及用户数据保持其完整性。因此,在客户端,我使用用户的私钥对该数据进行签名

我将数据和签名都以ArrayBuffer类型发送到服务器,并以base64编码

function signData(private_key, data, callback){

    var dataForHash = str2ab(JSON.stringify(sortObject(data)));
    computeSHA(dataForHash, "SHA-256", function(hash){


        signRSA(private_key, hash, function(data){      
            callback(data.buffer.b64encode(), dataForHash.b64encode());
        });    
    });
}

function computeSHA(data, mode, callback){
    window.crypto.subtle.digest(
        {
            name: mode,
        },
        data
    )
    .then(function(hash){
        callback(new Uint8Array(hash).buffer);
    })
    .catch(function(err){
        console.error(err);
    });
}

function signRSA(private_key, data, callback){
    window.crypto.subtle.sign(
        {
            name: "RSASSA-PKCS1-v1_5",
        },
        private_key,
        data
    )
    .then(function(signature){
        callback(new Uint8Array(signature));
    })
    .catch(function(err){
        console.error(err);
    });
}

ArrayBuffer.prototype.b64encode = function(){
    return btoa(String.fromCharCode.apply(null, new Uint8Array(this)));
};
之后,当Python服务器接收到这个http请求时,它将对来自base64的数据和签名进行解码

dataForHash = base64.b64decode(dataReceived['data'])
signature = base64.b64decode(dataReceived['signature'])
为了验证签名,需要公钥。因此:

data = utils.byteify(json.loads(dataForHash.decode("utf-16")))
pub_key = base64.b64decode(data['pub_key']) # Get PEM Public Key
(utils.byteify()将unicode字符串转换为常规字符串)

验证签名:

Authentication.verifySignature(signature, dataForHash, pub_key)
方法定义:

from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA

def verifySignature(signature, data, pub_key):
    key = RSA.importKey(pub_key)
    h = SHA256.new(data)
    verifier = PKCS1_v1_5.new(key)
    return verifier.verify(h, signature)

但是,签名验证返回False。我还尝试使用m2crypto库,但它返回0

我设法找到了问题

虽然在Python(PyCrypto)中,sign函数应该使用Web加密API接收要签名的数据的散列,但sign方法在对接收到的数据进行签名之前将散列函数应用于该数据

因此,JS中的数据被散列两次,一次在调用sign方法之前,一次在sign方法中,一次在创建签名之前

function signData(private_key, data, callback){

    var dataForHash = str2ab(JSON.stringify(sortObject(data)));

    signRSA(private_key, dataForHash, function(data){           

        callback(data.buffer.b64encode(), dataForHash.b64encode());
    });    
}

ArrayBuffer.prototype.b64encode = function(){
    return btoa(String.fromCharCode.apply(null, new Uint8Array(this)));
};

String.prototype.b64decode = function(){
    var binary_string = window.atob(this);
    var len = binary_string.length;
    var bytes = new Uint8Array(new ArrayBuffer(len));
    for (var i = 0; i < len; i++)        {
        bytes[i] = binary_string.charCodeAt(i);
    }
    return bytes;
};
函数signData(私钥、数据、回调){
var dataForHash=str2ab(JSON.stringify(sortObject(data));
signRSA(私钥,dataForHash,函数(数据){
回调(data.buffer.b64encode(),dataForHash.b64encode());
});    
}
ArrayBuffer.prototype.b64encode=函数(){
返回btoa(String.fromCharCode.apply(null,新的Uint8Array(this)));
};
String.prototype.B64解码=函数(){
var binary_string=window.atob(this);
var len=二进制字符串长度;
var bytes=新的Uint8Array(新的ArrayBuffer(len));
对于(变量i=0;i
通过此修改,python中的验证现在返回True