package com.geom.rsa;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import utils.FileUtils;
public class Rsa
{
public static RsaVO createKeyPair()
{
return createKeyPair(512);
}
public static RsaVO createKeyPair( int bit )
{
try
{
KeyPairGenerator key = KeyPairGenerator.getInstance("Rsa");
key.initialize(bit);
KeyPair kp = key.generateKeyPair();
RsaVO vo = new RsaVO();
vo.pubKey = new BASE64Encoder().encode(kp.getPublic().getEncoded());
vo.privKey = new BASE64Encoder().encode(kp.getPrivate().getEncoded());
return vo;
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return null;
}
public static byte[] publicEncrypt(byte[] content, String publicKey) throws Exception
{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, string2PublicKey(publicKey));
byte[] bytes = cipher.doFinal(content);
return bytes;
}
public static byte[] privateDecrypt(byte[] content, String privateKey) throws Exception
{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, string2PrivateKey(privateKey));
byte[] bytes = cipher.doFinal(content);
return bytes;
}
public static byte[] privateEncrypt2(byte[] content, String privateKey) throws Exception
{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, string2PrivateKey(privateKey));
byte[] bytes = cipher.doFinal(content);
return bytes;
}
public static byte[] publicDecrypt2(byte[] content, String publicKey) throws Exception
{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, string2PublicKey(publicKey));
byte[] bytes = cipher.doFinal(content);
return bytes;
}
private static PublicKey string2PublicKey(String pubStr) throws Exception
{
byte[] keyBytes = new BASE64Decoder().decodeBuffer(pubStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
private static PrivateKey string2PrivateKey(String priStr) throws Exception
{
byte[] keyBytes = new BASE64Decoder().decodeBuffer(priStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
public static void main(String[] args) throws Exception
{
RsaVO vo = Rsa.createKeyPair(1024);
System.out.println(vo.pubKey);
System.out.println(vo.privKey);
String path = "D:\\Code\\rsa\\";
FileUtils.writeFile(path + "pub_key", vo.pubKey);
FileUtils.writeFile(path + "priv_key", vo.privKey);
byte[] bytes = Rsa.publicEncrypt("sdfsdfsdf".getBytes(), vo.pubKey);
System.out.println(Rsa.privateDecrypt(bytes, vo.privKey));
}
}
package com.geom.rsa;
public class RsaVO
{
public String pubKey;
public String privKey;
}