Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 17
  1. #1
    Utente bannato
    Registrato dal
    Jun 2003
    Messaggi
    221

    [JAVA] crypt and decrypt

    ciao a tutti..
    mi servirebbero delle classi per criptare e decriptare....
    sapete dove posso trovarle....grazie..

  2. #2
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    allora o questa :

    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;
    	}
    
    }
    che chiami così :

    CodeEncodeString crypt = new CodeEncodeString();
    String decript = crypt.decrypt(stringaDaDecriptare);

    CodeEncodeString crypt = new CodeEncodeString();
    String encript = crypt.encrypt(stringaDaCriptare);


    oppure

    codice:
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    // FUNZIONE PER ENCODE DI UNA STRINGA
    	public String encode(String stringToEncode){
    		String returnValue = "";
    		BASE64Encoder encrypt = new BASE64Encoder();
    		try{
    			String codedString = encrypt.encode(stringToEncode.getBytes());
    			returnValue = codedString;
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return returnValue;
    	}
    	
    	
    	// FUNZIONE PER DECODE DI UNA STRINGA
    	public String decode(String stringToDecode){
    		String returnValue = "";
    		BASE64Decoder decrypt = new BASE64Decoder();
    		try {
    			String decodedString = new String(decrypt.decodeBuffer(stringToDecode));
    			returnValue = decodedString;	
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return returnValue;
    	}
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  3. #3
    Utente bannato
    Registrato dal
    Jun 2003
    Messaggi
    221
    sei un grande la provo subito ti ringrazio...pensa dovevo consegnare ieri...ti ringrazio adesso provo

  4. #4
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    non sono sicuro, ma credo che cambiando disposizione alle lettere cambia il modo di criptare i dati, una specie di personal key, quindi ti consiglio di farlo, in modo che solo tu li puoi decriptare...

    codice:
    private static final String [] DEFAULT_KEYS = {
    		"01210ACB39201293948ABE4839201CDF",
    		"123219843895AFDE3920291038103839",
    		"89128912093908120983980981098309",
    		"AABBCCDD019201920384383728298109"
    	};
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  5. #5
    Utente bannato
    Registrato dal
    Jun 2003
    Messaggi
    221
    ciao scusami
    io faccio un file...con delle sequenze per distinguere la stringa, quindi mi costruisco la stringa e appunto...criptandola..col tuo esempio..ho paura che nn mi legge il seguente carattere...??? " | "
    codice:
    codPaz + "|_|-|" +
    praticamente questo è un pezzo..dopo il + metto altre stringhe..
    nn so se quei caratteri li prende...che posso fare..??

  6. #6
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    no credo che prenda solo alfabeto e numeri...
    se nella tua strnga c'è quel carattere ti conviene usare il secondo modo che ti ho passato.. quello con il BASE64...
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  7. #7
    Utente bannato
    Registrato dal
    Jun 2003
    Messaggi
    221
    ti ringrazio..
    adesso provo..

  8. #8
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    con quello va tutto sicuro sicuro...
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  9. #9
    Utente bannato
    Registrato dal
    Jun 2003
    Messaggi
    221
    ciao
    senti il problema è che quando cripto la stringa con quei caratteri speciali la classe BASE64Decoder dovrebbe nn andare bene..ma...
    dovrebbe essere a 128 bit....
    no a 64...
    esiste un'altra classe per farlo????

  10. #10
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    ce ne sono molte, ma io purtroppo conosco solo ste 2...
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

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.