Duvida sobre javax.crypto

0 respostas
java
S

Tenho esse código como faço para passar somente senha encriptada para decriptar…

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class crypt {
    public static void main(String[] args) {
        String senha = "senhamuidificil";        
        //[B@47fd17e3
        System.out.println(senha);
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            SecretKey secretKey = keyGenerator.generateKey();
            
            Cipher cipher;
            
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            
            byte[] criptografia = cipher.doFinal(senha.getBytes());
            
            System.out.println(criptografia);           
            
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] descriptografia = cipher.doFinal(criptografia);
            System.out.println(new String (descriptografia));
            
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
            Logger.getLogger(crypt.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Desde já agradeço…

Criado 1 de maio de 2016
Respostas 0
Participantes 1