欢迎来到天天文库
浏览记录
ID:41123739
大小:36.00 KB
页数:5页
时间:2019-08-17
《java DES加密解密源码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、packagecom.lt.util;importjava.security.Key;importjava.security.Security;importjavax.crypto.Cipher;publicclassDesUtils{/**字符串默认键值*/privatestaticStringstrDefaultKey="national";/**加密工具*/privateCipherencryptCipher=null;/**解密工具*/privateCipherdecryptCipher=null;/***将byte数组转换为表示16进制值的字符串,如:byte[]{8,18}
2、转换为:0813,和publicstaticbyte[]*hexStr2ByteArr(StringstrIn)互为可逆的转换过程**@paramarrB*需要转换的byte数组*@return转换后的字符串*@throwsException*本方法不处理任何异常,所有异常全部抛出*/publicstaticStringbyteArr2HexStr(byte[]arrB)throwsException{intiLen=arrB.length;//每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍StringBuffersb=newStringBuffer(iLen*2);
3、for(inti=0;i4、@return转换后的byte数组*@throwsException*本方法不处理任何异常,所有异常全部抛出*@authorLiGuoQing*/publicstaticbyte[]hexStr2ByteArr(StringstrIn)throwsException{byte[]arrB=strIn.getBytes();intiLen=arrB.length;//两个字符表示一个字节,所以字节数组长度是字符串长度除以2byte[]arrOut=newbyte[iLen/2];for(inti=0;i5、;i=i+2){StringstrTmp=newString(arrB,i,2);arrOut[i/2]=(byte)Integer.parseInt(strTmp,16);}returnarrOut;}/***默认构造方法,使用默认密钥**@throwsException*/publicDesUtils()throwsException{this(strDefaultKey);}/***指定密钥构造方法**@paramstrKey*指定的密钥*@throwsException*/publicDesUtils(StringstrKey)throwsException{Security.6、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,key);}/***加密字节数组**@paramarrB*需加密的字节数组*@return加密后的字节7、数组*@throwsException*/publicbyte[]encrypt(byte[]arrB)throwsException{returnencryptCipher.doFinal(arrB);}/***加密字符串**@paramstrIn*需加密的字符串*@return加密后的字符串*@throwsException*/publicStringencrypt(StringstrIn)throwsException{returnbyt
4、@return转换后的byte数组*@throwsException*本方法不处理任何异常,所有异常全部抛出*@authorLiGuoQing*/publicstaticbyte[]hexStr2ByteArr(StringstrIn)throwsException{byte[]arrB=strIn.getBytes();intiLen=arrB.length;//两个字符表示一个字节,所以字节数组长度是字符串长度除以2byte[]arrOut=newbyte[iLen/2];for(inti=0;i5、;i=i+2){StringstrTmp=newString(arrB,i,2);arrOut[i/2]=(byte)Integer.parseInt(strTmp,16);}returnarrOut;}/***默认构造方法,使用默认密钥**@throwsException*/publicDesUtils()throwsException{this(strDefaultKey);}/***指定密钥构造方法**@paramstrKey*指定的密钥*@throwsException*/publicDesUtils(StringstrKey)throwsException{Security.6、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,key);}/***加密字节数组**@paramarrB*需加密的字节数组*@return加密后的字节7、数组*@throwsException*/publicbyte[]encrypt(byte[]arrB)throwsException{returnencryptCipher.doFinal(arrB);}/***加密字符串**@paramstrIn*需加密的字符串*@return加密后的字符串*@throwsException*/publicStringencrypt(StringstrIn)throwsException{returnbyt
5、;i=i+2){StringstrTmp=newString(arrB,i,2);arrOut[i/2]=(byte)Integer.parseInt(strTmp,16);}returnarrOut;}/***默认构造方法,使用默认密钥**@throwsException*/publicDesUtils()throwsException{this(strDefaultKey);}/***指定密钥构造方法**@paramstrKey*指定的密钥*@throwsException*/publicDesUtils(StringstrKey)throwsException{Security.
6、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,key);}/***加密字节数组**@paramarrB*需加密的字节数组*@return加密后的字节
7、数组*@throwsException*/publicbyte[]encrypt(byte[]arrB)throwsException{returnencryptCipher.doFinal(arrB);}/***加密字符串**@paramstrIn*需加密的字符串*@return加密后的字符串*@throwsException*/publicStringencrypt(StringstrIn)throwsException{returnbyt
此文档下载收益归作者所有