El Donaldo
El Donaldo

Reputation: 33

Java : My system for decrypt text doesn't work

I've a system for encrypt texts, but i've tried to create a system for decrypt texts, and it doesn't work. The system is :

He just return the encrypted text, but not the decrypted text. Have you an idea for debug this?

Thanks in advance.

byte[] getEncrypt(String text) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException
{
    String key = "Bép12345Taruy'(";

    Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, aesKey);
    byte[] encrypted = cipher.doFinal(text.getBytes());

    return encrypted;
}

String getDecrypt(String text) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException
{
    String key = "Bép12345Taruy'(";

    Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, aesKey);
    byte[] encrypted = cipher.doFinal(text.getBytes());

    cipher.init(Cipher.DECRYPT_MODE, aesKey);
    String decrypted = new String(cipher.doFinal(encrypted));

    return decrypted;
}

Upvotes: 2

Views: 210

Answers (2)

minus
minus

Reputation: 2786

I use jasypt to encrypt passwords. The method is very simple.

BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword("some_Password_Maybe_generated_At_random");
...
String myEncryptedText = textEncryptor.encrypt(myText);
...
String plainText = textEncryptor.decrypt(myEncryptedText);

Normally I do not put the password in the source but i save it in a file in the user home dir.

The encryption is as strong as the user home dir is safe and private.

This method is good because it returns a string which can be put in a config file.

As I said before, it is not very safe, it will protect the encrypted text as long as the encryption password is safe.

Upvotes: 0

heiwil
heiwil

Reputation: 669

You encrypt the encrypted text in your getDecrypt(...) method. Or do you want to encrypt and decript it again in one method?

One solution would be following code:

package test;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class Test2{        

    public static void main(String[] args) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException {
        System.out.println(new String(getEncrypt("test")));
        System.out.println(new String(getDecrypt(getEncrypt("test"))));
        
    }

    public static byte[] getEncrypt(String text) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        String key = "Bép12345Taruy'(";

        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        byte[] encrypted = cipher.doFinal(text.getBytes());

        return encrypted;
    }

    public static byte [] getDecrypt(byte[] encrypted) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        String key = "Bép12345Taruy'(";

        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");

        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        return cipher.doFinal(encrypted);
    }
    
}

Output:

�'���+~�@��@w

test

Upvotes: 2

Related Questions