Java encryption and decryption

Source: Internet
Author: User
//////////
Private Static string strdefaultkey = "# % ^ &";


/**
* Convert the byte array to a string that represents the hexadecimal value, for example, byte [] {8, 18} To 0813, and public static byte []
* Hexstr2bytearr (string strin) is a reversible conversion process.
*
* @ Param arrb
* Byte array to be converted
* @ Return refers to the converted string.
* @ Throws exception
* This method does not handle any exceptions. All exceptions are thrown.
*/
Private Static string bytearr2hexstr (byte [] arrb) throws exception {
Int ilen = arrb. length;
// Each byte can be expressed by two characters, so the string length is twice the length of the array.
Stringbuffer sb = new stringbuffer (ilen * 2 );
For (INT I = 0; I <ilen; I ++ ){
Int inttmp = arrb [I];
// Convert a negative number to a positive number
While (inttmp <0 ){
Inttmp = inttmp + 256;
}
// The number smaller than 0f must be set to 0.
If (inttmp <16 ){
SB. append ("0 ");
}
SB. append (integer. tostring (inttmp, 16 ));
}
Return sb. tostring ();
}

/**
* Convert the hexadecimal value string to a byte array and public static string bytearr2hexstr (byte [] arrb)
* Reversible conversion process
*
* @ Param strin
* String to be converted
* @ Return: converted byte array
* @ Throws exception
* This method does not handle any exceptions. All exceptions are thrown.
*
*/
Private Static byte [] hexstr2bytearr (string strin) throws exception {
Byte [] arrb = strin. getbytes ();
Int ilen = arrb. length;

// Two characters indicate one byte, so the length of the byte array is the string length divided by 2
Byte [] arrout = new byte [ilen/2];
For (INT I = 0; I <ilen; I = I + 2 ){
String strtmp = new string (arrb, I, 2 );
Arrout [I/2] = (byte) integer. parseint (strtmp, 16 );
}
Return arrout;
}


/**
* Encrypted byte array
*
* @ Param arrb
* Byte array to be encrypted
* @ Return encrypted byte array
* @ Throws exception
*/
Public static byte [] encrypt (byte [] arrb) throws exception {
Key key = getkey (strdefaultkey. getbytes ());
Cipher cipher = cipher. getinstance ("des ");
Cipher. INIT (Cipher. encrypt_mode, key );
Return cipher. dofinal (arrb );
}

/**
* Encrypted string
*
* @ Param strin
* String to be encrypted
* @ Return encrypted string
* @ Throws exception
*/
Public static string encrypt (string strin) throws exception {
Return bytearr2hexstr (encrypt (strin. getbytes ()));
}

/**
* Decrypt the byte array
*
* @ Param arrb
* Byte array to be decrypted
* @ Return the decrypted byte array
* @ Throws exception
*/
Public static byte [] decrypt (byte [] arrb) throws exception {
Key key = getkey (strdefaultkey. getbytes ());
Cipher cipher = cipher. getinstance ("des ");
Cipher. INIT (Cipher. decrypt_mode, key );
Return cipher. dofinal (arrb );
}

/**
* Decrypt a string
*
* @ Param strin
* String to be decrypted
* @ Return refers to the decrypted string.
* @ Throws exception
*/
Public static string decrypt (string strin) throws exception {
Return new string (decrypt (hexstr2bytearr (strin )));
}

/**
* Generate a key from a specified string. If the length of the byte array required for the key is 8 or less bits, the key is supplemented with 0. If the length exceeds 8 bits, the first 8 bits are used.
*
* @ Param arrbtmp
* The byte array that constitutes the string
* @ Return indicates the key generated.
* @ Throws java. Lang. Exception
*/
Private Static key getkey (byte [] arrbtmp) throws exception {
// Create an empty 8-byte array (default value: 0)
Byte [] arrb = new byte [8];

// Convert the original byte array to 8 bits
For (INT I = 0; I <arrbtmp. Length & I <arrb. length; I ++ ){
Arrb [I] = arrbtmp [I];
}

// Generate the key
Key key = new javax. crypto. spec. secretkeyspec (arrb, "des ");

Return key;
}


////////////////

 

 

Call:

String E = "abcdefg123 ";
String d = utils. Encrypt (E );

Log. I ("test", "encrypt:" + D );
Log. I ("test", "decrypt:" + utils. decrypt (d ));

 

 

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.