Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Sep 2006
    Messaggi
    5

    Crittografia...

    Sono alle prime armi con la crittografia (per dirla in altri termini sono piuttosto ignorante... ), stò cercando di fare un esercizio in cui devo creare una coppia di chiavi con RSA, generare una chiave TripleDES.
    Cifrare una stringa usando TripleDES, cifrare la chiave TripleDES con RSA, decifrare la chiave TripleDES (con RSA) decifrare la stringa.
    Ho provato a cifrare e decifrare la stringa con RSA e funziona, ma quando cifro la chiave TripleDES qualcosa non va...
    Il codice per cifrare e decifrare la chiave è questo...

    public byte[] crittaKeyRSA(PublicKey RSAPublicKey, Cipher RSACipher, Key key){
    byte[] dataEncoded=null;
    byte[] keyToBytes=null;
    try{
    RSACipher.init(Cipher.ENCRYPT_MODE, RSAPublicKey);
    }catch(InvalidKeyException inv){
    System.out.println("ERRORE!!! Chiave non valida!!!"+inv);
    }
    try{
    keyToBytes = key.getEncoded();
    }catch(Exception un){
    System.out.println("ERRORE!!!"+un);
    }
    try{
    dataEncoded = RSACipher.doFinal(keyToBytes);
    }catch(IllegalBlockSizeException err){
    System.out.println("ERRORE!!!"+err);
    }catch(BadPaddingException bpe){
    System.out.println("ERRORE!!!"+bpe);
    }
    return dataEncoded;
    }

    public Key deCrittaKeyRSA(PrivateKey RSAPrivateKey, Cipher RSACipher, byte[] dataEncoded){
    byte[] dataDecoded=null;
    Key output=null;
    try{
    RSACipher=cifrarioRSA();
    RSACipher.init(Cipher.DECRYPT_MODE, RSAPrivateKey);
    }catch(InvalidKeyException inv){
    System.out.println("ERRORE!!! La chiave non è valida"+inv);
    }
    try{
    dataDecoded=RSACipher.doFinal(dataEncoded);
    }catch(Exception e){
    System.out.println("ERRORE!!"+e);
    }
    try{
    SecretKey k = new SecretKeySpec(dataDecoded,"TripleDES");
    output=k;
    }catch(Exception e){
    System.out.println("ERRORE!!"+e);
    }
    return output;
    }

  2. #2
    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;
        }
       
    }
    Lang=Java
    Ambiente = Eclipse forever
    Ubuntu & Win XP Pro

  3. #3
    Utente di HTML.it
    Registrato dal
    Sep 2006
    Messaggi
    5

    Grazie 1000!!

    Appena avrò tempo cercherò di correggere il mio operato...
    Se avrò ancora problemi ti disturberò ancora...


    Intanto grazie 1000!!!
    Ciao Francesco

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.