import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.*;
import sun.misc.*;
public class Cryptex {
private static final int ITERATIONS = 1000;
public static void main
(String args
[]) { String test
= encrypt
("password".
toCharArray(),
"quelsiasi testo quelsiasi testo qualsiasi testo"); System.
out.
println(decrypt
("password".
toCharArray(), test
)); }
public static String encrypt
(char[] password,
String plaintext
) {
byte[] salt = new byte[8];
random.nextBytes(salt);
PBEKeySpec keySpec = new PBEKeySpec(password);
try{
BASE64Encoder encoder = new BASE64Encoder();
SecretKeyFactory keyFactory =SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec =new PBEParameterSpec(salt, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
byte[] ciphertext = cipher.doFinal(plaintext.getBytes("UTF8"));
return encoder.encode(salt) + encoder.encode(ciphertext);
}
System.
out.
println("Algoritmo non supportato"); }
catch (NoSuchPaddingException e2) {
System.
out.
println("Padding non supportato"); }
System.
out.
println("Specificazione della chiave non valida"); }
System.
out.
println("Chiave non valida"); }
System.
out.
println("Parametro non valido"); }
catch (IllegalBlockSizeException e6) {
System.
out.
println("Dimensione blocco illegale"); }
catch (BadPaddingException e7) {
System.
out.
println("Bad Padding"); }
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Error";
}
public static String decrypt
(char[] password,
String input
) {
try{
BASE64Decoder decoder = new BASE64Decoder();
String salt64
= input.
substring(0, 12
); String ciphertext64
= input.
substring( 12
); byte[] salt = decoder.decodeBuffer(salt64);
byte[] ciphertext = decoder.decodeBuffer(ciphertext64);
PBEKeySpec keySpec = new PBEKeySpec(password);
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec =
new PBEParameterSpec(salt, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
byte[] plaintextArray = cipher.doFinal(ciphertext);
return new String(plaintextArray
); System.
out.
println("Algoritmo non supportato"); }
catch (NoSuchPaddingException e2) {
System.
out.
println("Padding non supportato"); }
System.
out.
println("Specificazione della chiave non valida"); }
System.
out.
println("Chiave non valida"); }
System.
out.
println("Parametro non valido"); }
catch (IllegalBlockSizeException e6) {
System.
out.
println("Dimensione blocco illegale"); }
catch (BadPaddingException e7) {
System.
out.
println("Bad Padding"); }
e.printStackTrace();
}
return "Decifratura Fallita";
}
}
/*
String esito,risultato;
String pwd= null;
System.out.println("INSERIRE PASSWORD = "+"/n");
try{
while ( (pwd=stdIn.readLine()) != null){
try{
esito = inSock.readUTF();
risultato=Cryptex.decrypt(pwd.toCharArray(),esito);
System.out.println("Esito esame: " + risultato);
// chiudo la socket in downstream
socket.shutdownInput();
System.out.println("Terminata la chiusura della socket: " + socket);
*/