como implementar los algoritmos de encriptacion DES o RSA en jAvA

1,662 views
Skip to first unread message

jOsE guidO

unread,
Feb 27, 2006, 12:53:10 PM2/27/06
to JavaSOS
hola gente
alguno sabe como implementar los algoritmos de encriptacion DES o RSA
en jAvA,
un ejemplito, manuales, algo de ayuda please!!

Luis Soria

unread,
Feb 27, 2006, 1:29:02 PM2/27/06
to jav...@googlegroups.com
Jose, encontré esto por la web, lo modifqué a mi necesidad y funciona ok-.

Tenes que crear el certificado digital, yo lo hice con openssl en linux.
Luego instancias la clase, llamas al metodo init...
luego a los encrypt o decrypt...

Saludos.
Luis Soria.

RSAEncryptUtil.java

Ernesto Tagwerker

unread,
Feb 27, 2006, 1:29:30 PM2/27/06
to jav...@googlegroups.com
Hola,

Fijate en la api de la clase javax.crypto.Cipher, te permite usar
diferentes algoritmos de encripción. Además en el paquete javax.crypto
tenés otras clases que te pueden ser útiles.

Espero te sirva de algo.

Saludos,
Ernesto.

jOsE guidO

unread,
Feb 27, 2006, 1:31:10 PM2/27/06
to jav...@googlegroups.com, luis_s...@hotmail.com
Luis el .java que me enviaste ta vacio, me lo podes enviar otra ves
muchas gracias

Luis Soria

unread,
Feb 27, 2006, 1:38:17 PM2/27/06
to jOsE guidO, jav...@googlegroups.com
package com.hasar.util;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
 
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
 
import org.bouncycastle.jce.provider.BouncyCastleProvider;
 
import com.hasar.distribuidor.TareaPrincipal;
 
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
public class RSAEncryptUtil {
    protected static final String ALGORITHM = "RSA";
    protected static final String ENCODING = "UTF8";
    protected static final String PADDING = "RSA/ECB/PKCS1Padding";
    protected static final String CERT_FILENAME = "ARCHIVO.PFX";
    protected static final String PKCS = "PKCS12";
    protected static final String PASSWORD = "Clave de acceso al pfx";
    public static PrivateKey _privateKey;
 
    public static PublicKey _publicKey;
 
    public static X509Certificate _certificate;
   
    public static Cipher cipher;
 
    private RSAEncryptUtil() {
    }
 
    public static void init() {
        try {
            Security.addProvider(new BouncyCastleProvider());
            TareaPrincipal._debug.print("EncrypUtil","Provider en memoria\nObteniendo la instancia PKCS1Padding - Puede tardar ..");
            cipher = Cipher.getInstance(PADDING);
        } catch (NoSuchAlgorithmException e) {
             e.printStackTrace();
        } catch (NoSuchPaddingException e) {
             e.printStackTrace();
        }
    }
 
    public static byte[] encrypt(byte[] text, PublicKey key) throws Exception {
        byte[] cipherText = null;
        try {
            cipher.init(Cipher.ENCRYPT_MODE, key);
            cipherText = cipher.doFinal(text);
        } catch (Exception e) {
            throw e;
        }
        return cipherText;
    }
 
    public static String encrypt(String text, PublicKey key) throws Exception {
        String encryptedText;
        try {
            byte[] cipherText = encrypt(text.getBytes(ENCODING), key);
            encryptedText = encodeBASE64(cipherText);
        } catch (Exception e) {
            throw e;
        }
        return encryptedText;
    }
 
    private static String encodeBASE64(byte[] bytes) {
        BASE64Encoder b64 = new BASE64Encoder();
        return b64.encode(bytes);
    }
 
    public static void cargoCertificadoX509() throws Exception {
        KeyStore keyStore = KeyStore.getInstance(PKCS);
       
        FileInputStream inputStream = new FileInputStream(CERT_FILENAME);
        try {
           
            char[] passwordChars = new char[PASSWORD.length()];
            PASSWORD.getChars(0, PASSWORD.length(), passwordChars, 0);
            TareaPrincipal._debug.print("EncrypUtil","Espere Por Favor...");
            keyStore.load(inputStream, passwordChars);
            Enumeration enumeration = keyStore.aliases();
            String alias = (String) enumeration.nextElement();
            PrivateKey privKey = (PrivateKey) keyStore.getKey(alias,
                    passwordChars);
            X509Certificate cert = (X509Certificate) keyStore
                    getCertificate(alias);
            _privateKey = privKey;
            _certificate = cert;
            _publicKey = cert.getPublicKey();
        } finally {
           inputStream.close();
        }
    }
   
    public static byte[] decrypt(byte[] text, PrivateKey key) throws Exception
    {
        byte[] dectyptedText = null;
        try
        {
            cipher.init(Cipher.DECRYPT_MODE, key);
            dectyptedText = cipher.doFinal(text);
        }
        catch (Exception e)
        {
            throw e;
        }
        return dectyptedText;
 
    }
 
    public static String decrypt(String text, PrivateKey key) throws Exception
    {
        String result;
        try
        {
            byte[] dectyptedText = decrypt(decodeBASE64(text),key);
            result = new String(dectyptedText, ENCODING);
        }
        catch (Exception e)
        {
            throw e;
        }
        return result;
    }
   
    private static byte[] decodeBASE64(String text) throws IOException {
        BASE64Decoder b64 = new BASE64Decoder();
        return b64.decodeBuffer(text);
    }
}
 
 
----- Original Message -----
From: jOsE guidO

Gabriel Belingueres

unread,
Mar 2, 2006, 12:36:32 PM3/2/06
to JavaSOS
Hola,

Fijate en la página de http://www.bouncycastle.org que ahi tienen
implementaciones de esos algoritmos.

Saludos,
Gabriel

Reply all
Reply to author
Forward
0 new messages