1、由服务商生成通讯双方需要使用的公钥、密钥(通过一个给定的字符串),其中ZHCW0123456789为通过给定的标识,生成公钥文件
PUK.TXT、密钥文件PRK.TXT。
String[] a = {"ZHCW0123456789"};
grsak(a);
/**
*
* @param args 唯一公钥密钥的生成标识 String[]
*/
private static void grsak(String[] args) {
if (args.length > 3) {
usage();
return;
}
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
RSA_INSTANCE_NAME, new BouncyCastleProvider());
keyPairGenerator.initialize(RSA_KEY_SIZE, new SecureRandom());
KeyPair keyPair = keyPairGenerator.genKeyPair();
PublicKey puk = keyPair.getPublic();
PrivateKey prk = keyPair.getPrivate();
//16key
String pkString = encode(puk.getEncoded());
String skString = encode(prk.getEncoded());
String prkFile = DEFAULT_PRK_FILE_NAME;
String pukFile = DEFAULT_PUK_FILE_NAME;
if (args.length == 2) {
prkFile = args[1];
} else if (args.length == 3) {
prkFile = args[1];
pukFile = args[2];
}
writeBytesToFile(prkFile, skString.getBytes());
writeBytesToFile(pukFile, pkString.getBytes());
System.out
.println("Success to generate RSA key, the keys file are: ");
if (args.length == 1) {
System.out.println(" PRK.txt is the private key file");
System.out.println(" PUK.txt is the public key file ");
} else if (args.length == 2) {
System.out
.println(" " + args[1] + " is the private key file ");
System.out.println(" PUK.txt is the public key file");
} else {
System.out
.println(" " + args[1] + " is the private key file ");
System.out.println(" " + args[2] + " is the public key file");
}
System.out.println();
System.out.println("-- all keys are encoded by BASE64_ENCODER --");
} catch (Exception e) {
throw new java.lang.RuntimeException(e);
}
}
2、服务商私钥加密操作--demo
//定义要加密的测试串
String encrypt="dfasdfasdfasdfasd2222222444433";
//后台输出测试串
System.out.println("服务商私钥加密前的encrypt="+encrypt);
// 定义字符串数组ske_jm包括私钥文件、测试串
String[] ske_jm = {"","/jsp/PRK.txt",encrypt};
//根据私钥加密测试串encrypt,返回一个字符数组ske
byte[] ske=c.ske_new(ske_jm);
//后台出输出ske
System.out.println("服务商私钥加密后的ske="+ske);
3、客户商公钥解密操作--demo
//定义字符串数组ske_xm包括公钥文件、测试串加密过的字节数组对象ske
String[] pkd_xm = {"","/jsp/PUK.txt",""};
String pkd=c.pkd_new(pkd_xm,ske);
//客户端输出解密后的字符串
System.out.println("客户商公钥解密后的pkd="+pkd);
4、客户商公钥加密操作--demo
//定义要加密的测试串
String encrypt="dfsadfasdf222222222";
//后台输出测试串encrypt
System.out.println("客户商公钥加密前的encrypt="+encrypt);
// 定义字符串数组pke_jm包括私钥文件、测试串
String[] pke_jm = {"","PUK.txt",encrypt};
//根据公钥及测试串数据pke_jm,返回一个字符数组pke
byte[] pke=c.pke_new(pke_jm);
System.out.println("客户商公钥加密后的pke="+pke);
5、服务商私钥解密操作--demo
//定义私钥数据
String[] skd_xm = {"","PRK.txt",""};
//对客户商加密后的串进行解密
String skd=c.skd_new(skd_xm,pke);
//后台输出解密后字符串
System.out.println("服务商私钥解密后的skd="+skd);
以下是服务商、客户商加密和解密需要的方法:
1、服务商私钥加密
public static byte[] ske_new(String[] args) {
if (args.length < 3) {
usage();
return null;
}
String outFile = "jiami.txt" + SKE_SUFFIX;
if (args.length == 4) {
outFile = args[3];
}
String prkString = new String(readFileAsBytes(args[1]));
//byte[] keyBytes = readFileAsBytes(args[2]);
byte[] keyBytes =args[2].getBytes();
byte[] encryptedBytes = encryptedByPrivateKey(keyBytes, prkString);
//writeBytesToFile(outFile, encryptedBytes);
// System.out.println("jami.txt"
// + " has been encrypted by private key, the out file is: "
// + outFile);
return encryptedBytes;
}
2、客户商公钥解密
public static String pkd_new(String[] args,byte[] ske) {
if (args.length < 2) {
usage();
return null;
}
String outFile = "jiami.txt.ske" + PKD_SUFFIX;
if (args.length == 4) {
outFile = args[3];
}
String pukString = new String(readFileAsBytes(args[1]));
//byte[] keyByte2 = readFileAsBytes("jiami.txt.ske");
// System.out.println("ske_file="+new String(keyByte2));
//System.out.println("ske_string="+ske);
byte[] keyBytes =ske;
byte[] encryptedBytes = decryptedByPublicKey(keyBytes, pukString);
//writeBytesToFile(outFile, encryptedBytes);
// System.out.println(args[1]
// + " has been decrypted by public key, the out file is: "
// + outFile);
return new String(encryptedBytes);
}
3、客户商公钥加密
private static byte[] pke_new(String[] args) {
// System.out.println(args[2]);
if (args.length < 3) {
usage();
return null;
}
String outFile = args[2] + PKE_SUFFIX;
if (args.length == 4) {
outFile = args[3];
}
String pukString = new String(readFileAsBytes(args[1]));
//byte[] keyBytes = readFileAsBytes(args[2]);
byte[] keyBytes = args[2].getBytes();
byte[] encryptedBytes = encryptedByPublicKey(keyBytes, pukString);
//writeBytesToFile(outFile, encryptedBytes);
// System.out.println(args[2]
// + " has been encrypted by public key, the out file is: "
// + outFile);
return encryptedBytes;
}
4、服务商私钥解密
private static String skd_new(String[] args,byte[] pke) {
if (args.length < 3) {
usage();
return null;
}
String outFile = args[2] + SKD_SUFFIX;
if (args.length == 4) {
outFile = args[3];
}
String prkString = new String(readFileAsBytes(args[1]));
//byte[] keyBytes = readFileAsBytes(args[2]);
byte[] keyBytes =pke;
byte[] encryptedBytes = decryptedByPrivateKey(keyBytes, prkString);
// writeBytesToFile(outFile, encryptedBytes);
// System.out.println(args[1]
// + " has been decrypted by private key, the out file is: "
// + outFile);
return new String(encryptedBytes);
}