java字符串加密解密
try {
String test='123456789@fdj.com ';
encryptiondecryptiondes=newencryptiondecryption (' tour HB ' ); //自定义键
System.out.println ('加密前的字符:“test”
System.out.println ('加密字符: ' des.encrypt () test );
System.out.println (解密的字符:“des.decrypt(des.encrypt ) test )”
System.out.println (
des.decrypt (Fe 14 c7c 41 f7d2a 94 b 07d 7928 b 21346 E7 );
}catch(exceptione ) {
e .打印任务跟踪(;
}
以下是加密解密类的源代码
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
publicclassencryptiondecryption
privatestaticstringstrdefaultkey=' tour HB ';
/**加密工具*
privatecipherencryptcipher=null;
/**解密工具*
privatecipherdecryptcipher=null;
//*
将byte数组转换为表示十六进制值的字符串。 例如,byte [ ] { 8,18 }将转换为0813和公共静态byte [ ]
*hexstr2byteARR(stringstrin )是相互可逆的变换过程
*
* @param arrB
*需要转换的byte数组
* @return转换后的字符串
* @throws Exception
*
*/
publicstaticstringbytearr2hexstr (byte [ ] arrb ) throws Exception {
int iLen=arrB.length;
//因为各byte可以用2个文字来表现,所以字符串的长度是数组长度的2倍
string buffer sb=new string buffer (ilen *2);
for(intI=0; i iLen; I ) {
int intTmp=arrB[i];
//将负数转换为正数
while(inttmp0) {
intTmp=intTmp 256;
}
小于//0F的数需要在前面补0
if(inttmp16 ) {
sb.append('0);
}
sb.append(integer.tostring ) inttmp,16 );
}
return sb.toString (;
}
//*
*将表示十六进制值的字符串转换为byte数组和publicstaticstringbytearr2hexstr (byte [ ] arrb )
相互可逆的转换过程
*
需要* @param strIn转换的字符串
* @return转换后的byte数组
* @throws Exception
*
*/
公共静态字节[ ] hexstr 2字节阵列(string strin ) throws Exception { ) )。
byte[] arrB=strIn.getBytes (;
int iLen=arrB.length;
//2个字符表示一个字节,因此字节数组的长度等于字符串的长度除以2
byte[] arrOut=new byte[iLen/2];
for(intI=0; i iLen; I=I2({
stringstrtmp=newstring(arrb,I,2 );
arrout[I/2]=(byte ) integer.parseint(strtmp,16 );
}
返回区域输出;
}
//*
*默认生成方法,使用默认密钥
*
* @throws Exception
*/
公共加密描述() throws Exception {
this(strdefaultkey );
}
//*
*指定如何生成密钥
*
* @param strKey
*指定的密钥
* @throws Exception
*/
publicencryptiondecryption (string strkey ) throws Exception {
security.add provider (newcom.sun.crypto.provider.sunjce () );
keykey=Getkey(Strkey.Getbytes ) );
encrypt cipher=cipher.getinstance (' des );
encrypt cipher.init (cipher.encrypt _ mode,key;
ecryptcipher=cipher.getinstance (' des );
ecryptcipher.init (cipher.decrypt _ mode,key );
}
//*
*加密字节数组
*
* @param arrB
*要加密的字节数组
* @return加密的字节数组
* @throws Exception
*/
公共byte [ ] encrypt (byte [ ] arrb ) throws Exception {
returnencryptcipher.do final (arrb;
}
//*
*加密字符串
*
* @param strIn
*要加密的字符串
* @return加密后的字符串
* @throws Exception
*/
公共字符串加密(string strin ) throws Exception {
returnbytearr2hexstr (encrypt (strin.getbytes () );
}
//*
*解密字节数组
*
* @param arrB
*要解密的字节数组
* @return解码后的字节数组
* @throws Exception
*/
公共byte [ ] decrypt (byte [ ] arrb ) throws Exception {
returndecryptcipher.do final (arrb;
}
//*
*解密字符串
*
* @param strIn
*要解密的字符串
* @return解密后的字符串
* @throws Exception
*/
公共字符串描述(string strin ) throws Exception {
try {
返回新字符串(decrypt (hexstr2byte arr ) strin );
}catch(exceptione ) {
返回' ';
}
}
//*
*根据指定字符串生成密钥。 如果密钥所需的字节数组长度小于8位,则稍后添加0,如果长度超过8位,则只取前8位
*
* @param arrBTmp
*构成字符串的字节数组
* @return生成的密钥
* @throws java.lang.Exception
*/
私有密钥获取(byte [ ] arr btmp ) throws Exception {
//创建空的8位字节数组(默认为0 )
byte[] arrB=new byte[8];
//将原始字节数组转换为8位
for(intI=0; i arrBTmp.length i arrB.length; I ) {
arrB[i]=arrBTmp[i];
}
生成//密钥
keykey=new javax.crypto.spec.secret key spec (arrb,' DES ' );
返回密钥;
}
}
java字符串加密解密
资料来源: http://yijianfengvip.blog.163.com/blog/static/1752734320111135340931 /