Ver Mensaje Individual
  #2 (permalink)  
Antiguo 24/01/2011, 07:34
Avatar de jhonmelguizo
jhonmelguizo
 
Fecha de Ingreso: enero-2008
Ubicación: Medellín - Colombia
Mensajes: 264
Antigüedad: 16 años, 3 meses
Puntos: 6
De acuerdo Respuesta: Encriptar Conexion SQL

Código JAVA:
Ver original
  1. import java.io.UnsupportedEncodingException;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.IllegalBlockSizeException;
  4. import javax.crypto.SecretKey;
  5.  
  6. /**
  7.  *
  8.  * @author http://www.exampledepot.com/egs/javax.crypto/desstring.html
  9.  */
  10. public class DesEncrypter {
  11.     Cipher ecipher;
  12.     Cipher dcipher;
  13.  
  14.     DesEncrypter(SecretKey key) {
  15.         try {
  16.             ecipher = Cipher.getInstance("DES");
  17.             dcipher = Cipher.getInstance("DES");
  18.             ecipher.init(Cipher.ENCRYPT_MODE, key);
  19.             dcipher.init(Cipher.DECRYPT_MODE, key);
  20.  
  21.         } catch (javax.crypto.NoSuchPaddingException e) {
  22.         } catch (java.security.NoSuchAlgorithmException e) {
  23.         } catch (java.security.InvalidKeyException e) {
  24.         }
  25.     }
  26.  
  27.     public String encrypt(String str) throws IllegalBlockSizeException {
  28.         try {
  29.             // Encode the string into bytes using utf-8
  30.             byte[] utf8 = str.getBytes("UTF8");
  31.  
  32.             // Encrypt
  33.             byte[] enc = ecipher.doFinal(utf8);
  34.  
  35.             // Encode bytes to base64 to get a string
  36.             return new sun.misc.BASE64Encoder().encode(enc);
  37.         } catch (javax.crypto.BadPaddingException e) {
  38.         } catch (IllegalBlockSizeException e) {
  39.         } catch (UnsupportedEncodingException e) {
  40.         } catch (java.io.IOException e) {
  41.         }
  42.         return null;
  43.     }
  44.  
  45.     public String decrypt(String str) {
  46.         try {
  47.             // Decode base64 to get bytes
  48.             byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
  49.  
  50.             // Decrypt
  51.             byte[] utf8 = dcipher.doFinal(dec);
  52.  
  53.             // Decode using utf-8
  54.             return new String(utf8, "UTF8");
  55.         } catch (javax.crypto.BadPaddingException e) {
  56.         } catch (IllegalBlockSizeException e) {
  57.         } catch (UnsupportedEncodingException e) {
  58.         } catch (java.io.IOException e) {
  59.         }
  60.         return null;
  61.     }
  62. }

Lo que puedes hacer es encriptar todo el string de conexión o si prefieres cada una de las propiedades de tu clase, creas un método que encripte y uno para desencriptar, cómo se usa:

Importas en la clase que vayas a instanciar:
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

Código JAVA:
Ver original
  1. //---------------------------------------
  2. try {
  3.         // Generate a temporary key. In practice, you would save this key.
  4.         // See also Encrypting with DES Using a Pass Phrase.
  5.         SecretKey key = KeyGenerator.getInstance("DES").generateKey();
  6.  
  7.         // Create encrypter/decrypter class
  8.         DesEncrypter encrypter = new DesEncrypter(key);
  9.  
  10.         // Encrypt
  11.         String encrypted = encrypter.encrypt("Don't tell anybody!");
  12.  
  13.         // Decrypt
  14.         String decrypted = encrypter.decrypt(encrypted);
  15.  
  16.         javax.swing.JOptionPane.showMessageDialog(null, encrypted);
  17.         javax.swing.JOptionPane.showMessageDialog(null, decrypted );
  18.  
  19.         } catch (Exception e) {
  20.         }

Espero te sirva, saludos...