欢迎来到天天文库
浏览记录
ID:37919546
大小:62.50 KB
页数:5页
时间:2019-06-02
《des密码java版本》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、代码:主函数所在类:packagening.hao;importjava.io.FileNotFoundException;importjava.io.UnsupportedEncodingException;publicclassApplication{publicstaticvoidmain(Stringargs[])throwsUnsupportedEncodingException{Skey_DESk_DES=newSkey_DES();//getkeyk_DES.getKey();SEncen=newSEnc();en.getPlainTex
2、t();en.displayPlaintext();en.encryptionAndStoreToFile();en.displayCrytionText();try{SDecde=newSDec();de.displayPlaintext();}catch(FileNotFoundExceptione){}}}其他类:(1)packagening.hao;importjava.io.*;importjavax.crypto.*;importjavax.crypto.spec.SecretKeySpec;publicclassSDec{FileInpu
3、tStreamf;intnum;intnum1;byte[]cText;FileInputStreamf2;byte[]keykb;byte[]pText;SecretKeySpeck;Ciphercp;ObjectInputStreamoI;SDec()throwsFileNotFoundException{try{f=newFileInputStream("SEnc.dat");//readfromfileSEnc.datnum=f.available();cText=newbyte[num];f.read(cText);//readincText
4、f2=newFileInputStream("key1.dat");oI=newObjectInputStream(f2);//num1=f2.available();//keykb=newbyte[num1];k=(SecretKeySpec)oI.readObject();//强制类型转换//k=newSecretKeySpec(keykb,"DESede");//密钥空间cp=Cipher.getInstance("DESede");cp.init(Cipher.DECRYPT_MODE,k);pText=cp.doFinal(cText);}c
5、atch(Exceptione){}}voiddisplayPlaintext()throwsUnsupportedEncodingException{Stringp=newString(pText);System.out.println("aferencryption:");System.out.println(p);}}(2)packagening.hao;importjava.io.*;importjava.security.Key;importjava.util.Scanner;importjavax.crypto.*;publicclassS
6、Enc{Strings;FileInputStreamf;ObjectInputStreamb;Ciphercp;Keyk;byte[]pText;//storeplainTextbyte[]cText;SEnc(){try{s=newString();f=newFileInputStream("key1.dat");b=newObjectInputStream(f);k=(Key)b.readObject();cp=Cipher.getInstance("DESede");//initializeCipherobjectcp.init(Cipher.
7、ENCRYPT_MODE,k);//setencryptionmode}catch(Exceptione){e.printStackTrace();System.out.println("ERROR");}}voidgetPlainText()throwsUnsupportedEncodingException{System.out.println("EnteraplainText,stopendswith#");Scannersc=newScanner(System.in);while(!s.endsWith("#")){s=sc.next();}p
8、Text=s.getBytes("UTF8");}voiddisplayPlaintext()
此文档下载收益归作者所有