elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado:


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Como cifrar y descifrar con 3DES
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Como cifrar y descifrar con 3DES  (Leído 1,537 veces)
nolasco281


Desconectado Desconectado

Mensajes: 319


Ver Perfil
Como cifrar y descifrar con 3DES
« en: 1 Febrero 2016, 14:31 pm »

Hola a todos.

Mi consulta es la siquiente estoy tratando de cifrar un archivo con 3DES(TripleDes) he encontrado ejemplos de como se hace esto con cadenas de texto y funcionan, pero me entro la duda que pasa si es un archivo el que deseo cifrar, no he encontrado ejemplos en la web a ecepcion de este:

Ya lo probe y parece hacer la encritacion bien pero al momento de desencritar me devuelve 0 talvez puedan ayudarme, u orientarme. Saludos.

Esta es la parte de cifrar.
Código
  1. public class Encryptor {
  2.    private static String inputFilePath = "D:/1.txt";
  3.    public static void main(String[] args) {
  4.        FileOutputStream fos = null;
  5.        File file = new File(inputFilePath);
  6.        String keyString = "140405PX_0.$88";
  7.        String algorithm = "DESede";
  8.        try {
  9.            FileInputStream fileInputStream = new FileInputStream(file);
  10.            byte[] fileByteArray = new byte[fileInputStream.available()];
  11.            fileInputStream.read(fileByteArray);
  12.            for (byte b : fileByteArray) {
  13.            System.out.println(b);
  14.            }
  15.            SecretKey secretKey = getKey(keyString);
  16.            Cipher cipher = Cipher.getInstance(algorithm);
  17.            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  18.            ObjectOutputStream objectOutputStream = new ObjectOutputStream
  19.                    (new CipherOutputStream
  20.                            (new FileOutputStream
  21.                                    ("D:/Secret.file"), cipher));
  22.            objectOutputStream.writeObject(fileByteArray);
  23.            objectOutputStream.close();
  24.        } catch (Exception e) {
  25.            e.printStackTrace();
  26.        }
  27.    }
  28.    public static SecretKey getKey(String message) throws Exception {
  29.        String messageToUpperCase = message.toUpperCase();
  30.        byte[] digestOfPassword = messageToUpperCase.getBytes();
  31.        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
  32.        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
  33.        return key;
  34.    }
  35. }
  36.  

Esta es descifrar
Código
  1. public class Decryptor {
  2.    public static void main(String[] args) {
  3.        try {
  4.            File inputFileNAme = new File("d:/Secret.file");
  5.            FileInputStream fileInputStream = new FileInputStream(inputFileNAme);
  6.            FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath);
  7.            SecretKey secretKey = getKey(keyString);
  8.            Cipher cipher = Cipher.getInstance(algorithm);
  9.            cipher.init(Cipher.DECRYPT_MODE, secretKey);
  10.            ObjectInputStream objectInputStream = new ObjectInputStream
  11.                    (new CipherInputStream(fileInputStream, cipher));
  12.            System.out.println(objectInputStream.available());
  13.            while (objectInputStream.available() != 0) {
  14.                fileOutputStream.write((Integer) objectInputStream.readObject());
  15.                System.out.println(objectInputStream.readObject());
  16.            }
  17.            fileOutputStream.flush();
  18.            fileOutputStream.close();
  19.            fileInputStream.close();
  20.            objectInputStream.close();
  21.        } catch (Exception e) {
  22.            e.printStackTrace();
  23.        }
  24.    }
  25.    public static SecretKey getKey(String message) throws Exception {
  26.        String messageToUpperCase = message.toUpperCase();
  27.        byte[] digestOfPassword = messageToUpperCase.getBytes();
  28.        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
  29.        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
  30.        return key;
  31.    }
  32. }
  33.  


En línea

Lo que se puede imaginar... se puede programar.
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[C#] cifrar/descifrar en AES
.NET (C#, VB.NET, ASP)
Mace Windu 4 18,242 Último mensaje 20 Diciembre 2010, 04:56 am
por [D4N93R]
[c++] Cifrar y descifrar archivos
Programación C/C++
Beakman 2 17,551 Último mensaje 5 Diciembre 2016, 17:26 pm
por engel lex
cifrar/descifrar archivos
.NET (C#, VB.NET, ASP)
rob1104 3 6,921 Último mensaje 1 Julio 2012, 19:41 pm
por Keyen Night
cifrar/descifrar carpera en win y linux
Criptografía
maxmag 2 3,563 Último mensaje 27 Octubre 2012, 04:41 am
por APOKLIPTICO
[aporte]c++ cifrar/descifrar archivos comando
Programación C/C++
daryo 4 3,844 Último mensaje 12 Junio 2013, 17:57 pm
por daryo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines