Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    910

    [Java] Criptare stringhe...

    Salve,
    In php utilizzo la funzione md5("stringa"), per ottenere l'hash corrispondente, si può fare lo stesso in java?
    Ovvero c'è la medesima funzione?
    Qualora non ci fosse quale algoritmo utilizzate per criptare i vostri dati "sensibili"?!?!Avete qualche esempietto?!
    grazie


  2. #2
    prova con java.security.MessageDigest

  3. #3
    Utente di HTML.it L'avatar di morphy79
    Registrato dal
    Jun 2004
    Messaggi
    1,568
    allora c'è questa classe che è molto carina

    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;
    	}
    
    }

    se no c'è quest'altra


    codice:
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    public class CtrlKey {
    
    
    	
    	
    	// 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;
    	}		
    	
    	
    	
    	
    }

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

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Riprendo del codice saltato fuori qualche tempo fa, dando credito a chi di diritto (anche se non ricordo chi fosse).

    codice:
    import java.security.*;
    import java.io.*;
    .
    .
    .
    .
    
    public static String hex(byte[] array) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
          sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1,3));
        }
        return sb.toString();
      }
    
      public static String md5 (String message) {
        try {
          MessageDigest md = MessageDigest.getInstance("MD5");
          return hex (md.digest(message.getBytes("CP1252")));
        }
        catch (NoSuchAlgorithmException e) {}
        catch (UnsupportedEncodingException e) {}
        return null;
      }
    Un estratto per md5
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    910
    grazie mille md5 è proprio quello che mi serve...
    grazie a tutti!

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.