Ho implementato il seguente programma usando DES
[code]
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.*;
public class DES {
SecretKey key;
Cipher cipher;
//Cipher decipher;
public DES() {
try{
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56);
key = keyGenerator.generateKey();
cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// decipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
System.out.println(cipher.getProvider().getInfo() );
System.out.println(key.toString());
//decipher.init(Cipher.DECRYPT_MODE, key);
}catch (NoSuchAlgorithmException e){
}catch (NoSuchPaddingException e){
}catch (InvalidKeyException e){
}
}
public byte[] Encrypt(byte[] Message){
byte[] enc = {};
try{
enc = cipher.doFinal(Message);
}catch (IllegalBlockSizeException e){
System.out.println("IllegalBlockSizeException");
}catch (BadPaddingException e){
System.out.println("BadPaddingException");
}finally{
return enc;
}
}
public byte[] Decrypt(byte[] cipherText){
byte[] plainText = {};
try{
cipher.init(Cipher.DECRYPT_MODE, key);
plainText = cipher.doFinal(cipherText);
}catch (IllegalBlockSizeException e){
System.out.println("IllegalBlockSizeException");
}catch (BadPaddingException e){
System.out.println("BadPaddingException");
}finally{
return plainText;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DES des = new DES();
String Message;
byte[] cipherText;
Message = "Testo da cifrare";
try{
byte[] plaintext = Message.getBytes("UTF8");
cipherText = des.Encrypt(plaintext);
System.out.println("cipherText : " + cipherText);
plaintext = des.Decrypt(cipherText);
System.out.println("PlainText : " + plaintext);
}catch(UnsupportedEncodingException e){
}
}
}
[/ code]
ottengo il seguente output
Ho sostanzialmente 3 cose da chiarireSunJCE Provider (implements RSA, DES, Triple DES, AES, Blowfish, ARCFOUR, RC2, PBE, Diffie-Hellman, HMAC)
com.sun.crypto.provider.DESKey@fffe7b5f
cipherText : [B@65b4fad5
PlainText : [B@4cbfea1d
1)Il risultato è sbagliato (non so se cifra correttamente, ma senz'altro non restituisce il testo in chiaro corretto
2)Perchè se il codice in chiaro è di 16 caratteri, quando stampo il cifrato sono meno (11)?
3)La chiave (seconda riga dell'output) non dovrebbe essere di 56 bit?

Rispondi quotando