Android AES Encryption error handling: Javax.crypto.illegalblocksizeexception:error:1e00007b:cipher functions:OPENSSL_internal:WRONG_ Final_block_length

Source: Internet
Author: User
Tags decrypt response code

I. Description of the problem

Today writes the AES Add/Decrypt function apk, the assumption is four controls (testing, the interface ugly This kind of thing please ignore)

A edit box----used to enter the string to encrypt

A text box----used to output the encrypted string, and the decrypted string when the decryption button is clicked after encryption

An encryption button----Click to encrypt

A decryption button----Click to decrypt

The interface is as follows:

Click on the encryption no problem, but then click the decryption time has been error: "W/system.err:javax.crypto.illegalblocksizeexception:error:1e00007b:cipher functions:o Penssl_internal:wrong_final_block_length "

Second, error tracking

Because the class used for encryption and decryption is used in university cryptography, the basic certainty will not be a problem, so the problem is determined in passing the parameters of the problem.

2.1 toString () error using

The first observation is that regardless of how the contents and length of the encrypted string changes, the result of the encryption is always "[[[email] protected]" in the form, as shown in

This is definitely a problem----AES as a high-strength encryption algorithm encrypted content changes after the encryption results are the same as this is not possible, the length of the symmetric encryption encryption result does not vary with the content of the encryption is not possible.

The Add and Decrypt button clicks the event response code as follows:

debugging can see "[[Email protected]" More like the ID of byte[] instead of its contents

That is, you want to use byte_encrypt_result.tostring () to turn byte[] into a string, though it should look like this but it doesn't work.

2.2 byte[] Turn string again byte[] Content changes cause error

ToString () can not be used, and then see this little brother said can be through the new String (byte[]), the article is written very clear a look is a master I believe him. So the code is changed to the bottom.

Encryption result is a bunch of garbled, very good, this is what we want

But click to decrypt, still the same error "W/system.err:javax.crypto.illegalblocksizeexception:error:1e00007b:cipher Functions:openssl_ Internal:wrong_final_block_length "

The code logic seems to have no problem at all, how can it be an error? After repeated debugging, it is observed that the Byte_encrypt_result content and length obtained in the decryption are completely different (in front of byte[16] here byte[28])

That is, in new String (byte[]) to ToString (). GetBytes () retrieving byte[] In this process, byte[] content has changed

Baidu "byte[" turn string again back to byte[] ", see like saying new String () by default using UTF-8 encoding GetBytes () default using ISO8859-1 encoding raises the issue, specifying new String () and GetBytes () Unified use of iso8859-1 can solve the problem. (I think the Java coding problem is quite annoying, especially in the read and write files and network communication, the problem can be solved just do not want to delve into it)

So the final code is as follows:

Successful decryption of the program:

Android AES Encryption error handling: Javax.crypto.illegalblocksizeexception:error:1e00007b:cipher functions:OPENSSL_internal:WRONG_ Final_block_length

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.