package test.com;
import java.nio.ByteOrder;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;
import test.util.BytesToHex;
import test.util.DESUtil;
import test.util.DHUtil;
import test.util.RSAUtil;
import test.util.TripleDESUtil;
public class Test {
public static final String DATA = "Test";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// Test DH
byte[] publicKey1; // 甲方公钥
byte[] privateKey1; // 甲方私钥
byte[] secretKey1; // 甲方本地密钥
byte[] publicKey2; // 乙方公钥
byte[] privateKey2; // 乙方私钥
byte[] secretKey2; // 乙方本地密钥
// 初始化密钥并生成甲方密钥对
Map<String,Object> keymap1 = DHUtil.initKey();
publicKey1 = DHUtil.getPublicKey(keymap1);
privateKey1 = DHUtil.getPrivateKey(keymap1);
// 乙方根据甲方公钥产生乙方密钥对
Map<String,Object> keymap2 =DHUtil.initKey(publicKey1);
publicKey2 = DHUtil.getPublicKey(keymap2);
privateKey2 = DHUtil.getPrivateKey(keymap2);
// 对于甲方,根据其私钥与对方发来的公钥,生成本地密钥
secretKey1 = DHUtil.getSecretKey(publicKey2, privateKey1);
System.out.println("甲方本地密钥:"+BytesToHex.fromBytesToHex(secretKey1));
secretKey2 = DHUtil.getSecretKey(publicKey1, privateKey2);
System.out.println("乙方本地密钥:"+BytesToHex.fromBytesToHex(secretKey2));
/* Test RSA */
Map<String, Object> keyMap = RSAUtil.initKey();
RSAPublicKey rsaPublicKey = RSAUtil.getPublicKey(keyMap);
RSAPrivateKey rsaPrivateKey = RSAUtil.getPrivateKey(keyMap);
System.out.println("RSA PublicKey : " + rsaPublicKey);
System.out.println("RSA PrivateKey : " + rsaPrivateKey);
byte[] rsaResult = RSAUtil.encrypt(DATA.getBytes(), rsaPublicKey);
System.out.println(DATA + ">>>RSA 加密>>>" + BytesToHex.fromBytesToHex(rsaResult));
byte[] plainResult = RSAUtil.decrypt(rsaResult, rsaPrivateKey);
System.out.println(DATA + ">>>RSA 解密>>>" + new String(plainResult));
/* byte[] desKey = DESUtil.initKey();
System.out.println("DES key data "+desKey);
byte[] encrype = DESUtil.encrype(DATA.getBytes(), desKey);
System.out.println("DES 加密 "+encrype);
byte[] decrype = DESUtil.decrype(encrype, desKey);
System.out.println("DES 解密 "+new String(decrype));
byte[] tripleKey = TripleDESUtil.initKey();
System.out.println("3DES key data "+tripleKey);
byte[] encrype3 = TripleDESUtil.encrype(DATA.getBytes(), tripleKey);
System.out.println("3DES 加密 "+encrype3);
byte[] decrype3 = TripleDESUtil.decrype(encrype3, tripleKey);
System.out.println("3DES 解密 "+new String(decrype3));*/
}
}