Criptografia IDEA

Pessoal,

Alguem já utilizou o IDEA para criptografar em Java.

Valeu,

Ivan.

Você precisa usar um provider como o BouncyCastle.
Lembre-se que o IDEA é protegido por patentes, e para aplicações comerciais é aconselhável ver se é necessário solicitar uma licença dos inventores.

A implementação desse algoritmo está em src\org\bouncycastle\crypto\engines\IDEAEngine.java nos fontes do BouncyCastle. Veja aqui o cabeçalho, contendo a informação de licenciamento.

  • A class that provides a basic International Data Encryption Algorithm (IDEA) engine.
  • This implementation is based on the “HOWTO: INTERNATIONAL DATA ENCRYPTION ALGORITHM”
  • implementation summary by Fauzan Mirza (F.U.Mirza@sheffield.ac.uk). (baring 1 typo at the
  • end of the mulinv function!).
  • It can be found at ftp://ftp.funet.fi/pub/crypt/cryptography/symmetric/idea/
  • Note: This algorithm is patented in the USA, Japan, and Europe including
  • at least Austria, France, Germany, Italy, Netherlands, Spain, Sweden, Switzerland
  • and the United Kingdom. Non-commercial use is free, however any commercial
  • products are liable for royalties. Please see
  • www.mediacrypt.com for
  • further details. This announcement has been included at the request of
  • the patent holders.

Esta é uma modificação de um exemplo que vem com o BouncyCastle.

[code]
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.;
import javax.crypto.spec.
;
import java.security.;
import java.util.
;
import java.io.*;

class Test191 {
public static void main(String[] args) throws InvalidKeyException, IOException {
/*
* This will generate a random key, and encrypt the data
*/
Key key = null;
KeyGenerator keyGen;
Cipher encrypt = null;
Cipher decrypt = null;

	Security.addProvider(new BouncyCastleProvider());

	try
	{
		// "BC" is the name of the BouncyCastle provider

		//-- Aqui estamos gerando e gravando a chave de 128 bits (16 bytes)
		keyGen = KeyGenerator.getInstance("IDEA", "BC");
		keyGen.init(new SecureRandom());
		key = keyGen.generateKey();
		byte[] keyBytes = key.getEncoded();
		FileOutputStream fosKey = new FileOutputStream ("test.key");
		fosKey.write (keyBytes);
		fosKey.close();

		encrypt = Cipher.getInstance("IDEA/ECB/PKCS5Padding", "BC");

		encrypt.init(Cipher.ENCRYPT_MODE, key);

		//-- Aqui estamos gerando o arquivo cifrado
		FileOutputStream fos = new FileOutputStream("test.cipher");
		CipherOutputStream cOut = new CipherOutputStream(fos, encrypt);

		cOut.write("plaintext".getBytes());
		cOut.close();

		//-- Aqui estamos decifrando o arquivo com a chave lida a partir de "test.key"
		FileInputStream fisKey = new FileInputStream ("test.key");
		keyBytes = new byte[16];
		fisKey.read (keyBytes);
		SecretKeySpec sks = new SecretKeySpec (keyBytes, "IDEA/ECB/PKCS5Padding");
		decrypt = Cipher.getInstance("IDEA/ECB/PKCS5Padding", "BC");
		decrypt.init (Cipher.DECRYPT_MODE, sks);

		CipherInputStream cis = new CipherInputStream (new FileInputStream ("test.cipher"), decrypt);
		FileOutputStream fosPlain = new FileOutputStream ("test.plain");
		byte[] bt = new byte[1024];
		int n;
		while ((n = cis.read(bt)) > 0) {
			fosPlain.write (bt, 0, n);
		}
		fosPlain.close();
		cis.close();
	}
	catch (Exception e)
	{
		System.err.println(e);
		System.exit(1);
	}

}

}[/code]

Você tem um exemplo de criptografia de uma String ?

Valeu,

Ivan.

Strings não são arrays de bytes, portanto você deve tomar alguns cuidados
a) Converta a string usando String.getBytes (“ISO-8859-1”)
b) Cifre os bytes
c) Os bytes devem ser convertidos para Base-64 ou hexadecimal.
Nunca devem ser diretamente convertidos em uma String, porque você não vai conseguir decifrar a String de volta. Isso é porque new String (byte[]) pega alguns dos bytes e converte para o caracter 0x3F, ou “?”, efetivamente bagunçando a criptografia.
Na hora de decifrar
a) Converta a string que está em base-64 ou hexadecimal, para um array de bytes
b) Decifre os bytes
c) Agora sim,você pode converter os bytes usando new String(byte[], “ISO-8859-1”), porque os bytes que foram decifrados são apenas o que podem ser convertidos corretamente para uma String.
P.S. Você viu que uma string foi criptografada ( cOut.write(“plaintext”.getBytes()); ) , mas de propósito gravei em um arquivo (bytes), não em uma outra string. Não é difícil mudar o exemplo acima…

Nada de errado com o exemplo acima, mas uma sugestao seria usar UTF-8 ao inves de ISO-8859-1, ja que o UTF-8 suporta uma quantidade maior de caracteres (alias, suporta o Unicode todo ;))