package ssh.platform.user.util;
import java.io.ByteArrayInputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Des3 {
private static final byte[] key = {(byte)0xF8, (byte)0x5B,(byte)0xF1, (byte)0x80,
(byte)0xC1, (byte)0xFE, 0x08, 0x34, 0x7F, (byte)0xA4, (byte)0xD6,0x76,
(byte)0x73,(byte)0x5E,
(byte)0x62, 0x1C,(byte)0xA2,0x38, 0x7F,(byte)0xD6, 0x0E,
(byte)0x52, 0x67, 0x45
};
private static final byte[] keyiv = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
public static void main(String[] args) throws Exception {
Des3 des3 =new Des3();
byte[] key = {(byte)0xF8, (byte)0x5B,(byte)0xF1, (byte)0x80,
(byte)0xC1, (byte)0xFE, 0x08, 0x34, 0x7F, (byte)0xA4, (byte)0xD6,0x76,
(byte)0x73,(byte)0x5E,
(byte)0x62, 0x1C,(byte)0xA2,0x38, 0x7F,(byte)0xD6, 0x0E,
(byte)0x52, 0x67, 0x45
};
//byte[] key=new BASE64Decoder().decodeBuffer("F85BF180C1FE08347FA4D676735E621CA2387FD60E526745");
//byte[] key=Base64.decode("F85BF180C1FE08347FA4D676735E621CA2387FD60E526745");
byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8};
byte[] kiv1 = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
byte[] data="xuemaiwangleyixue".getBytes("UTF-8");
System.out.println("ECB加密解密");
byte[] str3 = des3EncodeECB(key,data );
byte[] str4 = ees3DecodeECB(key, str3);
System.out.println(new BASE64Encoder().encode(str3));
System.out.println(new String(str4, "UTF-8"));
System.out.println();
System.out.println("CBC加密解密");
byte[] str5 = des3.des3EncodeCBC(data);
String strb=new BASE64Encoder().encode(des3.des3EncodeCBC(data));
System.out.println("new BASE64Encoder().encode:"+strb);
String stru = java.net.URLEncoder.encode(strb, "UTF-8");
System.out.println("java.net.URLEncoder.encode:"+stru);
String stru1 = java.net.URLDecoder.decode(stru,"UTF-8");
System.out.println("解码url:"+stru1);
BASE64Decoder dc = new BASE64Decoder();
ByteArrayInputStream bis = new ByteArrayInputStream(stru1.getBytes());
System.out.println("解码Base:"+dc.decodeBuffer(stru1));
byte[] str6 = des3.des3DecodeCBC(dc.decodeBuffer(java.net.URLDecoder.decode(stru,"UTF-8")));
System.out.println(new String(str6,"UTF-8"));
String sp = "aaa$bbbb$cccc$dddd";
String[] a = sp.split("\\$");
System.out.println(a[0]);
// System.out.println(new BASE64Encoder().encode(str5));
//
// byte[] str6 = des3.des3DecodeCBC(java.net.URLDecoder.decode(dc.decodeBuffer(str).toString(), "UTF-8").getBytes());
// str = java.net.URLEncoder.encode(new BASE64Encoder().encode(str6),"UTF-8");
// System.out.println("---"+str);
//
//
//
//
// byte[] str7 = des3.des3EncodeCBC(data);
// byte[] str8 = des3.des3DecodeCBC(str5);
// //System.out.println(new String(Base64.encode(str5, Base64.DEFAULT),"UTF-8"));
// System.out.println(new String(str6, "UTF-8"));
// System.out.println();
// System.out.println(new BASE64Encoder().encode(str7));
// System.out.println(new String(str8, "UTF-8"));
// System.out.println(new BASE64Encoder().encode(str5).equals("6wS+lkzCcLkPNPEc+fXu3Q=="));
// System.out.println(java.net.URLEncoder.encode("6wS+lkzCcLlMIWbSXi+JN6vrXz3f0c6E", "UTF-8"));
}
/**
* ECB加密,不要IV
* @param key 密钥
* @param data 明文
* @return Base64编码的密文
* @throws Exception
*/
public static byte[] des3EncodeECB(byte[] key, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("DESede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* ECB解密,不要IV
* @param key 密钥
* @param data Base64编码的密文
* @return 明文
* @throws Exception
*/
public static byte[] ees3DecodeECB(byte[] key, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("DESede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* CBC加密
* @param key 密钥
* @param keyiv IV
* @param data 明文
* @return Base64编码的密文
* @throws Exception
*/
public byte[] des3EncodeCBC(byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("DESede" + "/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(keyiv);
cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* CBC解密
* @param key 密钥
* @param keyiv IV
* @param data Base64编码的密文
* @return 明文
* @throws Exception
*/
public byte[] des3DecodeCBC(byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("DESede" + "/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(keyiv);
cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
}