package com.rsa.zpl;
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.util.Date;
/**
* 生成公钥和私钥
* @author zpl
*
*/
public class GenKeys {
public static void main(String[] args) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom secureRandom = new SecureRandom(new Date().toString().getBytes());
keyPairGenerator.initialize(1024, secureRandom);
KeyPair keyPair = keyPairGenerator.genKeyPair();
String publicKeyFilename = "D:/publicKeyFile";
byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
FileOutputStream fos = new FileOutputStream(publicKeyFilename);
fos.write(publicKeyBytes);
fos.close();
String privateKeyFilename = "D:/privateKeyFile";
byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
fos = new FileOutputStream(privateKeyFilename);
fos.write(privateKeyBytes);
fos.close();
}
}
package com.rsa.zpl;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
/**
* 获取私钥
* @author zpl
*
*/
public class PrivateKeyReader {
public static PrivateKey get(String filename)throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int)f.length()];
dis.readFully(keyBytes);
dis.close();
PKCS8EncodedKeySpec spec =new PKCS8EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(spec);
}
public static void main(String[] args) throws Exception, InvalidKeySpecException, IOException {
System.out.println(PrivateKeyReader.get("d:/privateKeyFile"));
}
}
package com.rsa.zpl;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.security.KeyFactory;
/**
* 获取私钥
* @author zpl
*
*/
public class PublicKeyReader {
public static PublicKey get(String filename) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int)f.length()];
dis.readFully(keyBytes);
dis.close();
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}
public static void main(String[] args) {
try {
System.out.println(get("D:\\publicKeyFile"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.rsa.zpl;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
/**
* 公钥加密,私钥解密
* @author zpl
*
*/
public class TestEncryptAndDecrypt {
public static void main(String[] args) throws Exception {
//测试公钥加密,私钥解密
String input = "thisIsMyPassword$7788";
RSAPublicKey pubKey = (RSAPublicKey) PublicKeyReader.get("d:/publicKeyFile");
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherText = cipher.doFinal(input.getBytes());
//加密后的东西
// System.out.println("cipher: " + new String(cipherText));
Cipher cipher1 = Cipher.getInstance("RSA");
RSAPrivateKey privKey = (RSAPrivateKey) PrivateKeyReader.get("d:/privateKeyFile");
//开始解密
cipher1.init(Cipher.DECRYPT_MODE, privKey);
byte[] plainText = cipher1.doFinal(cipherText);
System.out.println("plain : " + new String(plainText));
}
}
package com.rsa.zpl;
import java.io.InputStream;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.io.IOUtils;
/**
* 公钥解密,私钥加密
* @author zpl
*
*/
public class TestEncryptAndDecrypt {
public static void main(String[] args) throws Exception {
//加密部分*****************************************************8
String input = "thisIsMyPassword$zpl";
//RSAPublicKey pubKey = (RSAPublicKey) PublicKeyReader.get("d:/publicKeyFile");// 这是公钥加密的
RSAPrivateKey privKey = (RSAPrivateKey) PrivateKeyReader.get("D:/privateKeyFile");// 这是私钥加密的
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privKey);
byte[] cipherText = cipher.doFinal(input.getBytes());
//加密后的东西
System.out.println("******************以上是加密部分cipher: " + new String(cipherText));
// 解密部分 *****************************************************
Cipher cipher1 = Cipher.getInstance("RSA");
RSAPublicKey pubKey = (RSAPublicKey)get(TestEncryptAndDecrypt.class.getResourceAsStream("publicKeyFile"));// 公钥解密
// RSAPublicKey pubKey = (RSAPublicKey) PublicKeyReader.get("d:/publicKeyFile");// 公钥解密
// RSAPrivateKey privKey = (RSAPrivateKey) PrivateKeyReader.get("d:/privateKeyFile");//私钥解密用这个
//开始解密
cipher1.init(Cipher.DECRYPT_MODE, pubKey);
byte[] plainText = cipher1.doFinal(cipherText);
System.out.println("plain : " + new String(plainText));
}
public static PublicKey get(InputStream inputStream) throws Exception {
byte[] keyBytes = IOUtils.toByteArray(inputStream);
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(spec);
}
}
cipher: 硤購镊爮x迨茦A吾 C ?承 蒹?{{"C闥牖S卭学???滏?配鶫?b 風
rNT AZ? 埖\探7oQ|X瓌9螿0 暗沌3达鲓8鼢?邆?G繬 ?貶
??
plain : thisIsMyPassword$zpl
生成这样,说明是正确的
转载于:https://my.oschina.net/zplswf/blog/187521