Visualizzazione dei risultati da 1 a 3 su 3

Discussione: [java] cript password

  1. #1
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568

    [java] cript password

    fino ad ora usavo questa semplice classe

    codice:
    package it.vegaspa.clientFXS.businessLogic;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    public class Encr_Decr {
    	
    
    	// FUNZIONE PER CODIFICARE LA STRINGA
    	public String Encode(String stringToEncode){
    		BASE64Encoder encrypt = new BASE64Encoder();
    		String codedString = "";
    		try{
    			codedString = encrypt.encode(stringToEncode.getBytes());
    			// DEBUG
    			//System.out.println(codedString);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return codedString;
    	}
    	
    	
    	// FUNZIONE PER DECODIFICARE LA STRINGA
    	public String Decode(String stringToDecode){
    		BASE64Decoder decrypt = new BASE64Decoder();
    		String decodedString = "";
    		try {
    			decodedString = new String(decrypt.decodeBuffer(stringToDecode));
    			// DEBUG
    			//System.out.println(decodedString);	
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return decodedString;
    	}
    	
    
    }

    solo che mi genera caratteri che non vanno bene...
    io vorrei criptare/decriptare usando solo nuomeri e caratteri dell'alfabeto...

    consigli ???
    odio chi parla di politica..
    anzi vorrei fondare un partito contro tutto ciò

  2. #2
    Utente di HTML.it L'avatar di netarrow
    Registrato dal
    Apr 2004
    Messaggi
    1,425
    il problema è che la codifica BASE64 non è per codificare o decodificare una stringa in modo da renderla irriconoscibile.
    Lo scopo di questo algoritmo è trasformare un byte da 8 bits in un byte da 7 bits, visto che lo standard ASCI appunto tiene l'8 sempre a 0.
    Prima di convertire devi codificare con un algoritmo appunto per codificare, ad esempio Des, TripleDes, Blowfish, o a chiave pubblica con RSA ecc... dopo che questi algoritmi hanno generato caratteri assurdi con BASE64 rimetti tutto apposto per renderlo leggibile in ASCI.
    Il package in cui trovi algoritmi per codificare è java.security, per esempi es fai una ricerca nel forum io e altri utenti abbiamo già postato del codice che potrebbe fare al coso tuo.
    Se vuoi approfondire l'argomento sicurezza con Java ti consiglio Sicurezza in Java di Jess Garms e Daniel Somerfield.

    Imparare è un'esperienza, tutto il resto è solo informazione. (Albert Einstein)

  3. #3
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    grazie, ma avevo già cercato sul forum e tutti i vari script o non mi andavano o mi mancavano librerie !!
    ho risolto con questa classe che ho trvato quasi per caso :

    codice:
    import java.io.UnsupportedEncodingException;
    
    public class Crypt
    {
    	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 Crypt() {
    		init(this.DEFAULT_KEYS);
    	}
    
    	public Crypt(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;
    	}
    
    }
    cmq grazie 1000 !!!
    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 © 2024 vBulletin Solutions, Inc. All rights reserved.