使用Cipher类进行对字符串的加密解密,加密没有问题,解密的时候老是出现javax.crypto.IllegalBlockSizeException错误,请问怎么办啊?我粘的诺基亚网站上的代码。
好像要长是8的倍数。
但我不知道为什么
String str = new String(outputBytes, 0, 8);
inputBytes = str.getBytes();
inputBytes 的长度是7
代码如下:
// Object of this class provides the functionality for
// encryption and decryption.
Cipher cipher;
try {
// parameter "DES" specifies type of cipher we want to create
// through the factory method. It includes algorithm, mode and
// padding. You can define only algorithm and in that case default
// values will be used for mode and padding.
cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
} catch (NoSuchAlgorithmException ex) {
// requested cryptographic algorithm is not available
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (NoSuchPaddingException ex) {
// requested padding mechanism is not available in the environment.
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
}
// The lenght of keyString is 8. It's important characteristic
// for encryption
String keyString = "testtest";
byte[] keyData = keyString.getBytes();
// key object specifies a secret key
// it uses to construct the secret key for our cipher object from a byte
// array
SecretKeySpec key = new SecretKeySpec(keyData, 0, keyData.length, "DES");
try {
// initializing with setting up the type of operation it's to be
// used for. In this case - for encryption.
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException ex) {
// given key object is inappropriate for initializing this cipher
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
}
int cypheredBytes = 0;
printString("Source string: " + textToCrypting);
byte[] inputBytes = textToCrypting.getBytes();
byte[] outputBytes = new byte[100];
try {
// doFinal method encrypts data to outputBytes array.
// for unknown or big counts of data update method more recomended
// counts of crypted bytes saved inside cypheredBytes variable
cypheredBytes = cipher.doFinal(inputBytes, 0, inputBytes.length,
outputBytes, 0);
} catch (IllegalStateException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return; } catch (ShortBufferException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (IllegalBlockSizeException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (BadPaddingException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
}
String str = new String(outputBytes, 0, cypheredBytes);
inputBytes = str.getBytes();
printString("Crypted string:" + str);
try {
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (InvalidKeyException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
}
try {
// decrypts data
cypheredBytes = cipher.doFinal(inputBytes, 0, inputBytes.length,
outputBytes, 0);
} catch (IllegalStateException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (ShortBufferException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (IllegalBlockSizeException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (BadPaddingException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
}
str = new String(outputBytes, 0, cypheredBytes);
printString("Decrypted string:" + str);
printString(" Ok! " + str);
好像要长是8的倍数。
但我不知道为什么
String str = new String(outputBytes, 0, 8);
inputBytes = str.getBytes();
inputBytes 的长度是7
代码如下:
// Object of this class provides the functionality for
// encryption and decryption.
Cipher cipher;
try {
// parameter "DES" specifies type of cipher we want to create
// through the factory method. It includes algorithm, mode and
// padding. You can define only algorithm and in that case default
// values will be used for mode and padding.
cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
} catch (NoSuchAlgorithmException ex) {
// requested cryptographic algorithm is not available
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (NoSuchPaddingException ex) {
// requested padding mechanism is not available in the environment.
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
}
// The lenght of keyString is 8. It's important characteristic
// for encryption
String keyString = "testtest";
byte[] keyData = keyString.getBytes();
// key object specifies a secret key
// it uses to construct the secret key for our cipher object from a byte
// array
SecretKeySpec key = new SecretKeySpec(keyData, 0, keyData.length, "DES");
try {
// initializing with setting up the type of operation it's to be
// used for. In this case - for encryption.
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException ex) {
// given key object is inappropriate for initializing this cipher
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
}
int cypheredBytes = 0;
printString("Source string: " + textToCrypting);
byte[] inputBytes = textToCrypting.getBytes();
byte[] outputBytes = new byte[100];
try {
// doFinal method encrypts data to outputBytes array.
// for unknown or big counts of data update method more recomended
// counts of crypted bytes saved inside cypheredBytes variable
cypheredBytes = cipher.doFinal(inputBytes, 0, inputBytes.length,
outputBytes, 0);
} catch (IllegalStateException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return; } catch (ShortBufferException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (IllegalBlockSizeException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (BadPaddingException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
}
String str = new String(outputBytes, 0, cypheredBytes);
inputBytes = str.getBytes();
printString("Crypted string:" + str);
try {
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (InvalidKeyException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
}
try {
// decrypts data
cypheredBytes = cipher.doFinal(inputBytes, 0, inputBytes.length,
outputBytes, 0);
} catch (IllegalStateException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (ShortBufferException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (IllegalBlockSizeException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
} catch (BadPaddingException ex) {
printString(ex.toString());
printString("Error! Snippets execution stopped.");
return;
}
str = new String(outputBytes, 0, cypheredBytes);
printString("Decrypted string:" + str);
printString(" Ok! " + str);
cypheredBytes = cipher.doFinal(inputBytes, 0,inputBytes.length,outputBytes, 0);
这句话抛出的。原因是inputBytes.length为7。可我不知道该怎么解决啊
inputBytes之前已经定长了,重新申请一个byte[] inputBytes1 = new byte[cypheredBytes];
try {
str = new String(outputBytes, 0, cypheredBytes, "iso-8859-1");
inputBytes1 = str.getBytes("iso-8859-1");
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
str = new String(outputBytes, 0, cypheredBytes, "iso-8859-1");
inputBytes1 = str.getBytes("iso-8859-1");
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
这样就可以了,也不用再申请inputBytes了。也谢谢上面各位,结贴了