这可能是已回答问题的重复,但是我似乎无法获得相同的结果。希望在这里有一些指导。
JSEncrypt(客户端)
let encrypt = new Encrypt.JSEncrypt();
encrypt.setPublicKey(this.publicKey); // retrieved from server
encrypt.encrypt(password);
Run Code Online (Sandbox Code Playgroud)
BouncyCastle(服务器)-RSA密钥生成
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(1024);
KeyPair pair = generator.generateKeyPair();
PublicKey pubKey = pair.getPublic();
PrivateKey privKey = pair.getPrivate();
// returned to client
String publicKeyStr = new String(Base64.encodeBase64(pubKey.getEncoded()));
String privateKeyStr = new String(Base64.encodeBase64(privKey.getEncoded()));
Run Code Online (Sandbox Code Playgroud)
BouncyCastle(服务器)-解密
Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// org.apache.commons.codec.binary.Hex
byte[] cipherText = cipher.doFinal(Hex.decodeHex(encrypted.toCharArray()));
decrypted = new String(cipherText, BaseConstant.ENC_UTF8);
Run Code Online (Sandbox Code Playgroud)
错误
org.apache.commons.codec.DecoderException:org.apache.commons.codec.binary.Hex.toDigit(Hex.java:178)处索引0处的非法十六进制字符I在org.apache.commons.codec.binary.Hex中.decodeHex(Hex.java:89)
我注意到的一件事是JSEncrypt加密的文本的长度为172,而服务器端的加密则为256。
提到的已回答问题是使用我已经设置的RSA / None / PKCS1Padding。我还能缺少什么?
我使用NodeJS Crypto模块在后端使用 RSA 进行加密和解密,并在前端使用JSencrypt进行前端 RSA 加密和解密
但问题是,每当我使用公钥在前端加密时,我的后端都会抛出此错误(PS:我在 NuxtJS 中使用它,因此使用导入函数。)
const { JSEncrypt } = await import('jsencrypt')
const rsa = new JSEncrypt({ default_key_size: 1024 })
rsa.setPublicKey(store.state.publicKey)
const xKey = rsa.encrypt(store.state.ticket)
Run Code Online (Sandbox Code Playgroud)
然后每当我尝试在后端使用这段代码进行解码时,它就会抛出这个
Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
Run Code Online (Sandbox Code Playgroud)
这是我使用 privateKey 进行 RSA 解码的后端代码
Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error
Run Code Online (Sandbox Code Playgroud) 我是密码学的全新人物.我想从服务器端生成RSA密钥对,并将其发送给所有客户端(浏览器).但在此之前,我只是通过加密python中的数据并通过pubnub发送到index.html文件并尝试在JavaScript中解密来测试场景.问题是当我做加密时;
random_generator = Random.new().read
key = RSA.generate(1024, random_generator)
print key.exportKey() #<--private key
public_key = key.publickey()
print public_key.exportKey() #<--public key
msg = "hello"
enc_data = public_key.encrypt(msg, 32)
print '----ENCRYPTED DATA----'
enc = enc_data[0]
Run Code Online (Sandbox Code Playgroud)
并发送加密数据enc,它给我这个错误:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc4 in position 2: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)
我试着把它转换成
enc = base64.b64encode(enc_data[0])
Run Code Online (Sandbox Code Playgroud)
它发送没有错误.但JS解密方法获取无
var enc_from_python = $('#input').val();
console.log("ENCRYPTED data:", enc_from_python);
var decrypt = new JSEncrypt();
decrypt.setPrivateKey($('#privkey').val());
var uncrypted = decrypt.decrypt(enc_from_python);
console.log(">>>",uncrypted); //<-- this is None ! why ?
Run Code Online (Sandbox Code Playgroud)
这两个代码都可以自己进行加/减.我还尝试使用python中收到的密钥对来加密/解析JS中的数据,这很有效.我想问题是来自Pycrypto的编码数据的unicode编码格式不匹配.谁能告诉我在这里我错过了什么.
Python的完整代码:
import time …Run Code Online (Sandbox Code Playgroud) 我试图通过使用对称和非对称加密来保护 JS 前端和 PHP 后端之间的通信。我正在客户端上创建一个对称密钥,并使用服务器的公钥和 JSEncrypt 对其进行加密,然后将其发送到服务器以供将来使用。但是,当我在服务器端获取数据时,我遇到了困难。openssl_open 需要一个信封来解密对称密钥,我什至不确定信封中应该包含什么数据。我的印象是信封是用公钥加密的对称密钥,但使用它并没有起作用。我还尝试了不同的解码组合,因为我读到 JSEncrypt 以 64 进制编码消息,以十六进制编码密钥,但这些尝试也是徒劳的。
\n\nJS加密代码:
\n\nlet pub = "-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----";\n\n//I have a function that just creates a random string of characters\nlet key = generateKey(32);\nlet aesData = CryptoJS.AES.encrypt( "test", key );\nlet symKey = aesData.key + ":::" + aesData.iv;\nlet msg = aesData.toString();\n\nlet rsa = new JSEncrypt();\nrsa.setPublicKey( pub );\nlet cryptKey = rsa.encrypt( symKey );\n\n//I\'m passing the data through a hidden form field\n$("#key").val(cryptKey + ":::" + msg);\nRun Code Online (Sandbox Code Playgroud)\n\nPHP解密代码:
\n\n$key …Run Code Online (Sandbox Code Playgroud) 我浏览了所有围绕该问题的教程,但未能找到解决方案。首先有一些背景。我有一个Angular 2应用,我需要使用此JS库进行加密:https : //github.com/travist/jsencrypt
首先,我用以下代码行安装了jsencrypt节点模块:npm install --save jsencrypt。这行得通,可以在我的node_modules文件夹中找到jsencrypt模块。
一些教程告诉我创建一个src / typings.d.ts文件,然后添加以下行:声明模块'jsencrypt';
然后,在我的comonents.ts文件中,通过以下行将其导入:import * as'JSencrypt from'jsencrypt';
我还尝试添加<脚本src =“ / node_modules / jsencrypt / bin / jsencrypt.js”> </ script>
到我的.html文件中。
在我的.component文件的初始化中,我尝试声明一个简单的JSEncrypt对象:var crypto = new JSEncrypt();
控制台显示此错误:TypeError:对象不是构造函数(正在评估“ new WEBPACK_IMPORTED_MODULE_4_jsencrypt()”)
因此,我假设它无法识别jsencrypt模块。
我只在Angular工作了几天,所以对于Angular应用程序的所有术语和基本了解我还是很陌生。任何帮助是极大的赞赏。
以本教程为起点:https : //hackernoon.com/how-to-use-javascript-libraries-in-angular-2-apps-ff274ba601af
我一直在探索用于 RSA 加密和解密的各种 JavaScript 库,并发现了一个 JSEncrypt,位于https://cdnjs.cloudflare.com/ajax/libs/jsencrypt/2.3.1/jsencrypt.min.js。
然而,我在尝试理解该库用于 RSA 加密和解密的逻辑和操作时遇到了很大的困难。主要是,
我无法找到这个库的任何像样的文档,而且我无法理解丝毫的源代码。任何帮助是极大的赞赏。
jsencrypt ×6
encryption ×3
rsa ×3
typescript ×2
angular ×1
bouncycastle ×1
cryptojs ×1
import ×1
java ×1
javascript ×1
node-crypto ×1
node.js ×1
padding ×1
php ×1
php-openssl ×1
pycrypto ×1
python ×1
salt ×1