Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Crypt e Decrypt...

  1. #1

    Crypt e Decrypt...

    Ciao a tutti e spero tanto che mi possiate aiutare.
    Sto realizzando una classe che faccia l'encrypt e la decrypt di una stringa passata in input con una chiave anche esse passata in input già in mio possesso (quindi non devo crearla o autogenerarla), con algoritmo TRIPLE-DES.
    Ma sto avendo un pò di problemi.
    Quando chiamo il metodo encrypt passandogli la stringa "ad esempio" mi codifica tale stringa, se faccio il passaggio inverso invece non ottengo lo stesso risultato di partenza.
    Questa è la classe che fa l'encrypt/decrypt:
    codice:
    import javax.crypto.Cipher; 
    import javax.crypto.spec.SecretKeySpec; 
    import javax.crypto.SecretKey;       
    public class Decrypt{     
        static final int    BLOCKSIZE   = 8;     
        static final char   PADDINGCHAR = ' ';     
        static final String TRIPLE_DES  = "DESede/ECB/NoPadding";     
        protected SecretKey key;       
    
        public Decrypt(String keycode) {         
            try {             
                 byte[] keyBytes =  keycode.getBytes();
                 key = new SecretKeySpec(keyBytes, "DESede");
             }
             catch (Exception e) {
                 e.printStackTrace();
                 System.exit(0);
            }
         }
    
         public String decrypt(String encryptText) throws Exception {
             String codec = "";
             String b64cipher;
             char dateControllor;
             int i=0;
    
             try {
                 b64cipher = encryptText;
                 codec = new String(cipher(b64cipher.getBytes(), Cipher.DECRYPT_MODE),"ISO-8859-1").trim();
                 return codec;
             }
             catch (Exception e) {
                 e.printStackTrace();
                 throw e;
             }
         }
    
         public String encrypt(String plainText) throws Exception {
              int remainder = plainText.length() % BLOCKSIZE;
              if (remainder != 0) {
                 int count = BLOCKSIZE - remainder;
                 for (int i = 0; i < count; i++) {
                     plainText += PADDINGCHAR;
                 }
             }
             byte[] text = plainText.getBytes();
             String ritorno = new String(cipher(text, Cipher.ENCRYPT_MODE),"ISO-8859-1");
             return ritorno;
         }
    
          private byte[] cipher(byte[] text, int mode) throws Exception {
             try {
                 byte[] cipherText;
                 Cipher aCipher = Cipher.getInstance(TRIPLE_DES);
                 aCipher.init(mode, key);
                 cipherText = aCipher.update(text);
                 return cipherText;
             }
             catch (Exception e) {
                 e.printStackTrace();
                 throw e;
             }
         }
     }
    Il main che lancia ciò è fatto così:
    codice:
    Decrypt dec = new Decrypt("CHIAVE DI 24 CARATTERI"); 
    String daCod = "Prova Prova Prova"; 
    String encr  = dec.encrypt(daCod); 
    System.out.println("\nCodifica = " + encr); 
    String dd = dec.decrypt(encr); 
    System.out.println("\nDecodifica = " + dd);
    E' corretto come sono state implementate le cose? In cosa sbaglio?

    Grazie e spero tanto in una vostra risposta.

  2. #2
    ma è normale che "CHIAVE DI 24 CARATTERI" sia in realtà di 22?
    a parte questo, dal momento che non posso provare il programma, non ho modo di aiutarti

  3. #3
    Originariamente inviato da google01
    ma è normale che "CHIAVE DI 24 CARATTERI" sia in realtà di 22?
    a parte questo, dal momento che non posso provare il programma, non ho modo di aiutarti
    E' per non mettere sul forum la vera chiave!

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.