J
jim
Hello All,
The following is the code I wrote to do DES encryption.
public byte[] run ()throws Exception
{
// TODO Auto-generated method stub
byte[] re = null;
byte[]re1 = null;
if (key.length != 8){
throw new DesException("Invalid Key!!!");
}
KeyGenerator kg = KeyGenerator.getInstance("DES");
SecretKey sk = kg.generateKey();
SecretKeySpec desKey = new SecretKeySpec(key, "DES");
Cipher des = Cipher.getInstance("DES");
if ((comm == Cipher.DECRYPT_MODE) && (data.length != 8)){
throw new DesException("Invalid Key!!!");
}
des.init(Cipher.ENCRYPT_MODE, desKey);
int l = des.getOutputSize(8);
re = des.doFinal(data);
des.init(Cipher.DECRYPT_MODE, desKey);
l = des.getOutputSize(16);
re1 = des.doFinal(re);
return re;
}
I don't understand why I try entrypt 8 bytes data, the the length of
return value is 16 bytes, the first 8 bytes are what I want, what are
last 8 bytes? as far as I know, for des, the input block is 8 bytes,
output block is 8 bytes too. I use this return value to decrypt, I got
correct value.
If I just input first 8bytes to decrypt I got exception.
Anyone can explain this to me or recommend any detail document about
these API?
Thanks a lot.
The following is the code I wrote to do DES encryption.
public byte[] run ()throws Exception
{
// TODO Auto-generated method stub
byte[] re = null;
byte[]re1 = null;
if (key.length != 8){
throw new DesException("Invalid Key!!!");
}
KeyGenerator kg = KeyGenerator.getInstance("DES");
SecretKey sk = kg.generateKey();
SecretKeySpec desKey = new SecretKeySpec(key, "DES");
Cipher des = Cipher.getInstance("DES");
if ((comm == Cipher.DECRYPT_MODE) && (data.length != 8)){
throw new DesException("Invalid Key!!!");
}
des.init(Cipher.ENCRYPT_MODE, desKey);
int l = des.getOutputSize(8);
re = des.doFinal(data);
des.init(Cipher.DECRYPT_MODE, desKey);
l = des.getOutputSize(16);
re1 = des.doFinal(re);
return re;
}
I don't understand why I try entrypt 8 bytes data, the the length of
return value is 16 bytes, the first 8 bytes are what I want, what are
last 8 bytes? as far as I know, for des, the input block is 8 bytes,
output block is 8 bytes too. I use this return value to decrypt, I got
correct value.
If I just input first 8bytes to decrypt I got exception.
Anyone can explain this to me or recommend any detail document about
these API?
Thanks a lot.