解密未正确进行 [英] Decrypting does not happen correctly

查看:69
本文介绍了解密未正确进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将XML文件作为字节数组导入到项目中

I import an XML file as a byte array to the project

RandomAccessFile rnd = new RandomAccessFile(filePath, "r");
byte[] fileData = new byte[(int) rnd.length()];
rnd.read(fileData);

我使用java.crypto对数组进行了加密

I encrypted the array using java.crypto

Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");

byte[] encypted = new byte[cipher.getOutputSize(fileData.length)];
int len = cipher.update(fileData, 0, fileData.length, encypted, 0);
len += cipher.doFinal(encypted, len);

当我解密字节数组并使用打印时

When I decrypt the byte array and print it using

System.out.println(new String(decrypted, "UTF-8"));

我得到了XML文件,但是末尾有一些未知的字符(它们仅在末尾).有什么办法可以删除这个?

I got the XML file but there were some unkown characters at the end (they are only at the end). Is there any way I can remove this?

预先感谢

推荐答案

看到类似的问题在这里,尽管答案可能与您的情况非常相关:

see a similar question here, the answer though is what might be very relavent to your situation:

1.如果您不知道加密使用了哪些填充,请使用设置为无填充".它将解密所有内容,包括填充,并且不会因填充不匹配而引发错误.

1.If you don't know what padding was used to encrypt, then decrypt with 'no padding' set. That will decrypt everything, including the padding, and won't throw an error because of mismatched padding.

2.解密密文后,看看最后一块输出,查看使用了什么填充.不同的填充物离开不同的字节模式,因此通常很容易分辨出来.

2.When you have decrypted the cyphertext, have a look at the last block of the output and see what padding was used. Different paddings leave different byte patterns, so it is usually easy enough to tell.

3.设置解密方法以期望正确的填充类型,并它会自动为您删除.

3.Set your decryption method to expect the correct type of padding, and it will be automatically removed for you.

以防万一,这是有关填充图案等的链接:填充维基百科

and incase this is relavant to you here is a link on padding patterns etc: Padding Wikipedia

这也是使用DES加密的很好的教程: http://www.exampledepot.com/egs/javax.crypto/desstring.html ,这里是DES中的PBE:具有HMAC(SHA1)和DES(EDE)的PBE

This over here is also a very good tutorial for encrypting using DES: http://www.exampledepot.com/egs/javax.crypto/desstring.html and here is for PBE in DES: PBE with HMAC(SHA1) and DES(EDE)

这篇关于解密未正确进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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