rsa php 转c,将RSA加密代码从php转换为python

我正在尝试连接到我的python应用程序中的api。因此api文档附带了php和asp示例代码,但没有python

我对php很好,但是对加密没有任何经验。我正在尝试使用php sample为api重新编写python代码。

他们将此类用于RSA

https://github.com/AlaFalaki/Pclass/blob/master/libraries/rsa.class.php

(因为它是一个RSA lib,所以我猜测是python RSA lib可以处理这部分):

static function rsa_sign($message, $private_key, $modulus, $keylength) {

$padded = RSA::add_PKCS1_padding($message, false, $keylength / 8);

$number = RSA::binary_to_number($padded);

$signed = RSA::pow_mod($number, $private_key, $modulus);

$result = RSA::number_to_binary($signed, $keylength / 8);

return $result;

}

这是php sign函数需要4个参数使用一些内部函数的问题……但是python rsa有3个,其中之一就是哈希方法!

rsa.sign(message, priv_key, hash)

Parameters:

message – the message to sign. Can be an 8-bit string or a file-like object. If message has a read() method, it is assumed to be a file-like object.

priv_key – the rsa.PrivateKey to sign with

hash – the hash method used on the message. Use ‘MD5’, ‘SHA-1’, ‘SHA-256’, ‘SHA-384’ or ‘SHA-512’.

我尝试了一些小实验,看看是否能获得相同的输出

所以我text在php中唱了一个简单的字符串

echo base64_encode(RSA::rsa_sign(sha1("test"),$private_key,$modulus,$key_length));

我有

就像是

dKt+4CocMNdIrtYCUr8aZykR8CpfmYUEEVONMuAPlM5mR70AoyzMhGjcEGB9fKLVC4rr5xt66w2ZmHqWO+p834rJmo9Fj57udRSY5wFs0VokMF2S2SMFn5WTYYmMBuWciRzZybWnfXcSIyp9Ibi28cdwl5hXJOMpXEJrNQLFy2s=

接下来,我从xml文件中提取了private_key,public_key,模数,他们使用包含我的密钥的api给我(使用相同的RSA类)

$xmlObj = simplexml_load_string($xmlRsakey);

$this->modulus = RSA::binary_to_number(base64_decode($xmlObj->Modulus));

$this->public_key = RSA::binary_to_number(base64_decode($xmlObj->Exponent));

$this->private_key = RSA::binary_to_number(base64_decode($xmlObj->D));

$this->key_length = strlen(base64_decode($xmlObj->Modulus))*8;

我和他们做了一个python字典

def keys():

obj = {

'modulus' : "14417185111734127374105962730273......." ,

'public_key' : "61111" ,

'private_key' : "3739752306322843055980611965983321761993....." ,

'key_length' : 1024 ,

}

return obj

我试图在python中签名一个字符串

def sign(request):

api = keys()

message = 'test'

crypto = rsa.sign(message.encode('utf-8'), api['private_key'] , 'SHA-1')

b64 = base64.b64encode(crypto)

return HttpResponse(b64)

但我得到:

'str' object has no attribute 'n'

那是我失败的实验

就像我说的,我没有任何加密或rsa的经验。

我应该放弃并使用php进行加密/解密吗?

解决方案

他们将此类用于RSA

https://github.com/AlaFalaki/Pclass/blob/master/libraries/rsa.class.php

我的建议:大声尖叫。

RSA是安全问题的一个雷区。您可以搞砸很多事情。因此,当有人使用BC扩展名在PHP中实现基元时,这在安全性上等同于赤裸裸地站在射击小队前,并期望没有漏洞。

libsodium同时提供PHP和Python绑定。

如果RSA是不可避免的...

如果您真的想要RSA而不是现代加密技术,请查看phpseclib。

use

$rsa = new RSA();

// HIGHLY RECOMMENDED FOR SECURITY:

$rsa->setEncryptionMode(RSA::ENCRYPTION_OAEP);

$rsa->setMGFHash('sha256');

$rsa->loadKey($yourPEMencodedRSAPublicKey);

$ciphertext = $rsa->encrypt($plaintext);

If you're going to encrypt with RSA, you must follow these cryptography rules. If your library doesn't let you follow these rules, it's time to switch to libsodium.

Also note that encrypting large messages with RSA is both slow and dangerous: There's usually nothing preventing messages from being reordered, which can be really bad.

The solution here is: Use symmetric-key authenticated encryption and use RSA to encrypt the public key. That's what EasyRSA does, although I'm not aware of any Python equivalents, so I can't recommend that as a solution.

Of course, if you use libsodium's crypto_box API you don't have to worry about that!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值