package com.yihu.wlyy.util;

import java.util.regex.Pattern;

public class RSATester {

	static {
//		try {
//			Map<String, Object> keyMap = RSAUtils.genKeyPair();
//			String publicKey = RSAUtils.getPublicKey(keyMap);
//			String privateKey = RSAUtils.getPrivateKey(keyMap);
//			System.err.println("公钥: \n\r" + publicKey);
//			System.err.println("私钥： \n\r" + privateKey);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	public static void main(String[] args) throws Exception {
//		RSAUtils.genKeyPair();

//		 String str = "0022f7cce565dacbc0c3ce7d97bbf055ea860b9fdbb317ed78f50d561fd7a4bc8be2f10a91174c663129fab72795fdcb2e32d04da409e781a5b49ac9defad8f41f1358f60d8659fbbecd487bc6b4";
//		 byte[] decodedData = RSAUtils.decryptByPrivateKey(RSAUtils.ASCII_To_BCD(str.getBytes(), str.getBytes().length), RSAUtils.privateKey);
//		 String target = new String(decodedData);
//		 System.out.println("解密后文字: \r\n" + target);
		
//		System.out.println(RSAUtils.encryptString("abcd"));
//		String str = "4b3b3ab5f4f4a2beffc65b0dda50e4a285500c5707c37dbf297e887f70cbe0a85698a9b939c4114da4e7830bea7bc91e11c69b4548adfec82403718c4432665ea6450bf6f6327d359cd733934b9a41e66459fe8f91f426568642241378cd969eaa01c16624c88199bb60f9097cbc6a3434919b45aa7630988ccbfa498ced43a7";
//		System.out.println(RSAUtils.getInstance().decryptStringByJs(str));

		// test();
		// testSign();
		
//		RSA rsa = new RSA();
//		System.out.println("加密后：\n"+rsa.encrypt("abcd"));
	}
	
	public static byte[] hexStringToByteArray(String hex)   
    {  
        Pattern replace = Pattern.compile("^0x");  
        String s = replace.matcher(hex).replaceAll("");  

        byte[] b = new byte[s.length() / 2];  
        for (int i = 0; i < b.length; i++){  
          int index = i * 2;  
          int v = Integer.parseInt(s.substring(index, index + 2), 16);  
          b[i] = (byte)v;  
        }  
        return b;  
    }  

//	static void test() throws Exception {
//		long begin = System.currentTimeMillis();
//		String source = "86951502213838920160412145611";
//		System.out.println("\r加密前文字：\r\n" + source);
//		byte[] data = source.getBytes();
//		byte[] encodedData = RSAUtils.encryptByPublicKey(data, RSAUtils.publicKey);
//		String bcdstr = RSAUtils.bcd2Str(encodedData);
//		System.out.println("加密后文字：\r\n" + bcdstr);
//		byte[] decodedData = RSAUtils.decryptByPrivateKey(RSAUtils.ASCII_To_BCD(bcdstr.getBytes(), bcdstr.getBytes().length), RSAUtils.privateKey);
//		String target = new String(decodedData);
//		System.out.println("解密后文字: \r\n" + target);
//		System.out.println(System.currentTimeMillis() - begin);
//	}
//
//	static void testSign() throws Exception {
//		System.err.println("私钥加密——公钥解密");
//		String source = "this is imei and token";
//		System.out.println("原文字：\r\n" + source);
//		byte[] data = source.getBytes();
//		byte[] encodedData = RSAUtils.encryptByPrivateKey(data, RSAUtils.privateKey);
//		System.out.println("加密后：\r\n" + new String(encodedData));
//		byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, RSAUtils.publicKey);
//		String target = new String(decodedData);
//		System.out.println("解密后: \r\n" + target);
//		System.err.println("私钥签名——公钥验证签名");
//		String sign = RSAUtils.sign(encodedData, RSAUtils.privateKey);
//		System.err.println("签名:\r" + sign);
//		boolean status = RSAUtils.verify(encodedData, RSAUtils.publicKey, sign);
//		System.err.println("验证结果:\r" + status);
//	}

}