The VOYOU
The VOYOU

Reputation: 524

Java:AES Encryption using CTR mode; unable to decrypte

I am using following code but It's not decrypting the text properly, what am I getting as output is

ciphered: %öNo2F?¢¶SHºûÅ“?¾

plaintext: hello × am originÎl

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    // Dernier exemple CTR mode
    // Clé 16 bits
    byte[] keyBytes = new byte[] { (byte) 0x36, (byte) 0xf1, (byte) 0x83,
            (byte) 0x57, (byte) 0xbe, (byte) 0x4d, (byte) 0xbd,
            (byte) 0x77, (byte) 0xf0, (byte) 0x50, (byte) 0x51,
            (byte) 0x5c, 0x73, (byte) 0xfc, (byte) 0xf9, (byte) 0xf2 };
    // IV 16 bits (préfixe du cipherText)
    byte[] ivBytes = new byte[] { (byte) 0x69, (byte) 0xdd, (byte) 0xa8,
            (byte) 0x45, (byte) 0x5c, (byte) 0x7d, (byte) 0xd4,
            (byte) 0x25, (byte) 0x4b, (byte) 0xf3, (byte) 0x53,
            (byte) 0xb7, (byte) 0x73, (byte) 0x30, (byte) 0x4e, (byte) 0xec };

    // Initialisation
    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

    // Mode
    Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");

    String originalText = "hello i am original";
    // ///////////////////////////////ENCRYPTING
    cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
    byte[] ciphered = cipher.doFinal(originalText.getBytes());
    String cipherText = new String(ciphered,"UTF-8");
    System.out.println("ciphered: " + cipherText);
    // ///////////////////////////////DECRYPTING
    cipher = Cipher.getInstance("AES/CTR/NoPadding");

    cipher.**init(Cipher.DECRYPT_MODE**, key, ivSpec);
    byte[] plain = **cipher.doFinal(ciphered);**
    originalText = new String(plain,"UTF-8");
    System.out.println("plaintext: " + originalText);
}

I couldn't figure out what am I doing wrong.any help is deeply appreciated. also is this proper way to encrypted some data this time I am trying to encrypt 4byte city pin code. thank in advance

////

I made those changes n' it's working fine but what's the issue if I passes cipherText.getByte() in cipher.init() function. Like

byte[] plain = cipher.doFinal(cipherText.getByte("UTF-8"));

n' Thanks for all your help.

Upvotes: 3

Views: 4739

Answers (2)

jtahlborn
jtahlborn

Reputation: 53694

You cannot convert the encrypted bytes to a String like that. "bytes" and "chars" are two entirely different things. remove the code which turns the bytes to a String and back again between encrypting and decrypting and your code should work (as pointed out in other answer, the second step should be using DECRYPT_MODE).

note that you need to be careful when using the platform character encoding to convert between bytes and chars/String, as this may be different on different platforms. this may cause problems if your data needs to move cross platform. it can also be lossy if your default platform encoding doesn't support all the characters in the text you are using.

Upvotes: 2

Henry
Henry

Reputation: 43788

For decryption you need to initialize the Cipher in DECRYPT_MODE. And also the byte[] to String conversion is not correct (See other answer).

Upvotes: 5

Related Questions