其实,我搜索很多,从互联网和计算器也为这个,
在我的加密和解密起初我没用过填充,
不过,最后我从这里的解决方案
http://stackoverflow.com/a/10775577/1115788
和我更新了我的code与填充的AES / CBC / PKCS5Padding 而同样的错误来了,最后一块未解密...
我在这方面的工作的最后两天,但没有找到解决办法
我Crypter code:
包mani.droid.browsedropbox;
进口java.io.FileInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口java.math.BigInteger的;
进口java.security.InvalidAlgorithmParameterException;
进口java.security.InvalidKeyException;
进口java.security.NoSuchAlgorithmException;
进口javax.crypto.Cipher中;
进口javax.crypto.CipherInputStream中;
进口javax.crypto.CipherOutputStream中;
进口javax.crypto.NoSuchPaddingException;
进口javax.crypto.SecretKey;
进口javax.crypto.spec.IvParameterSpec;
进口javax.crypto.spec.SecretKeySpec;
公共类Crypter {
密码加密器;
密码破译;
CipherInputStream顺;
CipherOutputStream COS;
的FileInputStream FIS;
byte []的ivbytes =新的字节[] {(字节)'A',(字节),B,(字节)'C',(字节)'D',(字节)'E',(字节)'F (字节),'G',(字节)'H',(字节)'我',(字节)'J',(字节)'K'(字节),L,(字节),M (字节)'N',(字节)的O,(字节)'P'};
IvParameterSpec IV =新IvParameterSpec(ivbytes);
公共布尔加密(字符串键,InputStream的是,OutputStream的OS)
{
尝试 {
byte []的EN codeD =新的BigInteger(键,16).toByteArray();
SecretKey的seckey =新SecretKeySpec(EN codeD,AES);
加密器= Cipher.getInstance(AES / CBC / PKCS7Padding);
encipher.init(Cipher.ENCRYPT_MODE,seckey,ⅳ);
顺=新CipherInputStream(是,加密器);
copyByte(顺,OS);
返回true;
}
赶上(InvalidKeyException将E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(抛出:NoSuchAlgorithmException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(NoSuchPaddingException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(InvalidAlgorithmParameterException使用E){
// TODO自动生成的catch块
e.printStackTrace();
}
返回false;
}
公共布尔解密(字符串键,InputStream的是,OutputStream的OS)
{
尝试 {
byte []的EN codeD =新的BigInteger(键,16).toByteArray();
SecretKey的seckey =新SecretKeySpec(EN codeD,AES);
加密器= Cipher.getInstance(AES / CBC / PKCS7Padding);
encipher.init(Cipher.DECRYPT_MODE,seckey,ⅳ);
COS =新CipherOutputStream(OS,加密器);
copyByte(是,COS);
//cos.close();
返回true;
}
赶上(InvalidKeyException将E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(抛出:NoSuchAlgorithmException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(NoSuchPaddingException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(InvalidAlgorithmParameterException使用E){
// TODO自动生成的catch块
e.printStackTrace();
}
返回false;
}
公共无效copyByte(InputStream的是,OutputStream的操作系统)抛出IOException异常
{
byte []的BUF =新的字节[8192];
INT的numBytes;
而((的numBytes = is.read(BUF))!= -1)
{
os.write(BUF,0,的numBytes);
os.flush();
}
os.close();
is.close();
}
}
解决方案
最后我得到的答案我自己的问题,与试错
其实这里的冲突是我在:编码= Cipher.getInstance(AES / CBC / PKCS7Padding)设置填充;
和第四组的一些价值观.....
最后,我得到了答案才刚刚更换了算法
从:
AES / CBC / PKCS7Paddinng
要:
AES / CFB8 / NoPadding
和它的工作就像魅力......,所以我建议这个答案的人谁这个问题苦苦挣扎,如果你解决你的问题,在这里提到的其他人...
Actually, I searched lot from internet and in stackoverflow too for this,
Initially I don't used padding in my encryption and decryption,
But Finally I got solution from here
http://stackoverflow.com/a/10775577/1115788
and I updated my code with padding as AES/CBC/PKCS5Padding and the same error is coming, and last block is not decrypted...
I'm working on this for last two day, but no solution found
my Crypter Code:
package mani.droid.browsedropbox;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class Crypter {
Cipher encipher;
Cipher decipher;
CipherInputStream cis;
CipherOutputStream cos;
FileInputStream fis;
byte[] ivbytes = new byte[]{(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', (byte)'o', (byte)'p'};
IvParameterSpec iv = new IvParameterSpec(ivbytes);
public boolean enCrypt(String key, InputStream is, OutputStream os)
{
try {
byte[] encoded = new BigInteger(key, 16).toByteArray();
SecretKey seckey = new SecretKeySpec(encoded, "AES");
encipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
encipher.init(Cipher.ENCRYPT_MODE, seckey, iv);
cis = new CipherInputStream(is, encipher);
copyByte(cis, os);
return true;
}
catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public boolean deCrypt(String key, InputStream is, OutputStream os)
{
try {
byte[] encoded = new BigInteger(key, 16).toByteArray();
SecretKey seckey = new SecretKeySpec(encoded, "AES");
encipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
encipher.init(Cipher.DECRYPT_MODE, seckey, iv);
cos = new CipherOutputStream(os, encipher);
copyByte(is, cos);
//cos.close();
return true;
}
catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public void copyByte(InputStream is, OutputStream os) throws IOException
{
byte[] buf = new byte[8192];
int numbytes;
while((numbytes = is.read(buf)) != -1)
{
os.write(buf, 0, numbytes);
os.flush();
}
os.close();
is.close();
}
}
解决方案
Finally I got answer for my own question, with trial and error
Actually here Conflict is I set Padding in encipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
and Set IV with some values.....,
Finally I got Answer only just replaced the Algorithm
From:
AES/CBC/PKCS7Paddinng
To:
AES/CFB8/NoPadding
and its worked like charm...., So I suggest this answer for others who struggling with this problem, if you solved you problem, mention here for others...
上一篇:使用 JAX-RS (Jersey) 和 @RolesAllowed 自定义 HTTP 状态响应自定义、状态、RS、JAX
下一篇:如何使用 Jersey 客户端对启用 JAAS 的 Web 服务器进行身份验证?如何使用、身份验证、客户端、服务器