Des Java source code

Source: Internet
Author: User
* Copyright: Guangzhou dianxun Technology Co., Ltd. 2003

* Created on 2004-2-24 by liudong

*/

Import java. Security .*;

Import javax. crypto. cipher;

Import javax. crypto. secretkey;

Import javax. crypto. secretkeyfactory;

Import javax. crypto. spec. deskeyspec;

/**

* String tool set

* @ Author liudong

*/

Public class stringutils {

Private Static final string password_crypt_key = "_ jdlog _";

Private Final Static string des = "des ";

/**

* Encryption

* @ Param SRC Data Source

* @ Param key, which must be a multiple of 8

* @ Return returns encrypted data.

* @ Throws exception

*/

Public static byte [] encrypt (byte [] SRC, byte [] Key) throws exception {

// The DES algorithm requires a trusted random number Source

Securerandom sr = new securerandom ();

// Create an eyspec object from the original key data

Deskeyspec DKS = new deskeyspec (key );

// Create a key factory and use it to convert the keyspec

// A secretkey object

Secretkeyfactory keyfactory = secretkeyfactory. getinstance (DES );

Secretkey securekey = keyfactory. generatesecret (DKS );

// The cipher object actually completes the encryption operation

Cipher cipher = cipher. getinstance (DES );

// Use the key to initialize the cipher object

Cipher. INIT (Cipher. encrypt_mode, securekey, Sr );

// Now, obtain and encrypt the data

// Perform the encryption operation

Return cipher. dofinal (SRC );

}

/**

* Decryption

* @ Param SRC Data Source

* @ Param key, which must be a multiple of 8

* @ Return returns the decrypted raw data.

* @ Throws exception

*/

Public static byte [] decrypt (byte [] SRC, byte [] Key) throws exception {

// The DES algorithm requires a trusted random number Source

Securerandom sr = new securerandom ();

// Create an eyspec object from the original key data

Deskeyspec DKS = new deskeyspec (key );

// Create a key factory and use it to convert the keyspec object

// A secretkey object

Secretkeyfactory keyfactory = secretkeyfactory. getinstance (DES );

Secretkey securekey = keyfactory. generatesecret (DKS );

// The cipher object actually completes the decryption operation

Cipher cipher = cipher. getinstance (DES );

// Use the key to initialize the cipher object

Cipher. INIT (Cipher. decrypt_mode, securekey, Sr );

// Now, obtain and decrypt the data

// Perform the decryption operation.

Return cipher. dofinal (SRC );

}

/**

* Password decryption

* @ Param data

* @ Return

* @ Throws exception

*/

Public final static string decrypt (string data ){

Try {

Return new string (decrypt (hex2byte (data. getbytes ()),

Password_crypt_key.getbytes ()));

} Catch (exception e ){

}

Return NULL;

}

/**

* Password encryption

* @ Param Password

* @ Return

* @ Throws exception

*/

Public final static string encrypt (string password ){

Try {

Return byte2hex (encrypt (password. getbytes (), password_crypt_key.getbytes ()));
} Catch (exception e ){

}

Return NULL;

}

/**

* Two-line conversion string

* @ Param B

* @ Return

*/

Public static string byte2hex (byte [] B ){

String HS = "";

String stmp = "";

For (INT n = 0; n <B. length; n ++ ){

Stmp = (Java. Lang. Integer. tohexstring (B [N] & 0xff ));

If (stmp. Length () = 1)

HS = HS + "0" + stmp;

Else

HS = HS + stmp;

}

Return HS. touppercase ();

}

Public static byte [] hex2byte (byte [] B ){

If (B. Length % 2 )! = 0)

Throw new illegalargumentexception ("the length is not an even number ");

Byte [] b2 = new byte [B. Length/2];

For (INT n = 0; n <B. length; n + = 2 ){

String item = new string (B, n, 2 );

B2 [n/2] = (byte) integer. parseint (item, 16 );

}

Return B2;

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.