欢迎来到天天文库
浏览记录
ID:39132112
大小:43.00 KB
页数:5页
时间:2019-06-25
《java写的des加密算法》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、importjava.security.Key;importjava.security.Security;importjava.util.Scanner;importjavax.crypto.Cipher;publicclassDes{/**字符串默认键值*/privatestaticStringstrDefaultKey="national";/**加密工具*/privateCipherencryptCipher=null;/**解密工具*/privateCipherdecryptCipher=null;/***将byte数组转换为表示16进制值的字符串,如:byte
2、[]{8,18}转换为:0813,和publicstaticbyte[]*hexStr2ByteArr(StringstrIn)互为可逆的转换过程**@paramarrB*需要转换的byte数组*@return转换后的字符串*@throwsException*本方法不处理任何异常,所有异常全部抛出*/publicstaticStringbyteArr2HexStr(byte[]arrB)throwsException{intiLen=arrB.length;//每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍StringBuffersb=newStrin
3、gBuffer(iLen*2);for(inti=0;i4、程**@paramstrIn*需要转换的字符串*@return转换后的byte数组*@throwsException*本方法不处理任何异常,所有异常全部抛出*@authorLiGuoQing*/publicstaticbyte[]hexStr2ByteArr(StringstrIn)throwsException{byte[]arrB=strIn.getBytes();intiLen=arrB.length;//两个字符表示一个字节,所以字节数组长度是字符串长度除以2byte[]arrOut=n5、ewbyte[iLen/2];for(inti=0;i6、s(StringstrKey)throwsException{//Security.addProvider(newcom.sun.crypto.provider.SunJCE());Keykey=getKey(strKey.getBytes());encryptCipher=Cipher.getInstance("DES");encryptCipher.init(Cipher.ENCRYPT_MODE,key);decryptCipher=Cipher.getInstance("DES");decryptCipher.init(Cipher.DECRYPT_MODE,k7、ey);}/***加密字节数组**@paramarrB*需加密的字节数组*@return加密后的字节数组*@throwsException*/publicbyte[]encrypt(byte[]arrB)throwsException{returnencryptCipher.doFinal(arrB);}/***加密字符串**@paramstrIn*需加密的字符串*@return加密后的字符串*@throwsException*/publicStringencrypt(StringstrIn)throwsException{returnb
4、程**@paramstrIn*需要转换的字符串*@return转换后的byte数组*@throwsException*本方法不处理任何异常,所有异常全部抛出*@authorLiGuoQing*/publicstaticbyte[]hexStr2ByteArr(StringstrIn)throwsException{byte[]arrB=strIn.getBytes();intiLen=arrB.length;//两个字符表示一个字节,所以字节数组长度是字符串长度除以2byte[]arrOut=n
5、ewbyte[iLen/2];for(inti=0;i6、s(StringstrKey)throwsException{//Security.addProvider(newcom.sun.crypto.provider.SunJCE());Keykey=getKey(strKey.getBytes());encryptCipher=Cipher.getInstance("DES");encryptCipher.init(Cipher.ENCRYPT_MODE,key);decryptCipher=Cipher.getInstance("DES");decryptCipher.init(Cipher.DECRYPT_MODE,k7、ey);}/***加密字节数组**@paramarrB*需加密的字节数组*@return加密后的字节数组*@throwsException*/publicbyte[]encrypt(byte[]arrB)throwsException{returnencryptCipher.doFinal(arrB);}/***加密字符串**@paramstrIn*需加密的字符串*@return加密后的字符串*@throwsException*/publicStringencrypt(StringstrIn)throwsException{returnb
6、s(StringstrKey)throwsException{//Security.addProvider(newcom.sun.crypto.provider.SunJCE());Keykey=getKey(strKey.getBytes());encryptCipher=Cipher.getInstance("DES");encryptCipher.init(Cipher.ENCRYPT_MODE,key);decryptCipher=Cipher.getInstance("DES");decryptCipher.init(Cipher.DECRYPT_MODE,k
7、ey);}/***加密字节数组**@paramarrB*需加密的字节数组*@return加密后的字节数组*@throwsException*/publicbyte[]encrypt(byte[]arrB)throwsException{returnencryptCipher.doFinal(arrB);}/***加密字符串**@paramstrIn*需加密的字符串*@return加密后的字符串*@throwsException*/publicStringencrypt(StringstrIn)throwsException{returnb
此文档下载收益归作者所有