Original address: http://blog.csdn.net/maxuyang1987/article/details/10226601
/**
* DES encryption
* @param plaindata
* @param secretkey
* @return
* @throws Exception
*/
public static string encryption (String plaindata) throws exception{
Cipher Cipher = null;
try {
cipher = Cipher.getinstance (des_algorithm);
Cipher.init (Cipher.encrypt_mode, GenerateKey (KEY));
catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
catch (Nosuchpaddingexception e) {
E.printstacktrace ();
}catch (InvalidKeyException e) {
}
try {
To prevent decryption times javax.crypto.IllegalBlockSizeException:Input length must be multiple of the 8 when decrypting with padded cipher exception ,
Cannot convert an encrypted byte array directly into a string
byte[] buf = cipher.dofinal (Plaindata.getbytes ());
Return Base64utils.encode (BUF);
catch (Illegalblocksizeexception e) {
E.printstacktrace ();
throw new Exception ("Illegalblocksizeexception", e);
catch (Badpaddingexception e) {
E.printstacktrace ();
throw new Exception ("Badpaddingexception", e);
}
}
/**
* des decryption
* @param secretdata
* @param secretkey
* @return
* @throws Exception
*/
public static string decryption (string secretdata) throws exception{
Cipher Cipher = null;
try {
cipher = Cipher.getinstance (des_algorithm);
Cipher.init (Cipher.decrypt_mode, GenerateKey (KEY));
catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
throw new Exception ("NoSuchAlgorithmException", e);
catch (Nosuchpaddingexception e) {
E.printstacktrace ();
throw new Exception ("Nosuchpaddingexception", e);
}catch (InvalidKeyException e) {
E.printstacktrace ();
throw new Exception ("InvalidKeyException", e);
}
try {
byte[] buf = cipher.dofinal (Base64utils.decode (Secretdata.tochararray ()));
return new String (BUF);
catch (Illegalblocksizeexception e) {
E.printstacktrace ();
throw new Exception ("Illegalblocksizeexception", e);
catch (Badpaddingexception e) {
E.printstacktrace ();
throw new Exception ("Badpaddingexception", e);
}
}
/**
* Get secret key
*
* @param secretkey
* @return
* @throws nosuchalgorithmexception
*/
private static Secretkey GenerateKey (String secretkey) throws nosuchalgorithmexception{
SecureRandom securerandom = new SecureRandom (Secretkey.getbytes ());
SecureRandom securerandom = securerandom.getinstance ("sha1prng");
Securerandom.setseed (Secretkey.getbytes ());
Generates a Keygenerator object for our chosen des algorithm
Keygenerator kg = null;
try {
kg = keygenerator.getinstance (des_algorithm);
catch (NoSuchAlgorithmException e) {
}
Kg.init (SecureRandom);
Kg.init (SecureRandom);
Generate key
return Kg.generatekey ();
}
Static Class Base64utils {
Static private char[] Alphabet = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=". ToCharArray ();
Static private byte[] codes = new byte[256];
static {
for (int i = 0; i < 256; i++)
Codes[i] =-1;
for (int i = ' A '; I <= ' Z '; i++)
Codes[i] = (byte) (i-' A ');
for (int i = ' a '; I <= ' z '; i++)
Codes[i] = (byte) (+ I-' a ');
for (int i = ' 0 '; I <= ' 9 '; i++)
Codes[i] = (byte) (+ I-' 0 ');
codes[' + '] = 62;
codes['/'] = 63;
}
/**
* Encode raw data as base64 encoding
*/
static public String encode (byte[] data) {
Char[] out = new char[((Data.length + 2)/3) * 4];
for (int i = 0, index = 0; i < data.length i + = 3, index + + 4) {
Boolean quad = false;
Boolean trip = false;
int val = (0xFF & (int) data[i]);
Val <<= 8;
if ((i + 1) < Data.length) {
Val |= (0xFF & (int) Data[i + 1]);
Trip = true;
}
Val <<= 8;
if ((i + 2) < Data.length) {
Val |= (0xFF & (int) Data[i + 2]);
Quad = true;
}
Out[index + 3] = alphabet[(quad?) (Val & 0x3F): 64)];
Val >>= 6;
Out[index + 2] = alphabet[(trip?) (Val & 0x3F): 64)];
Val >>= 6;
Out[index + 1] = alphabet[val & 0x3F];
Val >>= 6;
Out[index + 0] = alphabet[val & 0x3F];
}
Return a new String (out);
}
/**
* Decode Base64 encoded data into raw data
*/
Static public byte[] Decode (char[] data) {
int len = ((data.length + 3)/4) * 3;
if (data.length > 0 && data[data.length-1] = = ' = ')
--len;
if (Data.length > 1 && data[data.length-2] = = ' = ')
--len;
Byte[] out = new Byte[len];
int shift = 0;
int accum = 0;
int index = 0;
for (int ix = 0; ix < data.length; ix++) {
int value = Codes[data[ix] & 0xFF];
if (value >= 0) {
Accum <<= 6;
SHIFT + 6;
Accum |= value;
if (Shift >= 8) {
shift = 8;
out[index++] = (byte) ((Accum >> shift) & 0xff);
}
}
}
if (index!= out.length)
throw new Error ("miscalculated data length!");
return out;
}
}
public static void Main (string[] a) throws exception{
String input = "Test";
String result = encryption (input);
SYSTEM.OUT.PRINTLN (result);
System.out.println (decryption (result));
h+ac2aysrv6cwn3nch1hiw==
}
}