How to encrypt a code in Android

Source: Internet
Author: User

A requirement of a recent project is that the function of a module can be used only by entering the serial number. enter the serial number, encrypt it, and package it into a file and store it in sdcard. Verify whether such a file exists under sdcard next time.

How to encrypt and decrypt data in Java:

There are two classes:

Keygenerator: This class provides (symmetric) key generator functions.

Cipher: This class provides the password function for encryption and decryption.

During encryption and decryption, cipher uses the key generated by keygenerator to encrypt (generate ciphertext) and decrypt (PARSE ciphertext)

Public class main {public static void main (string [] ARGs) throws nosuchalgorithmexception, unsupportedencodingexception {// ------------------- encryption process ----------------------------- // generates a key, A "des" algorithm keygenerator = keygenerator needs to be associated. getinstance ("des"); secretkey generator ey = keygenerator. generatekey (); // infostring info = "12345678" to be encrypted; // output the ciphertext content before encryption system. out. println ("" + info); // generate a randomsecurerandom sr = new securerandom (); byte [] cipherbyteencrypt = NULL; try {cipher C1 = cipher. getinstance ("des"); c1.init (cipher. encrypt_mode, cipher ey, Sr); // generate the ciphertext cipherbyteencrypt = c1.dofinal (info. getbytes ();} catch (exception e) {e. printstacktrace ();} // output the encrypted ciphertext content system. out. println ("" + new string (cipherbyteencrypt, "ISO-8859-1"); // --------------------- decryption process ------------------------------- // generate a random sr = new securerandom (); byte [] cipherbytedecrypt = NULL; try {cipher C1 = cipher. getinstance ("des"); c1.init (cipher. decrypt_mode, cipher ey, Sr); // parse the ciphertext cipherbytedecrypt = c1.dofinal (cipherbyteencrypt);} catch (exception e) {e. printstacktrace ();} system. out. println ("" + new string (cipherbytedecrypt, "ISO-8859-1 "));}}

Output:

12345678
3 m ± @*:?;??? + J ??? ----------------> This is the ciphertext
12345678

 

Add one question:

Why is ISO-8859-1 used in code.

See the following code:

Bytebytes [] = new byte [] {50, 0,-1, 28,-24 };

String string = new string (bytes );

Byte [] ret = string. getbytes ();

Check the RET data and find that the data is 50, 0,-17,-65,-67, 28,-17,-65,-67. The data is not the original data.

Use the following code to obtain the original data:

Bytebytes [] = new byte [] {50, 0,-1, 28,-24 };

Stringisostring = new string (bytes, "ISO-8859-1 ");

Byte [] isoret = isostring. getbytes ("ISO-8859-1 ");

Why? The reason is that the first method is to generate a string using UTF-8 encoding by default, with system. getproperty ("Sun. JNU. encoding") You can get Android default encoding is UTF-8. The UTF-8 is a variable length encoding, and the original byte array is changed. While ISO8859-1 is usually called Latin-1, Latin-1 includes additional characters that are indispensable for writing all Western European languages, 0 ~ The character of 127 is the same as the ASCII code. It is a single-byte encoding method, so that the byte array in the string generated in the second method is the same as the original byte array. The use of other encoding in new string, such as GBK, gb2312, will also lead to changes in the byte array, so to get a single byte array in the string, you should use iso8859-1 encoding.

 

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.