Java Algorithm for base64 encryption and decryption!

Source: Internet
Author: User

Package JP. co. sumitomocorp. intra1china. Ess. batch. event;

Import java. Io. ioexception;
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 desencrypter {

Static key;

Public static void main (string [] ARGs ){

// Obtain the Encrypted Key
String testdata = "mjgwmdawmdi = ";
// DES encryption


// Base64decoder decoder = new base64decoder ();
// Byte [] decodedbytes = NULL;
// Try {
// Decodedbytes = decoder. decodebuffer (testdata );
//} Catch (ioexception e ){
// E. printstacktrace ();
//}
// New string (decodedbytes );

String enstr = getdesstring (testdata );
System. Out. println ("ciphertext:" + enstr );
/// Des decryption
// String destr = getdesstring (enstr );
// System. Out. println ("plaintext:" + destr );

}

/**
* Encrypted string plaintext input and string ciphertext output
*/
Public static string getencstring (string strming ){
// Ciphertext byte array
Byte [] bytemi = NULL;
// Plaintext byte array
Byte [] byteming = NULL;
// Ciphertext output
String strmi = "";

Base64encoder base64en = new base64encoder ();
Try {
Byteming = strming. getbytes ("utf8 ");
Bytemi = getenccode (byteming );
Strmi = base64en. encode (bytemi );
} Catch (exception e ){
Throw new runtimeexception ("error initializing sqlmap class. Cause :"
+ E );
} Finally {
Base64en = NULL;
Byteming = NULL;
Bytemi = NULL;
}
Return strmi;
}

/**
* Decryption is input in string ciphertext and output in string plaintext.
*
* @ Param strmi
* @ Return
*/
Public static string getdesstring (string strmi ){
Base64decoder decoder = new base64decoder ();
Byte [] decodedbytes = NULL;
String A = "";
Try {
Decodedbytes = decoder. decodebuffer (strmi );
A = new string (decodedbytes, "UTF-8 ");
} Catch (ioexception e ){
E. printstacktrace ();
}
Return;
}

/**
* Encryption is input in byte [] plaintext, and byte [] ciphertext is output.
*
* @ Param bytes
* @ Return
*/
Private Static byte [] getenccode (byte [] bytes ){
Byte [] bytefina = NULL;
Cipher cipher;
Try {
Cipher = cipher. getinstance ("des ");
Cipher. INIT (Cipher. encrypt_mode, key );
Bytefina = cipher. dofinal (bytes );
} Catch (exception e ){
Throw new runtimeexception ("error initializing sqlmap class. Cause :"
+ E );
} Finally {
Cipher = NULL;
}
Return bytefina;
}

/**
* Decryption is input in byte [] ciphertext and output in byte [] plaintext
*
* @ Param byted
* @ Return
*/
Private Static byte [] getdescode (byte [] byted ){
Cipher cipher;
Byte [] bytefina = NULL;
Try {
Cipher = cipher. getinstance ("des ");
Cipher. INIT (Cipher. decrypt_mode, key );
Bytefina = cipher. dofinal (byted );
} Catch (exception e ){
Throw new runtimeexception ("error initializing sqlmap class. Cause :"
+ E );
} Finally {
Cipher = NULL;
}
Return bytefina;
}

/**
* Generate a key based on the parameter
*/
Public static void setkey (string strkey ){
Try {
Keygenerator _ generator = keygenerator. getinstance ("des ");
_ Generator. INIT (New securerandom (strkey. getbytes ()));
Key = _ generator. generatekey ();
_ Generator = NULL;
} Catch (exception e ){
Throw new runtimeexception ("error initializing sqlmap class. Cause :"
+ E );
}
}

}

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.