fengketrade/doc/demo/MCipherDecode.java

108 lines
3.1 KiB
Java
Raw Normal View History

2025-10-20 15:29:15 +08:00
package B2C.PUB;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class MCipherDecode {
static {
if(Security.getProvider("BC")==null)
{
Security.addProvider(new BouncyCastleProvider());
}
}
private String encryptKey = "12345678";
public MCipherDecode(String key)
{
encryptKey = key.substring(0, 8);
}
public String getEncryptKey() {
return encryptKey;
}
public void setEncryptKey(String encryptKey) {
this.encryptKey = encryptKey.substring(0,8);
}
private static byte[] getSrcBytes(byte[] srcBytes, byte[] wrapKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidAlgorithmParameterException
{
SecretKeySpec key = new SecretKeySpec(wrapKey, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding","BC");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] cipherText = cipher.doFinal(srcBytes);
return cipherText;
}
public static byte[] DecodeBase64String(String base64Src) throws IOException
{
BASE64Decoder de = new BASE64Decoder();
byte[] base64Result = de.decodeBuffer(base64Src);
return base64Result;
}
public String getDecodeString(String urlString) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidAlgorithmParameterException
{
String tempString = URLDecoder.decode(urlString, "iso-8859-1");
String basedString = tempString.replaceAll(",", "+");
byte[] tempBytes = DecodeBase64String(basedString);
byte[] tempSrcBytes = getSrcBytes(tempBytes,encryptKey.getBytes("iso-8859-1"));
return new String(tempSrcBytes,"iso-8859-1");
}
public static void main(String[] agrs){
String key = "f6528d5c335b7092fc9ec1b3020111";
String str = "÷<EFBFBD><EFBFBD><EFBFBD><EFBFBD>|6214662020019275";
String cipherdURL = "AWWo2KKeATj6XxRglo7uaR0yZ2QQtCW%2C";
//ʹ<><CAB9>MCipherDecode.java<76><61><EFBFBD>е<EFBFBD>getDecodeString(String urlString)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD>ܣ<EFBFBD><DCA3><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
try {
MCipherDecode mcd = new MCipherDecode(key);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ
String decodedString = mcd.getDecodeString(cipherdURL);//<2F><><EFBFBD><EFBFBD>
byte[] tempByte = decodedString.getBytes("ISO-8859-1");
decodedString = new String(tempByte,"GBK");
System.out.println("decodedString-- " + decodedString);
}catch(Exception e){
e.printStackTrace();
}
}
}