A Java configuration file encryption decryption tool class sharing _java

Source: Internet
Author: User
Tags base64 decrypt generator

Common such as: Database user password, SMS platform user password, system calibration of fixed passwords.
This tool class refers to the "spring.3.x Enterprise application Development Combat" book 5.3 of the implementation.
The complete code and comment information are as follows:

Copy Code code as follows:

Package Com.cncounter.util.comm;

Import Java.security.Key;
Import Java.security.SecureRandom;

Import Javax.crypto.Cipher;
Import Javax.crypto.KeyGenerator;

Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;

public class Desutils {

Secret key
private static key key;
Key seed
private static String Key_str = "encrypt@cncounter.com";
Constant
public static final String utf_8 = "UTF-8";
public static final String DES = "des";

Static initialization
static{
try {
KEY Builder
Keygenerator generator = keygenerator.getinstance (DES);
initialization, secure random operator
Generator.init (New SecureRandom (Key_str.getbytes (Utf_8)));
Generate key
Key = Generator.generatekey ();
Generator = null;
catch (Exception e) {
throw new RuntimeException (e);
}
}

/**
* Encrypt source string, return BASE64 encoded encrypted string
* @param source string, plaintext
* @return Ciphertext string
*/
public static string encode (string source) {
try {
Get an array of bytes based on the encoding format
byte[] sourcebytes = source.getbytes (utf_8);
DES Encryption Mode
Cipher Cipher = cipher.getinstance (DES);
Cipher.init (Cipher.encrypt_mode, key);
Encrypted byte array
byte[] encryptsourcebytes = cipher.dofinal (sourcebytes);
BASE64 Encoder
Base64encoder Base64encoder = new Base64encoder ();
Return Base64encoder.encode (encryptsourcebytes);
catch (Exception e) {
Throw is also considered a return path.
throw new RuntimeException (e);
}
}

/**
* Decode/Decrypt strings encrypted by this tool class encode () method
* @param encrypted encrypted string, i.e. ciphertext
* @return PlainText string
*/
public static string decode (string encrypted) {
BASE64 Decoder
Base64decoder Base64decoder = new Base64decoder ();
try {
First, the Base64 decoding
byte[] Cryptedbytes = Base64decoder.decodebuffer (encrypted);
DES decryption Mode
Cipher Cipher = cipher.getinstance (DES);
Cipher.init (Cipher.decrypt_mode, key);
Decoded byte array
byte[] decryptstrbytes = cipher.dofinal (cryptedbytes);
Converts a byte array into a string using the given encoding format
return new String (Decryptstrbytes, utf_8);
catch (Exception e) {
This form does fit the processing tool class
throw new RuntimeException (e);
}
}
Unit Test
public static void Main (string[] args) {
Strings that need to be encrypted
String email = "renfufei@qq.com";
Encryption
String encrypted = desutils.encode (email);
Decrypt
String decrypted = Desutils.decode (encrypted);
Output results;
System.out.println ("email:" + email);
SYSTEM.OUT.PRINTLN ("Encrypted:" + encrypted);
System.out.println ("decrypted:" + decrypted);
System.out.println ("Email.equals (decrypted):" + email.equals (decrypted));
}
}

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.