Criptografia

0 respostas
Thiago_Francisco

Estamos desenvolvendo um sistema que irá se comunicar via socket com módulo do software de uma outra empresa. E eles usam um tipo de criptografia próprio. Ele convertem o resultado da criptografia para hexadecimal e a chave também está em hexa. Geralmente quando criptografamos os dados usando as bibliotecas de criptografia o resultado é algo como WBTkAbbQlCm/Hv12VKA1sA==

Porém vejam como funciona a criptografia deles com o exemplo que eles passaram:

SenhaDigitada : [4127]
SenhaAlinhada : [00004127] --> 0x30 0x30 0x30 0x30 0x34 0x31 0x32 0x37
WorkingKey : [3132330035363738] --> 0x31 0x32 0x33 0x00 0x35 0x36 0x37 0x38

AplicarDESNBS (CIFRAR_DADOS, SenhaAlinhada, WorkingKey) para obter o resultado abaixo:
Resultado/Pinblock: [3ABCE460E6C780BC] --> 0x3A 0xBC 0xE4 0x60 0xE6 0xC7 0x80 0xBC

O WorkingKey seria a chave e o resultado criptografado foi isso 3ABCE460E6C780BC que está em hexa, a questão é . Como elaborar esse algoritmo de criptografia usando as bibliotecas do java?. O algoritmo é o DES.

Essa é a classe que usamos para criptografas os dados

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


public class Cripto
{
 private Cipher cipher;
 private byte[] encryptKey;
 private KeySpec keySpec;
 private SecretKeyFactory secretKeyFactory;
 private SecretKey secretKey;

 /**
 * Method that create a new instance of class.
 * @return
 * @throws InvalidKeyException
 * @throws UnsupportedEncodingException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchPaddingException
 * @throws InvalidKeySpecException
 */
 

 /**
 * Default Constructor.
 * @throws UnsupportedEncodingException
 * @throws NoSuchAlgorithmException
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 * @throws InvalidKeySpecException
 */
 private String Chave = "";
  

 public  Cripto(String key) throws UnsupportedEncodingException, NoSuchAlgorithmException,
 NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException
 {
     encryptKey = key.getBytes( "UTF-8" );
     cipher = Cipher.getInstance( "DESede" );
     keySpec = new DESedeKeySpec( encryptKey );
     secretKeyFactory = SecretKeyFactory.getInstance( "DESede" );
     secretKey = secretKeyFactory.generateSecret( keySpec );
 }

 /**
 * Method that encrypts a value.
 * @param value
 * @return
 * @throws InvalidKeyException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws UnsupportedEncodingException
 */
 public String encrypt( String value ) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
 UnsupportedEncodingException
 {
     if (value==null)
         return null;

     cipher.init( Cipher.ENCRYPT_MODE, secretKey );
     byte[] cipherText = cipher.doFinal( value.getBytes( "UTF-8" ) );
     BASE64Encoder encoder = new BASE64Encoder();
     return encoder.encode( cipherText );
 }

 /**
 * Methot that decrypts a value.
 * @param value
 * @return
 * @throws InvalidKeyException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws IOException
 */
 public String decrypt( String value ) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException,
 IOException
 {
     if (value==null)
         return null;

     cipher.init( Cipher.DECRYPT_MODE, secretKey );
     BASE64Decoder dec = new BASE64Decoder();
     byte[] decipherText = cipher.doFinal( dec.decodeBuffer( value ) );
     return new String( decipherText );
 }

}
Criado 20 de outubro de 2011
Respostas 0
Participantes 1