Inconsistent file encoding read and write file garbled solution

Source: Internet
Author: User

These days to get an app in Android data encryption. In order to avoid the encryption and decryption algorithms being cracked, I will become the core of the encryption and decryption algorithms encapsulated together using JNI, only the interface exposes the Java layer.

The workflow is this:

1. Encrypt the data through the encryption and decryption tool written by oneself;

2, put the encrypted data in the Android asserts directory;

3. Copy the data from the asserts directory to a hidden directory when the data is first used;

4. Decrypt the files in the hidden directory.


in the encryption tool to encrypt the data, in the process of decrypting the data file, found that the decrypted file is the original file size of twice times, and is all garbled, Trace found that the main problem is now the 3rd step, the encoding of the read and write files inconsistent caused the file garbled, I used for example the following method to read the contents of the asserts directory:

public static string Readfilefromassets (context context, string fileName) throws IOException {if (null = = Context | | Textutils.isempty (fileName) {return null;} Assetmanager Assetmanager = Context.getassets (); InputStream input = Assetmanager.open (fileName); Bytearrayoutputstream output = new Bytearrayoutputstream (); byte[] buffer = new Byte[1024];int length = 0;while (length = Input.read (buffer))! =-1) {output.write (buffer, 0, length);} Output.close (); Input.close (); return output.tostring ();}

The problem is that I will read the content into a string, Bytearrayoutputstream the ToString method to convert the contents of the file to Utf-8 encoding, but the C language read and write files by default is Asii encoding method, Read-Write file encoding inconsistent result in garbled problem, the problem found, the solution is also out, such as the following:

public static byte[] Readfilefromassets (context context, String FileName) throws IOException {if (null = = Context | | Textutils.isempty (fileName) {return null;} Assetmanager Assetmanager = Context.getassets (); InputStream input = Assetmanager.open (fileName); Bytearrayoutputstream output = new Bytearrayoutputstream (); byte[] buffer = new Byte[1024];int length = 0;while (length = Input.read (buffer))! =-1) {output.write (buffer, 0, length);} Output.close (); Input.close (); return Output.tobytearray ();//return output.tostring ();}

Reads the contents of the asserts file in the form of a byte array, returning it.


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Inconsistent file encoding read and write file garbled solution

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.