Cryptographic decryption Tool

Source: Internet
Author: User
Tags md5 encryption

Package Com.neusoft.mid.cloong.web.page.common;

Import Java.net.URLDecoder;
Import Java.net.URLEncoder;
Import Java.security.MessageDigest;

Import Javax.crypto.Cipher;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESedeKeySpec;

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


public class Endecryptutil {

/**
* For MD5 encryption
*
* @param String
* The original Spkey
* @return byte[] Specifies that the encryption method is MD5 after byte[]
*/

Private byte[] MD5 (String strsrc) {
byte[] Returnbyte = null;
try {
MessageDigest MD5 = messagedigest.getinstance ("MD5");
Returnbyte = Md5.digest (Strsrc.getbytes ("UTF-8"));
} catch (Exception e) {
E.printstacktrace ();
}
return returnbyte;
}

/**
* Get 3-des key According to the interface specification, the key is 24 bytes, MD5 encryption is 16 bytes, so the back 8 bytes of 0
*
* @param String
* The original Spkey
* @return byte[] Specifies that the encryption method is MD5 after byte[]
*/

Private byte[] Getenkey (String spkey) {
byte[] Deskey = null;
try {
Byte[] DesKey1 = MD5 (Spkey);
Deskey = new BYTE[24];
int i = 0;
while (I < deskey1.length && I < 24) {
Deskey[i] = Deskey1[i];
i++;
}
if (I < 24) {
Deskey[i] = 0;
i++;
}
} catch (Exception e) {
E.printstacktrace ();
}

return deskey;
}

/**
* 3-des Encryption
*
* @param byte[] src to be 3-des encrypted byte[]
* @param byte[] enkey 3-des encryption key
* @return byte[] 3-des encrypted byte[]
*/

Public byte[] Encrypt (byte[] src, byte[] enkey) {
byte[] EncryptedData = null;
try {
Desedekeyspec DKs = new Desedekeyspec (Enkey);
Secretkeyfactory keyfactory = secretkeyfactory
. getinstance ("Desede");
Secretkey key = Keyfactory.generatesecret (DKS);
Cipher Cipher = cipher.getinstance ("Desede");
Cipher.init (Cipher.encrypt_mode, key);
EncryptedData = cipher.dofinal (src);
} catch (Exception e) {
E.printstacktrace ();
}
return EncryptedData;
}

/**
* BASE64 Encoding of strings
*
* @param byte[] src character to encode
*
* String encoded with @return string
*/

Public String getbase64encode (byte[] src) {
String requestvalue = "";
try {
Base64encoder base64en = new Base64encoder ();
Requestvalue = Base64en.encode (src);
System.out.println (Requestvalue);
} catch (Exception e) {
E.printstacktrace ();
}

return requestvalue;
}

/**
* Remove the string newline symbol base64 encoded 3-des data, the resulting string has a newline symbol, must be removed, otherwise uni-wise platform parsing stub will not succeed,
* Prompt "SP authentication failed". In the process of development, because this problem left me helpless, a friend told me I can ask Unicom to a piece of encrypted text, and then go to their own generated string comparison,
* This is a good way to debug. I finally found that the only difference in the string I generated was that there was more line breaks. I also wrote the stub request program in the C # language and did not find this problem.
*
*/

private string filter (String str) {
String output = null;
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < str.length (); i++) {
int ASC = Str.charat (i);
if (ASC! = && ASC! = 13)
Sb.append (Str.subsequence (i, i + 1));
}
Output = new String (SB);
return output;
}

/**
* Urldecoder.encode (strencoding) Encoding of strings
*
* @param String
* src string to encode
*
* String encoded with @return string
*/

public string Geturlencode (string src) {
String requestvalue = "";
try {

Requestvalue = Urlencoder.encode (src);
} catch (Exception e) {
E.printstacktrace ();
}

return requestvalue;
}

/**
* 3-des Encryption
*
* @param String
* src to be 3-des encrypted string
* @param String
* Spkey assigned by Spkey
* @return String 3-des Encrypted string
*/

public string Get3desencrypt (string src, string spkey) {
String requestvalue = "";
try {

Get the 3-des key.
byte[] Enkey = Getenkey (Spkey);
Content to be 3-des encrypted in/"utf-16le/" byte fetching
byte[] Src2 = src.getbytes ("Utf-16le");
bytes of content that are 3-des encrypted
byte[] EncryptedData = Encrypt (Src2, Enkey);

3-des encrypted content for BASE64 encoding
String base64string = Getbase64encode (EncryptedData);
BASE64 encoding after line break is removed
String Base64encrypt = filter (base64string);

The process of escaping HTML control codes in BASE64 encoding
Requestvalue = Geturlencode (Base64encrypt);
System.out.println (Requestvalue);
} catch (Exception e) {
E.printstacktrace ();
}

return requestvalue;
}

/**
* Urldecoder.decode (strencoding) decoding of strings
*
* @param String
* SRC string to decode
*
* @return string after decoding
*/

public string Geturldecoderdecode (string src) {
String requestvalue = "";
try {

Requestvalue = Urldecoder.decode (src);
} catch (Exception e) {
E.printstacktrace ();
}

return requestvalue;
}

/**
*
* 3-des decryption (secret key is equivalent to encrypted key).
*
* @param byte[] src to be 3-des decrypted byte[]
* @param String
* Spkey assigned by Spkey
* @return String 3-des the decrypted string
*/
public string DeCrypt (byte[] debase64, string spkey) {
String Strde = null;
Cipher Cipher = null;
try {
cipher = Cipher.getinstance ("Desede");
byte[] key = Getenkey (Spkey);
Desedekeyspec DKs = new Desedekeyspec (key);
Secretkeyfactory keyfactory = secretkeyfactory
. getinstance ("Desede");
Secretkey SKey = Keyfactory.generatesecret (DKS);
Cipher.init (Cipher.decrypt_mode, SKey);
byte ciphertext[] = cipher.dofinal (debase64);
Strde = new String (ciphertext, "utf-16le");
} catch (Exception ex) {
Strde = "";
Ex.printstacktrace ();
}
return Strde;
}

/**
* 3-des Decryption
*
* @param String
* src to be 3-des decrypted string
* @param String
* Spkey assigned by Spkey
* @return String 3-des Encrypted string
*/

public string Get3desdecrypt (string src, string spkey) {
String requestvalue = "";
try {

Get the 3-des key.

Urldecoder.decodetml control code to escape the process
String urlvalue = Geturldecoderdecode (src);

3-des encrypted content for BASE64 encoding

Base64decoder Base64decode = new Base64decoder ();
byte[] Base64dvalue = Base64decode.decodebuffer (Urlvalue);

Content to be 3-des encrypted in/"utf-16le/" byte fetching

Requestvalue = DeCrypt (Base64dvalue, Spkey);
} catch (Exception e) {
E.printstacktrace ();
}
return requestvalue;
}

public static void Main (string[] args) {
Endecryptutil test = new Endecryptutil ();
String oldstring = "Colombia Mighty";

String Spkey = "Neusoft";
System.out.println ("1. The assigned Spkey is: "+ Spkey);
System.out.println ("2. The content is: "+ oldstring);
String revalue = Test.get3desencrypt (oldstring, Spkey);
Revalue = Revalue.trim (). Intern ();
System.out.println ("Content after 3-des encryption:" + revalue);
String reValue2 = Test.get3desdecrypt (revalue, Spkey);
System.out.println ("Content after 3-des decryption:" + reValue2);
}
}

Cryptographic decryption Tool

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.