Olá a todos,

alguem teria um exemplo bem simples de como encrypto uma String e depois DesCrypto. Tipo, quero salvar a senha do usuário em um arquivo XML, por isso quero encriptografa-la.....

qualquer exemplo.......


(Ps. No final do email coloquei um exemplo, só que ele gera a chave todas as vezes... alias nao entendi direito....  :((  sorry!

Atenciosamente
PAULO BRANCO
DIRETIVA DIGITAL



import java.security.*;
import javax.crypto.*;

/**
 * Demonstrates how to generate a key, create and initialize a cipher,
 * encrypt and decrypt files using JCE;
 ***/

public class CipherDemo {

    static public void main(String args[]) throws GeneralSecurityException {

        // Generate a DES key
        //KeyGenerator keygen = KeyGenerator.getInstance("DES");
        KeyGenerator keygen = new KeyGenerator();
        SecretKey key = keygen.generateKey();
        System.out.println("key="+key);
       
        // Create the cipher
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

        // The plain text
        byte[] plaintext1 = "Hello World!".getBytes();

        // Initialize the cipher for encryption
        cipher.init(Cipher.ENCRYPT_MODE, key);

        // Encrypt plaintext1
        byte[] ciphertext = cipher.doFinal(plaintext1);
        System.out.println("Encrypt="+new String(ciphertext));
        // Initialize the cipher for decryption
        cipher.init(Cipher.DECRYPT_MODE, key);

        // Decrypt ciphertext
        byte[] plaintext2 = cipher.doFinal(ciphertext);

        System.out.println(new String(plaintext2)); // "Hello World!"

    }

}

Responder a