我想用enter image description here对JPG文件进行加密,但是我不知道如何用javascript解密。谁有更好的主意?这是我的java代码。
`private static void EncFile(File srcFile, File encFile) throws Exception
{
if(!srcFile.exists()){
System.out.println("source file not exixt");
return;
}//
if(!encFile.exists()){
System.out.println("encrypt file created");
encFile.createNewFile();
}
byte[] bytes = new byte[1024*8];
String key = "1234567812345678";
String iv = "1234567812345678";
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
InputStream fis = new FileInputStream(srcFile);
// CipherInputStream cin = new CipherInputStream(fis, cipher);
OutputStream fos = new FileOutputStream(encFile);
while ((dataOfFile = fis.read(bytes)) >0) {
byte[] encrypted = cipher.doFinal(bytes);
fos.write(encrypted,0,dataOfFile);
}
fis.close();
fos.flush();
fos.close();
}`这是我的javascipt代码,我使用了CryptoJS和Decrypt
var key = CryptoJS.enc.Latin1.parse('1234567812345678');
var iv = CryptoJS.enc.Latin1.parse('1234567812345678');
var url = "http://192.168.0.103/show3d_test/test/CR4/E1234.png";
var xhr = new XMLHttpRequest();
xhr.open("GET",url,true);
xhr.responseType = "arraybuffer";
xhr.onload = function() {
if(xhr.readyState ==4){
if (xhr.status == 200){
process(xhr.response,key);
}
}
}
xhr.send();
function process(buffer,key) {
var view = new Uint8Array(buffer);
var contentWA = CryptoJS.enc.u8array.parse(view);
var dcBase64String = contentWA.toString(CryptoJS.enc.Base64);
var decrypted = CryptoJS.AES.decrypt(dcBase64String,key,
{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.NoPadding});
var d64 = decrypted.toString(CryptoJS.enc.Base64);
var img = new Image;
img.src = "data:image/png;base64,"+d64;
document.body.append(img);
} `有人知道怎么做吗?我见过很多关于CryptoJS - Java加密/解密的例子,但大多数都使用硬编码的IV/key,或者只是从cryptoJS端向Java端发送IV/key。我只有一个密码短语,就像这个网站做的一样!
发布于 2017-08-31 01:13:48
不确定确切的问题是什么,但这应该会有所帮助。
IV的一种通用且安全的方法是创建一个带有CSPRNG (随机字节)的IV,并在加密数据前加上IV,这样它就可以用于解密。静脉注射不需要保密。
当使用“AES/CBC/无填充”时,输入必须是块大小的精确倍数(对于AES为16字节)。通常,会指定PKCS#7 (PKCS#5)填充,因此对要加密的数据长度没有限制。
https://stackoverflow.com/questions/45965525
复制相似问题