Visualizzazione dei risultati da 1 a 8 su 8

Discussione: Criptare stringhe

  1. #1

    Criptare stringhe

    Salve. Esiste qualche metodo che consente di criptare e decriptare delle stringhe? Ad esempio, se io volessi salvare del testo su un file di testo, e vorrei rendere illeggibile questo testo... esiste qualcosa del genere? Altrimenti devo creare io un metodo :P

  2. #2
    Utente di HTML.it L'avatar di ziz
    Registrato dal
    Jun 2008
    Messaggi
    52
    Esistono vari algoritmi di criptaggio, come l'RSA, il DES, il 3DES, ecc...
    Il migliore, per quel che ne so io, è il 3DES, ma non mi pare sia supportato dall'apposita libreria java, quindi dovresti implementarlo tu...
    Comunque ti basta fare una googlata e li trovi già implementati!!
    Spero di esserti stato utile, ciao!!

  3. #3
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,254

    Re: Criptare stringhe

    Originariamente inviato da Dreamer89
    Esiste qualche metodo che consente di criptare e decriptare delle stringhe? Ad esempio, se io volessi salvare del testo su un file di testo, e vorrei rendere illeggibile questo testo... esiste qualcosa del genere? Altrimenti devo creare io un metodo :P
    Puoi usare le API per la crittografia, ad esempio gli stream crittografici CipherOutputStream/CipherInputStream (package javax.crypto).
    Non sono difficilissimi da usare (ma bisogna conoscere almeno qualcosina di base sulla crittografia), fai qualche ricerca in rete. Ti posso suggerire il libro "Java I/O" della O'Reilly, che spiega moltissime cose sull'I/O e anche l'uso di queste API (non completamente comunque ... è focalizzato sull'I/O, non sulla crittografia!).

    Chiaramente se usi queste API, quello che otterrai non è un file di "testo" ma un file "binario" ovviamente incomprensibile.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

  4. #4
    A me servirebbe semplicemente trasformare delle stringhe... credo che mi creerò un sistema io, grazie mille

  5. #5
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Io usavo questa classe:

    codice:
    import java.io.UnsupportedEncodingException;
    
    public class CodeEncodeString
        {
            private static final String[] DEFAULT_KEYS =
            {
            "01210ACB39201293948ABE4839201CDF",
            "123219843895AFDE3920291038103839",
            "89128912093908120983980981098309",
            "AABBCCDD019201920384383728298109"
            };
            
            private static boolean updatedProps = false;
            private static final char[] hexDigits =
            {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
            'D', 'E', 'F'
            };
            
            private byte[][] keys = null;
            
            public CodeEncodeString()
            {
                init(CodeEncodeString.DEFAULT_KEYS);
            }
            
            public CodeEncodeString(String[] keystrs)
            {
                init(keystrs);
            }
            
            private void init(String[] keystrs)
            {
                keys = new byte[keystrs.length][];
                for (int i = 0; i < keys.length; i++)
                {
                    keys[i] = fromString(keystrs[i]);
                }
            }
            
            private String toString(byte[] ba)
            {
                char[] buf = new char[ba.length * 2];
                int j = 0;
                int k;
                
                for (int i = 0; i < ba.length; i++)
                {
                    k = ba[i];
                    buf[j++] = hexDigits[(k >>> 4) & 0x0F];
                    buf[j++] = hexDigits[k & 0x0F];
                }
                
                return new String(buf);
            }
            
            private int fromDigit(char ch)
            {
                if (ch >= '0' && ch <= '9')
                {
                    return ch - '0';
                }
                if (ch >= 'A' && ch <= 'F')
                {
                    return ch - 'A' + 10;
                }
                if (ch >= 'a' && ch <= 'f')
                {
                    return ch - 'a' + 10;
                }
                
                throw new IllegalArgumentException("invalid hex digit '" + ch + "'");
            }
            
            private byte[] fromString(String hex)
            {
                int len = hex.length();
                byte[] buf = new byte[((len + 1) / 2)];
                
                int i = 0, j = 0;
                if ((len % 2) == 1)
                {
                    buf[j++] = (byte) fromDigit(hex.charAt(i++));
                }
                
                while (i < len)
                {
                    buf[j++] = (byte) ((fromDigit(hex.charAt(i++)) << 4) |
                                       fromDigit(hex.charAt(i++)));
                }
                
                return buf;
            }
            
            private byte encrypt(byte d, byte[] key)
            {
                byte e;
                
                e = d;
                for (int i = 0; i < key.length; i++)
                {
                    e = (byte) ((int) e ^ (int) key[i]);
                }
                
                return e;
            }
            
            private byte decrypt(byte e, byte[] key)
            {
                byte d;
                
                d = e;
                for (int i = key.length - 1; i >= 0; i--)
                {
                    d = (byte) ((int) d ^ (int) key[i]);
                }
                
                return d;
            }
            
            public String encrypt(String orig)
            {
                byte[] ect = null;
                int size;
                byte[] origBytes = null;
                
                try
                {
                    origBytes = orig.getBytes("UTF-8");
                }
                catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                    throw new RuntimeException(e.toString());
                }
                
                ect = new byte[origBytes.length];
                for (int i = 0; i < origBytes.length; i += keys.length)
                {
                    for (int j = 0; j < keys.length; j++)
                    {
                        if ((i + j) >= origBytes.length)
                        {
                            break;
                        }
                        else
                        {
                            ect[i + j] = encrypt(origBytes[i + j], keys[j]);
                        }
                    }
                }
                
                return toString(ect);
            }
            
            public String decrypt(String ectstr)
            {
                byte[] ect = null;
                int size;
                byte[] origBytes = null;
                String dctStr = null;
                
                ect = fromString(ectstr);
                origBytes = new byte[ect.length];
                for (int i = 0; i < origBytes.length; i += keys.length)
                {
                    for (int j = 0; j < keys.length; j++)
                    {
                        if ((i + j) >= origBytes.length)
                        {
                            break;
                        }
                        else
                        {
                            origBytes[i + j] = decrypt(ect[i + j], keys[j]);
                        }
                    }
                }
                
                try
                {
                    dctStr = new String(origBytes, "UTF-8");
                }
                catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                    throw new RuntimeException(e.toString());
                }
                
                return dctStr;
            }
            
        }
    Vedi se ti è utile. L'utilizzo è molto semplice:


    codice:
    CodeEncodeString ces = new CodeEncodeString();
    
    String s = "Ciao";
    s = ces.encrypt(s);
    ces.decrypt(s);
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  6. #6
    Utente di HTML.it L'avatar di ziz
    Registrato dal
    Jun 2008
    Messaggi
    52
    Originariamente inviato da Alex'87
    Io usavo questa classe:

    codice:
    import java.io.UnsupportedEncodingException;
    
    public class CodeEncodeString
        {
            private static final String[] DEFAULT_KEYS =
            {
            "01210ACB39201293948ABE4839201CDF",
            "123219843895AFDE3920291038103839",
            "89128912093908120983980981098309",
            "AABBCCDD019201920384383728298109"
            };
            
            private static boolean updatedProps = false;
            private static final char[] hexDigits =
            {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
            'D', 'E', 'F'
            };
            
            private byte[][] keys = null;
            
            public CodeEncodeString()
            {
                init(CodeEncodeString.DEFAULT_KEYS);
            }
            
            public CodeEncodeString(String[] keystrs)
            {
                init(keystrs);
            }
            
            private void init(String[] keystrs)
            {
                keys = new byte[keystrs.length][];
                for (int i = 0; i < keys.length; i++)
                {
                    keys[i] = fromString(keystrs[i]);
                }
            }
            
            private String toString(byte[] ba)
            {
                char[] buf = new char[ba.length * 2];
                int j = 0;
                int k;
                
                for (int i = 0; i < ba.length; i++)
                {
                    k = ba[i];
                    buf[j++] = hexDigits[(k >>> 4) & 0x0F];
                    buf[j++] = hexDigits[k & 0x0F];
                }
                
                return new String(buf);
            }
            
            private int fromDigit(char ch)
            {
                if (ch >= '0' && ch <= '9')
                {
                    return ch - '0';
                }
                if (ch >= 'A' && ch <= 'F')
                {
                    return ch - 'A' + 10;
                }
                if (ch >= 'a' && ch <= 'f')
                {
                    return ch - 'a' + 10;
                }
                
                throw new IllegalArgumentException("invalid hex digit '" + ch + "'");
            }
            
            private byte[] fromString(String hex)
            {
                int len = hex.length();
                byte[] buf = new byte[((len + 1) / 2)];
                
                int i = 0, j = 0;
                if ((len % 2) == 1)
                {
                    buf[j++] = (byte) fromDigit(hex.charAt(i++));
                }
                
                while (i < len)
                {
                    buf[j++] = (byte) ((fromDigit(hex.charAt(i++)) << 4) |
                                       fromDigit(hex.charAt(i++)));
                }
                
                return buf;
            }
            
            private byte encrypt(byte d, byte[] key)
            {
                byte e;
                
                e = d;
                for (int i = 0; i < key.length; i++)
                {
                    e = (byte) ((int) e ^ (int) key[i]);
                }
                
                return e;
            }
            
            private byte decrypt(byte e, byte[] key)
            {
                byte d;
                
                d = e;
                for (int i = key.length - 1; i >= 0; i--)
                {
                    d = (byte) ((int) d ^ (int) key[i]);
                }
                
                return d;
            }
            
            public String encrypt(String orig)
            {
                byte[] ect = null;
                int size;
                byte[] origBytes = null;
                
                try
                {
                    origBytes = orig.getBytes("UTF-8");
                }
                catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                    throw new RuntimeException(e.toString());
                }
                
                ect = new byte[origBytes.length];
                for (int i = 0; i < origBytes.length; i += keys.length)
                {
                    for (int j = 0; j < keys.length; j++)
                    {
                        if ((i + j) >= origBytes.length)
                        {
                            break;
                        }
                        else
                        {
                            ect[i + j] = encrypt(origBytes[i + j], keys[j]);
                        }
                    }
                }
                
                return toString(ect);
            }
            
            public String decrypt(String ectstr)
            {
                byte[] ect = null;
                int size;
                byte[] origBytes = null;
                String dctStr = null;
                
                ect = fromString(ectstr);
                origBytes = new byte[ect.length];
                for (int i = 0; i < origBytes.length; i += keys.length)
                {
                    for (int j = 0; j < keys.length; j++)
                    {
                        if ((i + j) >= origBytes.length)
                        {
                            break;
                        }
                        else
                        {
                            origBytes[i + j] = decrypt(ect[i + j], keys[j]);
                        }
                    }
                }
                
                try
                {
                    dctStr = new String(origBytes, "UTF-8");
                }
                catch (UnsupportedEncodingException e)
                {
                    e.printStackTrace();
                    throw new RuntimeException(e.toString());
                }
                
                return dctStr;
            }
            
        }
    Vedi se ti è utile. L'utilizzo è molto semplice:


    codice:
    CodeEncodeString ces = new CodeEncodeString();
    
    String s = "Ciao";
    s = ces.encrypt(s);
    ces.decrypt(s);
    Ottimo codice...complimenti!! è basato su qualche standard o è di tua invenzione?

  7. #7
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Originariamente inviato da ziz
    Ottimo codice...complimenti!! è basato su qualche standard o è di tua invenzione?
    Me lo ha passato qualche anno fa un utente di questo forum. Avevo bisogno di criptare il file delle risposte del compito del mio progetto d'esame

    p.s.: Non era necessario quotare tutto il codice ^^'
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  8. #8
    Utente di HTML.it L'avatar di ziz
    Registrato dal
    Jun 2008
    Messaggi
    52
    Originariamente inviato da Alex'87
    p.s.: Non era necessario quotare tutto il codice ^^'
    Era troppo bello e non ho saputo resistere!!!
    La prossima volta non lo farò, promesso!!
    Grazie mille per la risposta!! Ciao!!

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 © 2024 vBulletin Solutions, Inc. All rights reserved.