使用已使用 java 编码的 openssl 解码数据
Decode data using openssl that has been encoded using java
我必须与用java编写的系统进行交互,该系统使用以下java方法加密数据:
public final void rsaEncrypt(String data, String filePath) {
try {
Base64.Encoder encoder = Base64.getEncoder();
PublicKey pubKey = readKeyFromFile("/" + Constants.PUBLIC_KEY_FILE_NAME, filePath);
Cipher cipher = Cipher.getInstance(Constants.RSA_INSTANCE);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherData = cipher.doFinal(data.getBytes("UTF-8"));
writeToFile(Constants.ENCRYPTED_STRING_FILE_NAME, filePath, encoder.encodeToString(cipherData));
} catch (BadPaddingException | InvalidKeyException | NoSuchPaddingException | NoSuchAlgorithmException
| IllegalBlockSizeException | UnsupportedEncodingException e) {
if (LOG.isErrorEnabled())
LOG.error("Error encrypting String.", e);
throw new EncryptionException("Error encrypting data", e);
}
}
我的代码是使用 openssl 用 c++ 编写的:
std::string prv =
"-----BEGIN RSA PRIVATE KEY-----n"
// cut key data
"-----END RSA PRIVATE KEY-----n";
BIO *bio = BIO_new_mem_buf((void*)prv.c_str(), -1);
RSA* rsaPrivKey = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
if (!rsaPrivKey)
printf("ERROR: Could not load PRIVATE KEY! PEM_read_bio_RSAPrivateKey FAILED: %sn", ERR_error_string(ERR_get_error(), NULL));
BIO_free(bio);
// where enc[] holds the Base64 decoded data, but len becomes -1 and no data is decoded.
int len = RSA_private_decrypt(64,(unsigned char *)&enc, (unsigned char *)&dec, private_key_, RSA_PKCS1_PADDING);
我可以解码我用公钥加密的数据,但似乎与 java 选项不匹配。
有什么建议吗?
这些在这里没有答案,就像我的评论所说,事实证明 Java 代码在创建它们的密钥时不正确,这些密钥与我得到的 .pem 文件不匹配。
相关文章:
- 无法解码base64+deflate数据
- 使用已使用 java 编码的 openssl 解码数据
- 在CRC-16 CCITT中将数据从二进制解码为文本,我应该输入一个码字,使用CRC生成器进行编码
- 如何将 MJPEG 解码为原始 RGB(或 YUV)数据
- 将数据包从C++服务器发送到NodeJs服务器时出现MessagePack解码错误
- 解码 c++ 中以 base64 编码的大量数据
- 获取GIF帧数,而无需解码所有数据
- 从串行端口(带整数的字符串)解码数据
- 使用 UDP 协议从 Windows 套接字发送到 Qt 套接字的网络数据包上的结构编码和解码
- 如何预先分配LibavCodec的内存来编写解码的帧数据
- 寻求使用 FFmpeg 在 mp4 容器中 h264 编解码器的视频帧.数据包 pts 始终为 0
- 如何解码非密钥ASN1数据
- 跨平台UTF-8字符文件数据编码/解码
- 当JPEG数据不正确时,libjpeg解码崩溃
- B64在数据不是3的倍数的情况下的编码/解码处理
- 解码像 FAST 这样的数据协议的快速方法是什么,其中数据以字节为单位编码,并将位作为存在标志
- FFMPEG:编码后直接解码数据包
- 如何使用Zxing C++解码数据
- Windows Media Foundation:获取 AAC 解码数据
- 使用C40技术解码数据矩阵