package net.shopxx.util.ccb.sample;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
/**
*
* @author suzh
*
* 2006-8-21
*
* 生成密钥的方法
*/
public class GenerateKey
{
public GenerateKey()
{
}
/**
* 生成RSA密钥对
*
* @param pub
* @param pri
*/
public void generRSAKey(String pub, String pri)
{
FileOutputStream output = null;
try
{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
// 密钥的长度
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
PublicKey pbkey = kp.getPublic();
PrivateKey prkey = kp.getPrivate();
// 保存公钥
output = new FileOutputStream(pub);
output.write(ConvertBase64.encodeBase64(pbkey.getEncoded()));
output.close();
// 保存私钥
output = new FileOutputStream(pri);
output.write(ConvertBase64.encodeBase64(prkey.getEncoded()));
} catch (Exception e)
{
e.printStackTrace();
} finally
{
try
{
if (output != null)
{
output.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 生成对称算法的密钥对象
*
* @param filepath
* 文件路径
* @return
*/
public void generSecretKey(String secret)
{
FileOutputStream output = null;
try
{
output = new FileOutputStream(secret);
KeyGenerator kg = KeyGenerator.getInstance("DESede");
// 密钥的长度
kg.init(112);
SecretKey secretKey = kg.generateKey();
output.write(ConvertBase64.encodeBase64(secretKey.getEncoded()));
} catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (output != null)
output.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
*
* @param args
*/
public static void main(String[] args)
{
GenerateKey gener = new GenerateKey();
//非对称密钥对
gener.generRSAKey("D:/test/商户正式RSA/key_RSA_pub.dat","D:/test/商户正式RSA/key_RSA_priv.dat");
FileInputStream input = null;
try {
input = new FileInputStream(new File("D:/test/商户正式RSA/key_RSA_pub.dat"));
byte[] p_data = new byte[input.available()];
input.read(p_data);
input.close();
System.out.println("str:"+new String(p_data));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//对称密钥
gener.generSecretKey("D:/test/商户正式RSA/DES_key.dat");
}
}