Algoritmos DES para Java
Estoy buscando algunos ejemplos de algoritmos DES para java, ¿Alguien tendra algunos links?
ACA TE DEJO UN ALGORITMO ENVIADO POR LUPUS, ESPERO QUE TE SIRVA.
Re: Re: busco algoritmo de encriptacion
Enviado por Lupus el dÃa 7 de abril de 2003
Aqui esta un pequeño ejemplo de como encriptar un password utilizando DES y MD5 con el JCE, espero y les sirva:
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class PBEExample {
byte[] salt = { (byte)0xd4, (byte)0xa3, (byte)0xff, (byte)0x9e,
(byte)0x12, (byte)0xc7, (byte)0xd0, (byte)0x84 };
Cipher c;
PBEParameterSpec paramSpec;
SecretKey passwordKey;
public PBEExample(String password) {
try {
paramSpec = new PBEParameterSpec( salt, 20 );
PBEKeySpec keySpec = new PBEKeySpec( password.toCharArray() );
SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
passwordKey = kf.generateSecret( keySpec );
c = Cipher.getInstance("PBEWithMD5AndDES");
} catch (Exception e) {
System.out.println("PBEExample() caught exception '" + e +
"' with message '" + e.getMessage() + "'.");
e.printStackTrace();
}
}
public void encrypt(DataInputStream dis, DataOutputStream dos)
throws Exception {
c.init(Cipher.ENCRYPT_MODE, passwordKey, paramSpec);
CipherInputStream cis = new CipherInputStream( dis, c );
int i;
while ( (i = cis.read()) >= 0 ) {
dos.write(i);
}
}
public void decrypt(DataInputStream dis, DataOutputStream dos)
throws Exception {
c.init(Cipher.DECRYPT_MODE, passwordKey, paramSpec);
CipherInputStream cis = new CipherInputStream( dis, c );
int i;
while ( (i = cis.read()) >= 0 ) {
dos.write(i);
}
}
public static void main(String[] args) {
System.out.println("PBEample, A Password Based Encryptor.");
System.out.println("Example: PBEExample [e|d] [filename] [password]");
try {
FileInputStream fis = new FileInputStream( args[1] );
DataInputStream dis = new DataInputStream( fis );
FileOutputStream fos = new FileOutputStream( args[1] + "." +
args[0] );
DataOutputStream dos = new DataOutputStream( fos );
PBEExample pbe = new PBEExample( args[2] );
if ( args[0].equals("e") )
pbe.encrypt( dis, dos );
else if ( args[0].equals("d") )
pbe.decrypt( dis, dos );
else
System.out.println("Error, '" + args[0] +
"' is an invalid command");
dos.flush();
fos.flush();
fos.close();
fis.close();
} catch (Exception e) {
System.out.println("PBEExample.main() caught exception '" + e +
"' with message '" + e.getMessage() + "'.");
e.printStackTrace();
}
}
}
Re: Re: busco algoritmo de encriptacion
Enviado por Lupus el dÃa 7 de abril de 2003
Aqui esta un pequeño ejemplo de como encriptar un password utilizando DES y MD5 con el JCE, espero y les sirva:
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class PBEExample {
byte[] salt = { (byte)0xd4, (byte)0xa3, (byte)0xff, (byte)0x9e,
(byte)0x12, (byte)0xc7, (byte)0xd0, (byte)0x84 };
Cipher c;
PBEParameterSpec paramSpec;
SecretKey passwordKey;
public PBEExample(String password) {
try {
paramSpec = new PBEParameterSpec( salt, 20 );
PBEKeySpec keySpec = new PBEKeySpec( password.toCharArray() );
SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
passwordKey = kf.generateSecret( keySpec );
c = Cipher.getInstance("PBEWithMD5AndDES");
} catch (Exception e) {
System.out.println("PBEExample() caught exception '" + e +
"' with message '" + e.getMessage() + "'.");
e.printStackTrace();
}
}
public void encrypt(DataInputStream dis, DataOutputStream dos)
throws Exception {
c.init(Cipher.ENCRYPT_MODE, passwordKey, paramSpec);
CipherInputStream cis = new CipherInputStream( dis, c );
int i;
while ( (i = cis.read()) >= 0 ) {
dos.write(i);
}
}
public void decrypt(DataInputStream dis, DataOutputStream dos)
throws Exception {
c.init(Cipher.DECRYPT_MODE, passwordKey, paramSpec);
CipherInputStream cis = new CipherInputStream( dis, c );
int i;
while ( (i = cis.read()) >= 0 ) {
dos.write(i);
}
}
public static void main(String[] args) {
System.out.println("PBEample, A Password Based Encryptor.");
System.out.println("Example: PBEExample [e|d] [filename] [password]");
try {
FileInputStream fis = new FileInputStream( args[1] );
DataInputStream dis = new DataInputStream( fis );
FileOutputStream fos = new FileOutputStream( args[1] + "." +
args[0] );
DataOutputStream dos = new DataOutputStream( fos );
PBEExample pbe = new PBEExample( args[2] );
if ( args[0].equals("e") )
pbe.encrypt( dis, dos );
else if ( args[0].equals("d") )
pbe.decrypt( dis, dos );
else
System.out.println("Error, '" + args[0] +
"' is an invalid command");
dos.flush();
fos.flush();
fos.close();
fis.close();
} catch (Exception e) {
System.out.println("PBEExample.main() caught exception '" + e +
"' with message '" + e.getMessage() + "'.");
e.printStackTrace();
}
}
}