首页 > 编程知识 正文

字母加密成数字,32个字符密码例子

时间:2023-05-04 03:31:30 阅读:147581 作者:3573

因为最近在做系统,所以系统必须能够在大屏幕上显示数据页。 此页面中的数据与每个用户相关联。 但是,不能向用户显示URL地址中用户的个人ID。

以前使用BASE64的加密算法加密ID,后来发现密文中出现了%的字符,偶尔会出现无法解密的问题。

但是,大多数情况下是可能的。

一直找不到原因。

之后,经过仔细调试,进入调试模式,发现如果密文中有%这样的符号,大多数情况下都会在从WEB传递到后台的过程中转义。 例如:

//原始密文: d BS9CbMfbE=

//收到的密文: d BS9CbMfbE=

结果,将无法解密。 然后,在网上寻找了这个DES的加密算法,结果很顺利。

转载: http://blog.csdn.net/Lin cyang/article/details/6207902

package com.qx.client.com mon.safe; 输入Java.security.*; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.secret keyfactory; import javax.crypto.spec.deskey spec; /** * DES密码算法*该密码算法是一种对称密码算法*该密码算法在本系统中的应用主要是因为该密码算法加密后*密文为字符串和数字,没有其他字符。

这样的密文可以应用于url地址。 *不用担心通过URL地址传递的数字会被转义。 * @ author administrator * */publicclassrdes {/* *加密算法可通过des、DESede、 可在blowfish.*/privatator中使用/** * DES解密算法* @param data * @param cryptKey密钥为偶数* @ return * @ throws exception */pubube stringcryptkey (throws exception (return new string ) decrypt (hex2byte ) data.getbytes )、cryptKey.getBytes ) ) }/*。 @ return * @ throws exception */publicfinalstaticstringencrypt (string data stringcryptkey (throws exception (return byte2hex } privatestaticbyte [ ] encrypt [ byte [ ] data,byte [ ] key ] throws exception {//des算法包括可靠的随机数源securerandomsr //创建一个密钥工厂,并使用它将DESKeySpec转换为SecretKey对象secretkeyfactorykeyfactory=secret keyfactory.getinstance (algorithm ); secretkeysecurekey=keyfactory.generate secret (dks; //Cipher对象实际完成加密操作的cipher cipher=cipher.getinstance (algorithm ); 用加密密钥初始化cipher.init (cipher.encrypt _ mode,securekey,sr ); //现在获取数据并加密//正式执行加密操作的returncipher.dofinal(data ); } privatestaticbyte [ ] decrypt [ ] data,byte [ ] key ] throws exception {//des算法包括可靠的随机数源securerandomsr=new sew //密钥工厂,然后使用它将DESKeySpec对象转换为SecretKey对象secret keyfactory=secret keyfactory.getinstance (algorithm ); secretkeysecurekey=keyfactory.generate secret (dks; //Cipher对象实际完成解密操作的cipher cipher=cipher.getinstance (algorithm ); 用加密密钥初始化cipher.init (cipher.decrypt _ mode,securekey,sr ); //现在获取数据并解密//正式执行解密操作的returncipher.dofinal(data ); } privatestaticbyte [ ] hex2byte (byte ) (b ) ) if ) ) b.length%2)!=0) thrownewillegalargumentexception ('长度不是偶数); byte[] b2=new byte[b.length/2]; for(intn=0; n b.length; n =2) stringitem=newstring(b,n,2 ); B2[n/2]=(byte ) integer.parseint(item,16 ); } return b2; } privatestaticstringbyte2hex (byte [ ] b ) { String hs=' '; String stmp=' '; for(intn=0; n b.length; n () stmp=(Java.lang.integer.to hex string ) b[n]0xff ) ); if(stmp.length(==1) hs=hs '0' stmp; else hs=hs stmp; } return hs.toUpperCase (); }/* publicstaticvoidmain (string [ ] args ) stringcryptkey=' schoolcodekeyqxq '; String data='1'; try{stringT1=rdes.encrypt(data,cryptKey ); stringT2=rdes.decrypt(T1,cryptKey ); system.out.println(T1; system.out.println(T2; }catch(exceptione )//todo auto-generatedcatchblocke.print stack trace ); () /

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。