Criptografia JAVA

4 respostas
java
R

Bom dia!

Fiz um algorítimo de criptografia mas a saída que tenho esta em um formato estranho.

import java.io.UnsupportedEncodingException;

import java.security.GeneralSecurityException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

import java.util.Arrays;
import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;

public class teste16 {

public static void main(String[] args)

throws UnsupportedEncodingException, GeneralSecurityException, DecoderException {

String ciphertext = [telefone removido];

String password = c2769646-42b5-4032-b0c6-d9d37d0dd278;
String decrypt = decrypt(ciphertext, password);

  System.out.println(decrypt.toString());

}

public static String decrypt(String ciphertext, String passphrase) {

try {

final int keySize = 256;

final int ivSize = 128;
byte[] ctBytes = Base64.encodeBase64(ciphertext.getBytes("UTF-8"));

  	byte[] saltBytes = Arrays.copyOfRange(ctBytes, 8, 16);

  	byte[] ciphertextBytes = Arrays.copyOfRange(ctBytes, 16, ctBytes.length);

  	byte[] key = new byte[keySize / 8];
  	byte[] iv = new byte[ivSize / 8];
  	EvpKDF(passphrase.getBytes("UTF-8"), keySize, ivSize, saltBytes, key, iv);

  	Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  	cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
  	byte[] recoveredPlaintextBytes = cipher.doFinal(ciphertextBytes);

  	return new String(recoveredPlaintextBytes);

  } catch (Exception e) {
  	e.printStackTrace();
  }

  return null;

}

private static byte[] EvpKDF(byte[] password, int keySize, int ivSize, byte[] salt, byte[] resultKey,

byte[] resultIv) throws NoSuchAlgorithmException {

return EvpKDF(password, keySize, ivSize, salt, 1, MD5”, resultKey, resultIv);

}
private static byte[] EvpKDF(byte[] password, int keySize, int ivSize, byte[] salt, int iterations,

String hashAlgorithm, byte[] resultKey, byte[] resultIv) throws NoSuchAlgorithmException {

keySize = keySize / 32;

ivSize = ivSize / 32;

int targetKeySize = keySize + ivSize;

byte[] derivedBytes = new byte[targetKeySize * 4];

int numberOfDerivedWords = 0;

byte[] block = null;

MessageDigest hasher = MessageDigest.getInstance(hashAlgorithm);

while (numberOfDerivedWords < targetKeySize) {

if (block != null) {

hasher.update(block);

}

hasher.update(password);

block = hasher.digest(salt);

hasher.reset();

for (int i = 1; i < iterations; i++) {

block = hasher.digest(block);

hasher.reset();

}

System.arraycopy(block, 0, derivedBytes, numberOfDerivedWords * 4,

Math.min(block.length, (targetKeySize - numberOfDerivedWords) * 4));

numberOfDerivedWords += block.length / 4;

}

System.arraycopy(derivedBytes, 0, resultKey, 0, keySize * 4);

System.arraycopy(derivedBytes, keySize * 4, resultIv, 0, ivSize * 4);

return derivedBytes;

}

}

A saída que tenho no console: ]jS�1`I�9+�’tĸ

A saída que eu espero deveria ser assim: U2FsdGVkX1+ghs/I4pP0MFSn0IzbEdCTbIDGbiiwYPg=

4 Respostas

TerraSkilll

Você obtém o mesmo resultado fora do console (ex: exibindo num JOptionPane)?

Faça esse testes, pois pode ser que o console não esteja conseguindo exibir alguns caracteres do seus resultado.

Abraço.

R

TerraSkilll

Tentei agora para exibir com o JOptionPane e o resultado foi o mesmo.

R

Eu acredito que ele não esteja fazendo a criptografia pois eu peguei o resultado ]jS�1`I�9+�’tĸ e fiz uma consulta em um site que criptografa e descriptografa e ele me retornou nada.

R

O erro esta nessa linha.

byte[] ciphertextBytes = Arrays.copyOfRange(ctBytes, 16, ctBytes.length);

O ciphertextBytes esta vindo vazio.

Criado 8 de maio de 2019
Ultima resposta 8 de mai. de 2019
Respostas 4
Participantes 2