欢迎来到天天文库
浏览记录
ID:50826801
大小:39.45 KB
页数:2页
时间:2020-03-15
《java加密算法(参考网络).doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、packagecom.ex8;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.ObjectOutputStream;importjava.security.Key;importjava.security.KeyFactory;importjava.secu
2、rity.KeyPair;importjava.security.KeyPairGenerator;importjava.security.NoSuchAlgorithmException;importjava.security.PublicKey;importjava.security.spec.InvalidKeySpecException;importjava.security.spec.X509EncodedKeySpec;importjavax.crypto.KeyGenerator;publicclas
3、sKeyGeneratorEx{publicstaticvoidmain(String[]args)throwsNoSuchAlgorithmException,IOException,InvalidKeySpecException{KeyGeneratorkg=KeyGenerator.getInstance("DESede");kg.init(168);Keykey=kg.generateKey();byte[]bs=key.getEncoded();System.out.println("得到单钥加密密钥:"
4、+newString(bs));System.out.println("-----------------------------------------");KeyPairGeneratorkpg=KeyPairGenerator.getInstance("RSA");kpg.initialize(1024);KeyPairkp=kpg.generateKeyPair();bs=kp.getPublic().getEncoded();System.out.println("产生双玥的Public密钥对的公钥:"+
5、newString(bs));System.out.println("------------------------------------------");bs=kp.getPrivate().getEncoded();System.out.println("产生双玥的Private密钥对的私钥:"+newString(bs));System.out.println("-----------------------------------------");Stringpath="E:"+File.separat
6、or+"forrest"+File.separator+"java"+File.separator+"key"+File.separator+"publicKey.dat";Filef=newFile(path);FileOutputStreamfout=newFileOutputStream(f);fout.write(kp.getPublic().getEncoded());fout.flush();fout.close();System.out.println("PublicKeysavedabsolutep
7、ath:"+f.getAbsolutePath());System.out.println("------------------------------------------");path="E:"+File.separator+"forrest"+File.separator+"java"+File.separator+"key"+File.separator+"privateKey.dat";ObjectOutputStreamout=newObjectOutputStream(newFileOutputS
8、tream(newFile(path)));out.writeObject(kp.getPrivate());out.flush();out.close();System.out.println("PrivateKeysaved");System.out.println("------------------------------------------"
此文档下载收益归作者所有