相当于java中的OpenSSL命令 [英] Equivalent to OpenSSL commands in java

查看:42
本文介绍了相当于java中的OpenSSL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就此而言,我对用 java 或 openssl 加密文件不是很熟悉.我知道学校的基础知识,但从未真正实施过.

I'm not very familiar with encryption of files in java or openssl for that matter. I know the basics from school but have never actually implemented it.

现在我得到了以下 3 个命令:

Now I've been given the following 3 commands:

//generate random base64 private key
openssl rand -base64 32 -out (keypath)
//Encrypt random key with public key
openssl rsautl -encrypt -inkey (encryptionkey) -pubin -in (input) -out (output)
//encrypt file
openssl enc -aes-256-cbc -salt -in (input) -out (output) -pass file:(keypath)

我需要在java中完全复制它.有没有简单的方法来做到这一点?有没有我可以使用的库使这更容易?

I need to replicate this exactly in java. Is there an easy way of doing this? Are there a library i can use which makes this easier?

对于第一行,我使用 java 7 中的 SecureRandom 函数生成一个字节数组,然后我使用 apache commons 编解码器库将其重新编码到 base64.像这样:

For the first line I'm using the SecureRandom function from java 7 to generate a byte array which i then encore to base64 using apache commons codec library. like so:

byte[] bytes = new byte[32];
new SecureRandom().nextBytes(bytes);
String test = new String(Base64.encodeBase64(bytes));
test = test.substring(0, Math.min(test.length(), 10));

如果我没记错的话,这应该做同样的事情.

If i'm not mistaken this should do the same thing.

第二,我需要使用提供的公共 RSA 密钥加密包含上述脚本输出的文件.加密文件或文件内的数据有区别吗?意思是实际文件添加位吗?

For the second I need to encrypt the file containing the output from the above script using a provided public RSA key. Is there a difference between encrypting a file or the data inside the file? meaning does the actual file add bits?

然而,第三个是它变得困难的地方,因为它需要盐和 aes-256-cbc 加密标准.我希望有一个包含所有所需功能的库.如果是这样,有人能指出我正确的方向吗?我正在阅读有关充气城堡的加密库的信息,但没有找到我需要的东西.

The third however is where it gets hard since it needs a salt and the aes-256-cbc encryption standard. I'm hoping there is a library which encompasses all the functionality required. If so can anyone point me in the right direction? I was reading about bouncy castle's crypto library but haven't had much luck finding the things i need.

亲切的问候

推荐答案

随机字节数组代码很好,但为什么 Base64 对随机字节进行编码,这实际上只是显示可能只需要 ASCII 字符的用途所必需的.

The random byte array code is fine but why Base64 encode the random bytes, that is really only neccessary for display of perhaps use where only ASCII charactrers are required.

有关 AES 加密,请参阅 javax 的 Java 文档.加密.

For AES encryption see the Java docs for javax.crypto.

使用 SecureRandom 创建 iv 并将其添加到加密数据中,它不需要保密.解密时只需从数据中选择 iv 用于 iv.

Use SecureRandom to create the iv and prepend it to the encrypted data, it does not need to be secret. On decryption just pick the iv from the data to use for the iv.

这篇关于相当于java中的OpenSSL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆