日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

openssl java aes_请问如何使用AES对使用OpenSSL命令加密的Java文件进行解密?

發布時間:2025/3/11 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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文件进行解密?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 高潮一区二区三区乱码 | 亚洲一卡二卡在线观看 | 日穴视频| 精品午夜一区二区三区在线观看 | 国产精品一区二区免费 | 九七av| 亚洲一区二区电影网 | 精品一区二区三区蜜桃 | 成人夜视频 | 天天干,天天操 | 国产另类xxxxhd高清 | 欧美亚洲综合久久 | 直接看的毛片 | 污视频网站在线播放 | 欧美热热 | 激情天堂网 | 四虎看黄 | 精品黑人一区二区三区在线观看 | 欧美日韩一级黄色片 | 男女污污视频在线观看 | 日本午夜一区 | xxxx国产片 | 国内露脸中年夫妇交换 | 男人的天堂在线 | 99国产精品欲 | 香蕉a视频| 日韩黄色视屏 | 综合色婷婷一区二区亚洲欧美国产 | 99思思 | 日韩在线第二页 | 中文字幕一区二区三区视频 | 国产成人精品一区二三区 | 国产精品porn | 日韩丰满少妇 | 狠狠干狠狠干狠狠干 | 久久精彩免费视频 | 欧美性受xxxxxx黑人xyx性爽 | 日本网站在线播放 | 无遮挡av| 久久九九精品视频 | 少妇与公做了夜伦理 | 久久人妻无码aⅴ毛片a片app | 国产网红在线观看 | 亚洲精品在线免费观看视频 | 成人黄色免费网站 | 视频二区中文字幕 | 5d肉蒲团之性战奶水 | 成人在线视频免费观看 | 久久精品国产视频 | 久在操| 国产一区二区免费视频 | 中文字幕一区二区三区av | 自拍偷拍国内 | 深夜精品| 久久久免费在线观看 | 欧美高清大白屁股ass18 | 欧美日b片| 青青伊人av | 91精品日韩| 特级西西444www大精品视频免费看 | 色香蕉在线视频 | 国产伦精品一区二区三区在线观看 | 无罩大乳的熟妇正在播放 | 日本国产精品 | 亚洲一区二区中文字幕 | 久久在线免费视频 | 色老二导航 | 日本不卡视频在线播放 | 国产真实偷伦视频 | аⅴ资源中文在线天堂 | 国产乡下妇女三片 | 一卡二卡在线观看 | 手机看片福利久久 | 羞羞的视频在线观看 | 都市激情亚洲一区 | 国产激情第一页 | 91看片在线观看 | 日韩精品黄| 午夜777| 97精品在线视频 | 亚洲a网| 一区二区成人av | 干干天天 | 亚洲午夜精品久久久久久浪潮 | 国产精品v日韩精品v在线观看 | 大黄毛片 | 日本在线观看网站 | 天天干天天操天天玩 | 亚洲精品影院 | 色香蕉视频 | 日韩一区二区视频在线 | 日韩网站免费观看高清 | 国产一级做a | 日韩高清影视在线观看 | 日夜夜操 | 青草久久久久 | 不卡一区二区三区四区 | 亚州黄色| 国产成人99|