java处理加密文件---RSA加密和解密

openkk 12年前

 public class RSA {   public static void main(String []args)throws Exception      {    String ptext = "he";    //获取公钥生成参数    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");    kpg.initialize(1024);    KeyPair keyPair = kpg.genKeyPair();      RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();        System.out.println("公共码"+publicKey.getModulus().toString());        byte[] ptexts = ptext.getBytes("UTF8");    BigInteger m = new BigInteger(ptexts);    BigInteger c = m.modPow(publicKey.getPublicExponent(), publicKey.getModulus());    System.out.println("密文"+c);        //System.out.println("d:"+privateKey.getPrivateExponent());        System.out.println("私密码"+privateKey.getModulus());        BigInteger jm=c.modPow(privateKey.getPrivateExponent(), privateKey.getModulus());    System.out.println("解码数据"+jm);        byte[] mt =jm.toByteArray();        System.out.print("解码结果");    for(int i=0;i<mt.length;i++){     System.out.print((char)mt[i]);    }          }  }