ciao devo confrontare delle password di mysql che sonos state inserite nel db così
PASSWORD("ciao")
e risultano criptate con mysql
io dovrei confrontarle nel logi, ma dovrei criptare la stringa che mi arriva in java, come posso fare?
![]()
ciao devo confrontare delle password di mysql che sonos state inserite nel db così
PASSWORD("ciao")
e risultano criptate con mysql
io dovrei confrontarle nel logi, ma dovrei criptare la stringa che mi arriva in java, come posso fare?
![]()
live free or die
Ma i dati nel db li inserisci tu? O sono già li?
Hai pensato di crittografare tu le password usando i package di java o mysql?
Per inserire e riottenere le password, in un progetto ho usato la seguente classe.
Ho anche utilizzato il package di java sulla sicurezza. Cmq tutto dipende da come sei vincolato al dbcodice:/* * Created on 14-giu-2004 */ package control; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import db.ConnectionPoolAb; import db.ConnectionPoolVector; /** * @author Zero-2 * */ public class Crypt { /** * Metodo che permette la decodifica della password * @param tmp * @return * @throws Exception */ public static String encode(String tmp) throws Exception{ ConnectionPoolAb cpv=ConnectionPoolVector.getConnectionPool(); Connection conn=cpv.getConnection(); Statement st=conn.createStatement(); //mi serve come chiave per la crittografia ResultSet rs=st.executeQuery("select MD5(partitaIVA) from DatiAzienda"); rs.next(); String key=rs.getString(1); rs=st.executeQuery("select AES_ENCRYPT('"+tmp+"','"+key+"')"); rs.next(); cpv.releaseConnection(conn); return rs.getString(1).replace("\'","\\\'"); } /** * Metodo che codifica la password del dipendente * @param tmp * @return * @throws Exception */ public static String decode(String tmp) throws Exception{ ConnectionPoolAb cpv=ConnectionPoolVector.getConnectionPool(); Connection conn=cpv.getConnection(); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("select MD5(partitaIVA) from DatiAzienda"); rs.next(); String key=rs.getString(1); rs=st.executeQuery("select AES_DECRYPT('"+tmp+"','"+key+"')"); rs.next(); cpv.releaseConnection(conn); return rs.getString(1); } }![]()
Lang=Java
Ambiente = Eclipse forever
Ubuntu& Win XP Pro
i dati sono giò nel db ma posso anche inserirli io
si ho pensato di crittografarli ma nn so da dove cominciare
nn capisco dove cripti la stringa con java, vedo MD5 sono nelle query sql
![]()
live free or die
Infatti in quella classe non uso Java, ma sfrutto i comandi sql. Tuttavia quella classe per ogni crittografia effettuata richiama un comando sql
Questa è la classe che ho implementato per crittografare usando Java
codice:package beans; import java.io.IOException; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class SecurityUtils implements Serializable { /** * */ private static final long serialVersionUID = 2525681816498748588L; private String seme; private SecretKey key; private String messaggio; public String getSeme(){ return seme; } public void setSeme(String seme){ this.seme=seme; } public String getMessaggio(){ return messaggio; } public void setMessaggio(String messaggio){ this.messaggio=messaggio; } /** * Genera una chiave a partire da un seme inziale, restituendo sempre la stessa chiave. */ public void genKey() throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException { DESKeySpec des=new DESKeySpec(seme.getBytes()); SecretKeyFactory secret=SecretKeyFactory.getInstance("DES"); key= secret.generateSecret(des); } /** * Decripta una stringa a partire da una chiave privata Key * @param key * @param mess * @return * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IOException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidKeySpecException */ public String getDecrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException { genKey(); Cipher cipher=Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE,key); BASE64Decoder decoder = new BASE64Decoder(); byte[] raw = decoder.decodeBuffer(messaggio); byte[] stringBytes = cipher.doFinal(raw); return new String(stringBytes,"UTF8"); } /** * Cripta una stringa usando una chiave privata Key * @param key * @param mess * @return * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws UnsupportedEncodingException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidKeySpecException */ public String getCrypt() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException{ genKey(); Cipher cipher=Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE,key); byte[] stringBytes=messaggio.getBytes("UTF8"); byte[] raw=cipher.doFinal(stringBytes); BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(raw); } }
Lang=Java
Ambiente = Eclipse forever
Ubuntu& Win XP Pro
ciao ho provato a capirlo ed applicarlo ma nn ci riesco
se potete incollatemi il codice per criptare in MD5 una stringa
String a = "aaa";
Altrimenti lo leggerò con più calma in futuro
grazie
live free or die
La classe usa il DES per criptare le stringhe e per usarla devi fare così
E' molto semplice , anche se la classe non è molto commentatacodice:Per crittografare SecurityUtils su=new SecurityUtils(); su.setMessaggio("bla bla bla"); su.setSeme("pippo"); String crypted=su.getCrypt();//restituisce la stringa criptata con il seme specificato Per decrittare su.setMessaggio(crypted);//messaggio criptato su.setSeme("pippo"); String decrypted=su.getDecrypt();![]()
![]()
Lang=Java
Ambiente = Eclipse forever
Ubuntu& Win XP Pro
Penso che questo faccia al caso tuo
codice:import java.io.*; import java.security.*; public class MdCrypt { public static void main(String args[]) { try { MessageDigest md = MessageDigest.getInstance("SHA"); String data="La mia stringa"; byte buf[] = data.getBytes( ); md.update(buf); System.out.println(md.digest()); } catch (Exception e) { System.out.println(e); } } }
Lang=Java
Ambiente = Eclipse forever
Ubuntu& Win XP Pro