2DES对称密码算法验证

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

2.DES对称密码算法验证packageDES;importjava.security.InvalidKeyException;importjava.security.Key;importjava.security.NoSuchAlgorithmException;importjava.security.SecureRandom;importjava.security.spec.InvalidKeySpecException;importjavax.crypto.Cipher;importjavax.crypto.SecretKey;importjavax.crypto.SecretKeyFactory;importjavax.crypto.spec.DESKeySpec;publicclassDESConfirm{publicstaticbyte[]encrypt(byte[]datasource,Stringpassword){try{SecureRandomrandom=newSecureRandom();DESKeySpecdesKey=newDESKeySpec(password.getBytes());SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance(DES);SecretKeysecurekey=keyFactory.generateSecret(desKey);Ciphercipher=Cipher.getInstance(DES);cipher.init(Cipher.ENCRYPT_MODE,securekey,random);returncipher.doFinal(datasource);}catch(Throwablee){e.printStackTrace();}returnnull;}publicstaticbyte[]decrypt(byte[]src,Stringpassword)throwsException{SecureRandomrandom=newSecureRandom();DESKeySpecdesKey=newDESKeySpec(password.getBytes());SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance(DES);SecretKeysecurekey=keyFactory.generateSecret(desKey);Ciphercipher=Cipher.getInstance(DES);cipher.init(Cipher.DECRYPT_MODE,securekey,random);returncipher.doFinal(src);}}Main.javapackageDES;importsun.security.krb5.internal.crypto.Des;importjavax.crypto.spec.DESKeySpec;importjava.io.IOException;importjava.util.Vector;publicclassmain{publicstaticvoidmain(String[]args){IOio=newIO();io.GetPlainText(C:\\Users\\Administrator\\workspace\\Cryptology\\src\\DES\\PlainText.text);Stringstr=io.getPlainText();DESdes=newDES();//密码,长度要是8的倍数//Stringpassword=9588028820109132570743325311898426347857298773549468758875018579537757772163084478873699447306034466200616411960574122434059469100235892702736860872901247123456;Stringpassword=computer;byte[]result=DES.encrypt(str.getBytes(),password);Stringencryresult=newString(result);System.out.print(加密后:);System.out.println(encryresult);io.WriteCipher(C:\\Users\\Administrator\\workspace\\Cryptology\\src\\DES\\Cipher.text,encryresult);//直接将如上内容解密try{byte[]decryResult=DES.decrypt(result,password);System.out.println(解密后:+newString(decryResult));}catch(Exceptione1){e1.printStackTrace();}}}IO.javapackageDES;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;publicclassIO{privateStringPlainText;publicvoidGetPlainText(Stringpath){//String[]out;Filefile=newFile(path);BufferedReaderreader=null;try{reader=newBufferedReader(newFileReader(file));StringtempString=null;intline=1;while((tempString=reader.readLine())!=null){PlainText=tempString;line++;}reader.close();}catch(IOExceptione){e.printStackTrace();}finally{if(reader!=null){try{reader.close();}catch(IOExceptione1){}}}}publicStringgetPlainText(){returnPlainText;}publicvoidWriteCipher(Stringpath,StringCipher){Filefile=newFile(path);FileWriterfw=null;BufferedWriterwriter=null;try{fw=newFileWriter(file);writer=newBufferedWriter(fw);writer.write(Cipher);writer.newLine();//换行writer.flush();}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{try{writer.close();fw.close();}catch(IOExceptione){e.printStackTrace();}}}}运行结果:

1 / 2
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功