A string encrypted (AES) after decryption prints the same value , but false on equals()

Vadim

My program sends a string encrypted (AES) with received session key to Client to prove the key is correct. Client should decrypt it, get the string and verify it with original one.

Program works fine. It encrypts and decrypts the string. It prints the string I need, but gives me false when I do String.equals(string). I can figure out why. There is the encryption part of my code:

// ----create a challenge for Client (to check if the session key is correct)--------

public void sessionKeyVer(String challenge, File out) throws Exception{

    aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpec); // switching mode for encryption

    CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), aesCipher); //output stream to another file 

    os.write(challenge.getBytes("UTF-8"));// function to copy String to outputstream
    os.close();     //close the stream

}

There is the decryption part:

public boolean sessionKeyVer(File file) throws Exception{
    aesCipher.init(Cipher.DECRYPT_MODE, aeskeySpec); // switching mode for decryption

    CipherInputStream is = new CipherInputStream(new FileInputStream(file), aesCipher); //output stream to another file 
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    int i;
    byte[] b = new byte[1024];
    while((i=is.read(b))!=-1) {
          os.write(b, 0, i);
      }

    is.close();
    os.close();
    String file_string = new String(b,"UTF-8");
    System.out.print(file_string);
    return file_string.equals(challenge); //return false

}

Thank you.

user207421

The first part is the encryption part. The second part is the decryption part.

The second part is wrong. You are decrypting the last part of the still-encrypted buffer, rather than the entire, decrypted ByteArrayOutputStream, and committing a size error in the process too.

String file_string = new String(b,"UTF-8");

should be

String file_string = new String(os.toByteArray(), "UTF-8");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the decryption method working to return the encrypted string back to the original?

From Dev

How to decrypt an AES-256-CBC encrypted string

From Dev

Garbage value after decrypting Base64 encrypted String with RSA java

From Dev

A string encrypted (AES) after decryption prints the same value , but false on equals()

From Dev

Remove \r and \n from AES encrypted string

From Dev

Foreach and while loop prints the same value

From Dev

Specify input string length in AES_encrypt function while decryption

From Dev

Why String.Equals is returning false?

From Dev

AES decryption with password using CryptoJS returns a blank value

From Dev

AES decryption result varies after openssl upgrade

From Dev

Can you reverse-generate the RSA/AES key if you have the decrypted and encrypted version of the same string? (JS client-side crypto)

From Dev

AES/CBC/PKCS5PADDING IV - Decryption in NodeJs (Encrypted in Java)

From Dev

Is it possible to compare two AES encrypted string in iOS?

From Dev

Java AES encryption/decryption always return the same content

From Dev

Arrays.equals() and String.equals() always return false

From Dev

Unable to get decrypted file as String in AES Encryption/Decryption

From Dev

Specify input string length in AES_encrypt function while decryption

From Dev

Unable to decrypt AES encrypted string from Objective C

From Dev

How to decrypt a C# encrypted AES string in java

From Dev

Size of data after AES decryption

From Dev

I have 2 Same value object of String Buffer Class. String equals() method Showing False result Why?

From Dev

Returning after ajax call prints false

From Dev

AES decryption after sending message to IP address

From Dev

Problems retrieving stored AES encrypted string in Python

From Dev

Why does this java string comparison prints false?

From Dev

swift aes 128 decryption with string input

From Dev

JavaScript date compare fails but prints same value

From Dev

Decrypt a base64 encoded and aes encrypted string using RNCryptor

From Dev

decryption string that is encrypted from c#

Related Related

  1. 1

    How to get the decryption method working to return the encrypted string back to the original?

  2. 2

    How to decrypt an AES-256-CBC encrypted string

  3. 3

    Garbage value after decrypting Base64 encrypted String with RSA java

  4. 4

    A string encrypted (AES) after decryption prints the same value , but false on equals()

  5. 5

    Remove \r and \n from AES encrypted string

  6. 6

    Foreach and while loop prints the same value

  7. 7

    Specify input string length in AES_encrypt function while decryption

  8. 8

    Why String.Equals is returning false?

  9. 9

    AES decryption with password using CryptoJS returns a blank value

  10. 10

    AES decryption result varies after openssl upgrade

  11. 11

    Can you reverse-generate the RSA/AES key if you have the decrypted and encrypted version of the same string? (JS client-side crypto)

  12. 12

    AES/CBC/PKCS5PADDING IV - Decryption in NodeJs (Encrypted in Java)

  13. 13

    Is it possible to compare two AES encrypted string in iOS?

  14. 14

    Java AES encryption/decryption always return the same content

  15. 15

    Arrays.equals() and String.equals() always return false

  16. 16

    Unable to get decrypted file as String in AES Encryption/Decryption

  17. 17

    Specify input string length in AES_encrypt function while decryption

  18. 18

    Unable to decrypt AES encrypted string from Objective C

  19. 19

    How to decrypt a C# encrypted AES string in java

  20. 20

    Size of data after AES decryption

  21. 21

    I have 2 Same value object of String Buffer Class. String equals() method Showing False result Why?

  22. 22

    Returning after ajax call prints false

  23. 23

    AES decryption after sending message to IP address

  24. 24

    Problems retrieving stored AES encrypted string in Python

  25. 25

    Why does this java string comparison prints false?

  26. 26

    swift aes 128 decryption with string input

  27. 27

    JavaScript date compare fails but prints same value

  28. 28

    Decrypt a base64 encoded and aes encrypted string using RNCryptor

  29. 29

    decryption string that is encrypted from c#

HotTag

Archive