Java生成非对称型加密的公钥和私钥
非对称型加密非常适合多个客户端和服务器之间的秘密通讯,客户端使用同一个公钥将明文加密,而这个公钥不能逆向的解密,密文发送到服务器后有服务器 端用私钥解密,这样就做到了明文的加密传送。非对称型加密也有它先天的缺点,加密、解密速度慢制约了它的发挥,如果你有大量的文字需要加密传送,建议你通 过非对称型加密来把对称型‘密钥’分发到客户端,及时更新对称型‘密钥’。
KeyRSA.java
import java.io.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; /** * RSA非对称型加密的公钥和私钥 * @author 五斗米 <如转载请保留作者和出处> * @blog http://blog.csdn.net/mq612 */ public class KeyRSA { private KeyPairGenerator kpg = null; private KeyPair kp = null; private PublicKey public_key = null; private PrivateKey private_key = null; private FileOutputStream public_file_out = null; private ObjectOutputStream public_object_out = null; private FileOutputStream private_file_out = null; private ObjectOutputStream private_object_out = null; /** * 构造函数 * @param in 指定密匙长度(取值范围:512~2048) * @throws NoSuchAlgorithmException 异常 */ public KeyRSA(int in, String address) throws NoSuchAlgorithmException, FileNotFoundException, IOException { kpg = KeyPairGenerator.getInstance("RSA"); //创建‘密匙对’生成器 kpg.initialize(in); //指定密匙长度(取值范围:512~2048) kp = kpg.genKeyPair(); //生成‘密匙对’,其中包含着一个公匙和一个私匙的信息 public_key = kp.getPublic(); //获得公匙 private_key = kp.getPrivate(); //获得私匙 //保存公匙 public_file_out = new FileOutputStream(address + "/public_key.dat"); public_object_out = new ObjectOutputStream(public_file_out); public_object_out.writeObject(public_key); //保存私匙 private_file_out = new FileOutputStream(address + "/private_key.dat"); private_object_out = new ObjectOutputStream(private_file_out); private_object_out.writeObject(private_key); } public static void main(String[] args) { try { new KeyRSA(1024, "c:/key_rsa"); } catch (IOException ex) { } catch (NoSuchAlgorithmException ex) { } } }