openssl java aes_请问如何使用AES对使用OpenSSL命令加密的Java文件进行解密?
以下是OpenSSLPBEInputStream和OpenSSLPBEOutputStream它可以用于以与OpenSSL兼容的方式加密/解密任意字节流。
示例用法:// The original clear text bytes
byte[] originalBytes = ...
// Encrypt these bytes
char[] pwd = "thePassword".toCharArray();
ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
OpenSSLPBEOutputStream encOS = new OpenSSLPBEOutputStream(byteOS, ALGORITHM, 1, pwd);
encOS.write(originalBytes);
encOS.flush();
byte[] encryptedBytes = byteOS.toByteArray();
// Decrypt the bytes
ByteArrayInputStream byteIS = new ByteArrayInputStream(encryptedBytes);
OpenSSLPBEInputStream encIS = new OpenSSLPBEInputStream(byteIS, ALGORITHM, 1, pwd);
其中算法(只使用JDK类)可以是:“PBEWithMD5AndDES”、“PBEWithMD5AndTripleDES”、“PBEWithSHA1AndDESede”、“PBEWithSHA1AndRC2_40”。
要处理原始海报的password.txt-out password.txt.enc中的“OpenSSL AES-256-CBC-a-盐类”,在类路径中添加弹跳城堡,并使用算法=“PBEWITMD5AND256BITAES-CBC-OpenSSL”。/* Add BC provider, and fail fast if BC provider is not in classpath for some reason */
Security.addProvider(new BouncyCastleProvider());
依赖性:
org.bouncycastle
bcprov-jdk16
1.44
输入流:import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
public class OpenSSLPBEInputStream extends InputStream {
private final static int READ_BLOCK_SIZE = 64 * 1024;
private final Cipher cipher;
private final InputStream inStream;
private final byte[] bufferCipher = new byte[READ_BLOCK_SIZE];
private byte[] bufferClear = null;
private int index = Integer.MAX_VALUE;
private int maxIndex = 0;
public OpenSSLPBEInputStream(final InputStream streamIn, String algIn, int iterationCount, char[] password)
throws IOException {
this.inStream = streamIn;
try {
byte[] salt = readSalt();
cipher = OpenSSLPBECommon.initializeCipher(password, salt, Cipher.DECRYPT_MODE, algIn, iterationCount);
} catch (InvalidKeySpecException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new IOException(e);
}
}
@Override
public int available() throws IOException {
return inStream.available();
}
@Override
public int read() throws IOException {
if (index > maxIndex) {
index = 0;
int read = inStream.read(bufferCipher);
if (read != -1) {
bufferClear = cipher.update(bufferCipher, 0, read);
}
if (read == -1 || bufferClear == null || bufferClear.length == 0) {
try {
bufferClear = cipher.doFinal();
} catch (IllegalBlockSizeException | BadPaddingException e) {
bufferClear = null;
}
}
if (bufferClear == null || bufferClear.length == 0) {
return -1;
}
maxIndex = bufferClear.length - 1;
}
return bufferClear[index++] & 0xff;
}
private byte[] readSalt() throws IOException {
byte[] headerBytes = new byte[OpenSSLPBECommon.OPENSSL_HEADER_STRING.length()];
inStream.read(headerBytes);
String headerString = new String(headerBytes, OpenSSLPBECommon.OPENSSL_HEADER_ENCODE);
if (!OpenSSLPBECommon.OPENSSL_HEADER_STRING.equals(headerString)) {
throw new IOException("unexpected file header " + headerString);
}
byte[] salt = new byte[OpenSSLPBECommon.SALT_SIZE_BYTES];
inStream.read(salt);
return salt;
}
}
输出流:import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
public class OpenSSLPBEOutputStream extends OutputStream {
private static final int BUFFER_SIZE = 5 * 1024 * 1024;
private final Cipher cipher;
private final OutputStream outStream;
private final byte[] buffer = new byte[BUFFER_SIZE];
private int bufferIndex = 0;
public OpenSSLPBEOutputStream(final OutputStream outputStream, String algIn, int iterationCount,
char[] password) throws IOException {
outStream = outputStream;
try {
/* Create and use a random SALT for each instance of this output stream. */
byte[] salt = new byte[PBECommon.SALT_SIZE_BYTES];
new SecureRandom().nextBytes(salt);
cipher = OpenSSLPBECommon.initializeCipher(password, salt, Cipher.ENCRYPT_MODE, algIn, iterationCount);
/* Write header */
writeHeader(salt);
} catch (InvalidKeySpecException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new IOException(e);
}
}
@Override
public void write(int b) throws IOException {
buffer[bufferIndex] = (byte) b;
bufferIndex++;
if (bufferIndex == BUFFER_SIZE) {
byte[] result = cipher.update(buffer, 0, bufferIndex);
outStream.write(result);
bufferIndex = 0;
}
}
@Override
public void flush() throws IOException {
if (bufferIndex > 0) {
byte[] result;
try {
result = cipher.doFinal(buffer, 0, bufferIndex);
outStream.write(result);
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new IOException(e);
}
bufferIndex = 0;
}
}
@Override
public void close() throws IOException {
flush();
outStream.close();
}
private void writeHeader(byte[] salt) throws IOException {
outStream.write(OpenSSLPBECommon.OPENSSL_HEADER_STRING.getBytes(OpenSSLPBECommon.OPENSSL_HEADER_ENCODE));
outStream.write(salt);
}
}
普通小班:import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
class OpenSSLPBECommon {
protected static final int SALT_SIZE_BYTES = 8;
protected static final String OPENSSL_HEADER_STRING = "Salted__";
protected static final String OPENSSL_HEADER_ENCODE = "ASCII";
protected static Cipher initializeCipher(char[] password, byte[] salt, int cipherMode,
final String algorithm, int iterationCount) throws NoSuchAlgorithmException, InvalidKeySpecException,
InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException {
PBEKeySpec keySpec = new PBEKeySpec(password);
SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
SecretKey key = factory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(cipherMode, key, new PBEParameterSpec(salt, iterationCount));
return cipher;
}
}
openssl java aes_请问如何使用AES对使用OpenSSL命令加密的Java文件进行解密?相关推荐
- java忍者神龟,IT忍者神龟之命令行执行java程序
} 1.编译:cmd--cd到c:/temp/bin javac GetGreeting.java 生成GetGreeting.class文件 2.执行:java Greeting 打印出Hello ...
- java实现对文件加解密操作
源文件: 加密后的文件: 解密后的文件: package com.gblfy.test;import java.io.*;/*** java 实现对文件加解密的方法** @author gblfy* ...
- SM2加密解决java与iOS端加解密不配套问题
SM2加密解决java与iOS端加解密不配套问题 问题描述 问题判定 代码 声明 问题描述 使用java开发的SM2加解密,由java层生成SM2公私钥,iOS.Android(因和java层一致,暂 ...
- openssl java aes_Java AES算法和OpenSSL配对
近日工作上的原因,需要实现Java AES算法和C语言下基于OpenSSL的AES 算法通信.这是个老问题了,网上搜到不少资料,但都不是很详细,没能解决问题.只能自己来了. 先说说AES算法.AES ...
- [译] 最佳安全实践:在 Java 和 Android 中使用 AES 进行对称加密
原文地址:Security Best Practices: Symmetric Encryption with AES in Java and Android 最佳安全实践:在 Java 和 Andr ...
- javascript 解密_Javascript中的AES加密和Java中的解密
javascript 解密 AES代表高级加密系统,它是一种对称加密算法,很多时候我们需要在客户端加密一些纯文本,例如密码,然后将其发送到服务器,然后由服务器解密以进行进一步处理.AES加密和解密更加 ...
- Javascript中的AES加密和Java中的解密
AES代表高级加密系统,它是一种对称加密算法,很多时候我们需要在客户端加密一些纯文本(例如密码)并将其发送到服务器,然后由服务器解密以进行进一步处理.AES加密和解密更加容易在相同的平台(例如Andr ...
- C++使用openssl实现aes加解密,其中加密是string到文件,解密是文件到string,切合项目背景
代码 使用md5对于用户输入的密码进行保护,也使得密码的长度固定 crypto_util.h #pragma once#include <string>namespace hsm{ nam ...
- java aes 模式_Java实现多种加密模式的AES算法-总有一种你用的着!
前言 对于AES算法,相信很多程序员小伙伴都听过.用过,其原理本文就不介绍了,而是讲讲在实际项目中的应用.前几天,项目需要跟乙方接口对接,乙方提供加密后的信息串,而我这边负责对加密串进行解密.其中加解 ...
最新文章
- akaze特征匹配怎么去掉不合适的点_图像匹配几种常见算法与实践
- 20个开源项目托管站点推荐
- 两种解法-树形dp+二分+单调队列(或RMQ)-hdu-4123-Bob’s Race
- python控制窗口缩放
- gearman mysql持久化_gearman + mysql方式实现持久化操作示例
- 21世纪的设计模式:抽象工厂模式
- Java事实让您大吃一惊! (信息图)
- js DOM——JS学习笔记2015-7-2(第73天)
- php linux 删除文件夹,linux下如何删除文件夹
- qq浏览器极速版_安卓手机QQ轻聊版大升级,极速版正式上线:无广告/省内存
- 百度BCC云解析配置(新旧文档对比) - (文档篇)
- 在SQL Server中插入IN-T-SQL语句
- 钉钉一行代码_利用Python快速搭建钉钉和邮件数据推送系统
- Java中构造方法的案例及常用类int lenght类的使用方法举例,new实例化对象方法,静态方法实例方法的举例
- oracle wallet使用与维护
- AD19 编译报错:has only one pin 和 floating net label
- 1.2帮助软件Rstudio的下载与安装
- 联想服务器安装系统蓝屏怎么办,联想笔记本u盘装系统出现蓝屏怎么解决
- SQL server-数据库的查询(高级)
- java类编来那个初始化顺序_java类的初始化顺序
热门文章
- 商标45类分类表明细表_2019版注册商标分类表,商标注册45类范围明细
- java安全(五)java反序列化
- 计算机网络讨论4,计算机网络实验四
- python垃圾回收机制为什么标记能解决循环引用问题_python 关于循环引用以及标记清除的问题...
- win7无法连接打印机拒绝访问_如何解决局域网无法访问SQL Server 2008 无法连接到(local)...
- Java集合:Map集合
- Qt与QML的枚举绑定(C++枚举)
- c#随机数的产生与输出【C#】
- apply()与call()的区别
- DevOps平台中的自动化部署框架设计