Io per cryptare una chiave o una stringa uso questa mia classe di utilità
codice:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class SecurityUtils {
/**
* Applica l'encoding64 ad un array di byte
* @param bytes
* @return
*/
public static String encode64(byte[] bytes){
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(bytes);
return base64;
}
/**
* Applica il decode64 al messaggio
* @param mess
* @return
* @throws IOException
*/
public static byte[] decode64(String mess) throws IOException{
BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(mess);
}
public static Key genKey(String string) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
DESKeySpec des=new DESKeySpec(string.getBytes());
SecretKeyFactory secret=SecretKeyFactory.getInstance("DES");
return secret.generateSecret(des);
}
/**
* Genera una chiave randomica con l'algoritmo DES
*
* @return
* @throws NoSuchAlgorithmException
*/
public static Key genKey() throws NoSuchAlgorithmException{
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key;
KeyGenerator generator= KeyGenerator.getInstance("DES");
generator.init(new SecureRandom());
key=generator.generateKey();
return key;
}
/**
* Consente di criptare una Key
* @param toCrypt La chiave da Criptare
* @param source La chiave con cui criptare
* @return String La chiave criptata sottoforma di stringa
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
*/
public static String cryptKey(Key toCrypt,Key source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException{
Cipher cipher=Cipher.getInstance("DES");
cipher.init(Cipher.WRAP_MODE,source);
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(cipher.wrap(toCrypt));
return base64;
}
/**
* Decritta una chiave a partire dai byte criptati
* @param toDecrypt La chiave da decrittare
* @param source La chiave con la quale decrittare
* @return la Key decrittata
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IOException
*/
public static Key decryptKey(String toDecrypt,Key source) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException{
BASE64Decoder decoder = new BASE64Decoder();
byte[] raw = decoder.decodeBuffer(toDecrypt);
Cipher cipher=Cipher.getInstance("DES");
cipher.init(Cipher.UNWRAP_MODE,source);
//return cipher.unwrap(toDecrypt,"DES",Cipher.SECRET_KEY);
return cipher.unwrap(raw,"DES",Cipher.SECRET_KEY);
}
/**
* Decripta una stringa a partire da una chiave privata Key
* @param key
* @param mess
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws IOException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String decrypt(Key key, String mess) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher=Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE,key);
BASE64Decoder decoder = new BASE64Decoder();
byte[] raw = decoder.decodeBuffer(mess);
byte[] stringBytes = cipher.doFinal(raw);
return new String(stringBytes,"UTF8");
}
/**
* Cripta una stringa usando una chiave privata Key
* @param key
* @param mess
* @return
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws UnsupportedEncodingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String crypt(Key key,String mess) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException{
Cipher cipher=Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] stringBytes=mess.getBytes("UTF8");
byte[] raw=cipher.doFinal(stringBytes);
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(raw);
return base64;
}
}