我们有openssl C ++代码加密束消息。我们的目标是在Chrome浏览器中使用纯javascript解密它
C ++代码 私人的: EncryptUtil():_ isRight(true){ BIO * bio = BIO_new_mem_buf((void *)PUBLIC_CERT,sizeof(PUBLIC_CERT)); if(!bio){ _isRight = false; }
_publicKey = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
if ( ! _publicKey) {
_isRight = false;
}
_keyLen = RSA_size(_publicKey);
}
RSA* _publicKey;
bool _isRight;
size_t _keyLen;
static EncryptUtil* _EncryptUtilInst;
public :
static EncryptUtil* GetInstance() {
if ( ! _EncryptUtilInst) {
_EncryptUtilInst = new EncryptUtil();
}
return _EncryptUtilInst;
}
bool isRight() { return _isRight; }
bool encryptContent(const unsigned char* src, size_t nLen, std::vector<unsigned char>& dst)
{
unsigned char rsaRes[1024] = {0};
int res = RSA_public_encrypt(_keyLen, src, rsaRes, _publicKey, RSA_NO_PADDING);
dst.assign(rsaRes, rsaRes + res);
int size = dst.size();
return (res > 0);
}
};
EncryptUtil* EncryptUtil::_EncryptUtilInst = NULL;
bool encryptRSA(const unsigned char* src, size_t len, std::string& encryptionResult)
{
EncryptUtil* encryptUtil = EncryptUtil::GetInstance();
if ( ! encryptUtil || ! encryptUtil->isRight() ) {
return false;
}
std::vector<unsigned char> encryptContent;
if ( ! encryptUtil->encryptContent(src, len, encryptContent) )
return false;
return base64Encode(&encryptContent[0], encryptContent.size(), encryptionResult);
}
问题是在解码base64之后,它出现了二进制文件 https://github.com/travist/jsencrypt 不能拿二进制。 我试图将二进制文件更改为字符串,但它不会解密字符串
像这样的事情 let buff = new Buffer(window.atob(encryptedData)); var uncrypted = decrypt.decrypt(parseInt(buff,2).toString(10));任何想法,谢谢