Criptografia

3 respostas
wagnerlegiao

Alguém poderia me indicar alguma biblioteca em JAVA que implemente os principais algoritmos de criptografia, com md5, sha-1, etc?
Obrigado.

3 Respostas

ivo_costa

segue exemplo:

import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64DecoderStream;
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.BASE64EncoderStream;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.swing.JOptionPane;
import java.security.spec.KeySpec;

public final class Base64 {
	 private static SecretKey skey;
	 private static KeySpec ks;
	 private static PBEParameterSpec ps = new PBEParameterSpec (
	 		new byte[]{3,1,4,1,5,9,2,6}, 20);
	 private static final String algorithm = "PBEWithMD5AndDES";
	 private static SecretKeyFactory skf;
	 static{
	 	try{
	 		skf = SecretKeyFactory.getInstance(algorithm);
	 	}
	 	catch(Exception e){
	 		JOptionPane.showMessageDialog(null, e.getMessage());
	 	}
	 }
	 
	 public static final byte[] desencriptar(final byte[] b, final String senha)
	 	throws Exception{
	 	
	 	ks = new PBEKeySpec (senha.toCharArray());
	 	skey = skf.generateSecret (ks);
	 	
	 	final Cipher cipher = Cipher.getInstance(algorithm);
	 	cipher.init(Cipher.DECRYPT_MODE, skey, ps);
	 	return BASE64DecoderStream.decode(cipher.doFinal(b));
	 }
	 public static final byte[] encriptar(final byte[] b, final String senha)
		 throws Exception{
	 	
	 	ks = new PBEKeySpec (senha.toCharArray());
	 	skey = skf.generateSecret (ks);
	 	final Cipher cipher = Cipher.getInstance(algorithm);
	 	cipher.init(Cipher.ENCRYPT_MODE, skey, ps);
                
	 	return cipher.doFinal(BASE64EncoderStream.encode(b));
	 }
}
rafaeldiego

MD5

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class md5
{
   private static MessageDigest md = null;
   static
   {
      try
      {
         md = MessageDigest.getInstance("MD5");
      }
      catch (NoSuchAlgorithmException ex)
      {
         ex.printStackTrace();
      }
   }

   private static char[] hexCodes(byte[] text)
   {
      char[] hexOutput = new char[text.length * 2];
      String hexString;

      for (int i = 0; i < text.length; i++)
      {
         hexString = "00" + Integer.toHexString(text[i]);
         hexString.toUpperCase().getChars(hexString.length() - 2,
         hexString.length(), hexOutput, i * 2);
      }

      return hexOutput;
   }

   public static String hash(String pwd)
   {
      if (md != null)
      {
         return new String(hexCodes(md.digest(pwd.getBytes())));
      }
      return null;
   }
}
F

Olá,

Segue um método básico …

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;




public String getSenha(String senha)
    {
    	try {
                       
            MessageDigest md = MessageDigest.getInstance( "MD5" );
            md.update( senha.getBytes() );
            BigInteger hash = new BigInteger( 1, md.digest() );
            senha = hash.toString(16);
            
            
           }
      catch(NoSuchAlgorithmException ns) {
      ns.printStackTrace();
        }

Espero ter ajudado …

Generosamente,

Frid

Criado 16 de setembro de 2008
Ultima resposta 16 de set. de 2008
Respostas 3
Participantes 4