java实现的RC4加密解密算法示例
本文实例讲述了java实现的RC4加密解密算法。分享给大家供大家参考,具体如下:
有一个项目,需要解析一个用户提供的rc4加密后的文件,特意搜索整理了一个Java版本的RC4加解密算法。
publicstaticStringHloveyRC4(StringaInput,StringaKey) { int[]iS=newint[256]; byte[]iK=newbyte[256]; for(inti=0;i<256;i++) iS[i]=i; intj=1; for(shorti=0;i<256;i++) { iK[i]=(byte)aKey.charAt((i%aKey.length())); } j=0; for(inti=0;i<255;i++) { j=(j+iS[i]+iK[i])%256; inttemp=iS[i]; iS[i]=iS[j]; iS[j]=temp; } inti=0; j=0; char[]iInputChar=aInput.toCharArray(); char[]iOutputChar=newchar[iInputChar.length]; for(shortx=0;x加密和解密都用这一个方法。也就是说参数StringaInput可以传一个明文,也可以传一个加密后的字符串,程序会自动的识别。然后执行加解密的响应操作。
使用例子如下:
publicstaticvoidmain(String[]args){ StringinputStr="做个好男人"; Stringkey="abcdefg"; Stringstr=HloveyRC4(inputStr,key); //打印加密后的字符串 System.out.println(str); //打印解密后的字符串 System.out.println(HloveyRC4(str,key)); }PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password迅雷、快车、旋风URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode更多关于java相关内容感兴趣的读者可查看本站专题:《Java数学运算技巧总结》、《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《java日期与时间操作技巧汇总》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。