等效于Java中的OpenSSL命令 [英] Equivalent to OpenSSL commands in java

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

问题描述

我对Java或openssl中的文件加密不是很熟悉.我知道学校的基础知识,但从未真正实施过.

现在,我已收到以下3条命令:

//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中完全复制它.有一个简单的方法可以做到这一点吗? 我有可以使用的图书馆吗?

对于第一行,我使用来自Java 7的SecureRandom函数生成一个字节数组,然后使用apache commons编解码器库将其加为base64.像这样:

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));

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

第二次,我需要使用提供的公共RSA密钥对包含上述脚本输出的文件进行加密.加密文件或文件中的数据有区别吗?实际文件会增加位吗?

但是第三个是困难的地方,因为它需要盐和aes-256-cbc加密标准.我希望有一个包含所有所需功能的库.如果是这样,谁能指出我正确的方向?我当时在读有关充气城堡的加密图书馆,但运气不好,找不到我需要的东西.

亲切的问候

解决方案

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

对于AES加密,请参阅 javax的Java文档.加密.

使用SecureRandom创建iv并将其添加到加密数据之前,它不需要是秘密的.解密时,只需从数据中选择iv即可用于iv.

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.

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)

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?

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.

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?

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.

kind regards

解决方案

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.

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

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天全站免登陆