Python原生库实现JS RSA-NOPADDING加密

 最近需要对接一个JS前端,采用了如下文件

security.js
/*
 * RSA, a suite of routines for performing RSA public-key computations in JavaScript.
 * Copyright 1998-2005 David Shapiro.
 * Dave Shapiro
 * dave@ohdave.com 
 * changed by Fuchun, 2010-05-06
 * fcrpg2005@gmail.com
 */

但是Python端并没有相应的库,网上的记录大多异常繁琐,特此记录下踩坑过程 

var modulus ="00A828DB9D028A4B9FC017821C119DFFB8537ECEF7F91D4BC06DB06CC8B4E6B2D0A949B66A86782D23AA5AA847312D91BE07DC1430C1A6F6DE01A3D98474FE4511AAB7E4E709045B61F17D0DC4E34FB4BE0FF32A04E442EEE6B326D97E11AE8F23BF09926BF05AAF65DE34BB90DEBDCEE475D0832B79586B4B02DEED2FC3EA10B3";
var exponent ="010001";
var key = RSAUtils.getKeyPair(exponent, '', modulus);
phoneNum = RSAUtils.encryptedString(key,phoneNum);

 最终实现代码

def rsa_no_padding(modulus, exponent):
    m = int(modulus, 16)
    e = int(exponent, 16)
    # 16进制转10进制
    t = '13888888888'[::-1].encode('utf-8')
    # 字符串逆向并转换为bytes
    input_nr = int.from_bytes(t, byteorder='big')
    # 将字节转化成int型数字,如果没有标明进制,看做ascii码值
    crypt_nr = pow(input_nr, e, m)
    # 计算x的y次方,如果z在存在,则再对结果进行取模,其结果等效于pow(x,y) %z
    length = ceil(m.bit_length() / 8)
    # 取模数的比特长度(二进制长度),除以8将比特转为字节
    crypt_data = crypt_nr.to_bytes(length, byteorder='big')
    # 将密文转换为bytes存储(8字节),返回hex(16字节)
    return crypt_data.hex()

RSA算法原理(二) - 阮一峰的网络日志

Python实现RSA无填充加密,兼容BouncyCastle - 超软毛毛虫 - 博客园

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RSA-3072 加密和解密的实现可以使用 Python 中的 `cryptography` ,而 AES128 加密和解密则可以使用 Python 中的 `pycryptodome` 。 下面分别介绍如何使用这两个实现加密和解密: 1. RSA-3072 加密和解密 ```python from cryptography.hazmat.primitives.asymmetric import rsa, padding from cryptography.hazmat.primitives import serialization, hashes # 生成 RSA 密钥对 private_key = rsa.generate_private_key( public_exponent=65537, key_size=3072, ) public_key = private_key.public_key() # 将密钥对序列化为 PEM 格式 private_key_pem = private_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption() ) public_key_pem = public_key.public_bytes( encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo ) # 加密明文 message = b"Hello, World!" ciphertext = public_key.encrypt( message, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) # 解密密文 plaintext = private_key.decrypt( ciphertext, padding.OAEP( mgf=padding.MGF1(algorithm=hashes.SHA256()), algorithm=hashes.SHA256(), label=None ) ) ``` 2. AES128 加密和解密 ```python from Crypto.Cipher import AES from Crypto.Random import get_random_bytes # 生成随机密钥 key = get_random_bytes(16) # 加密明文 message = b"Hello, World!" cipher = AES.new(key, AES.MODE_EAX) ciphertext, tag = cipher.encrypt_and_digest(message) # 解密密文 decipher = AES.new(key, AES.MODE_EAX, nonce=cipher.nonce) plaintext = decipher.decrypt_and_verify(ciphertext, tag) ``` 需要注意的是,这两个的安装方式如下: ``` pip install cryptography pip install pycryptodome ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值