首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用openssl解码已使用java编码的数据。

使用openssl解码已使用java编码的数据。
EN

Stack Overflow用户
提问于 2018-10-25 02:04:41
回答 1查看 25关注 0票数 0

我必须与一个用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: %s\n", 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选项不匹配。

有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2018-10-25 03:19:29

这里没有答案,正如我的评论所说,事实证明Java代码在创建它们的键时是不正确的,这些键与我得到的.pem文件不匹配。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52975396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档