JAVA语言用代码实现RC4加解密
小标 2018-09-11 来源 : 阅读 2247 评论 0

摘要:本文主要向大家介绍了JAVA语言用代码实现RC4加解密,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

本文主要向大家介绍了JAVA语言用代码实现RC4加解密,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。

一.前言
在密码学中,RC4是一种流加密算法,密钥长度可变。它加解密使用相同的密钥,因此也属于对称加密算法。
二.示例代码
public class RC4Util {     public static String encryRC4String(String data, String key) {        if (data == null || key == null) {            return null;        }        return toHexString(asString(encryRC4Byte(data, key)));    }     public static byte[] encryRC4Byte(String data, String key) {        if (data == null || key == null) {            return null;        }        byte bData[] = data.getBytes();        return RC4Base(bData, key);    }     public static String decryRC4(String data, String key) {        if (data == null || key == null) {            return null;        }        return new String(RC4Base(HexString2Bytes(data), key));    }     public static String decryRC4(byte[] data, String key) {        if (data == null || key == null) {            return null;        }        return asString(RC4Base(data, key));    }     private static String asString(byte[] buf) {        StringBuffer strbuf = new StringBuffer(buf.length);        for (int i = 0; i < buf.length; i++) {            strbuf.append((char) buf[i]);        }        return strbuf.toString();    }     private static byte[] initKey(String aKey) {        byte[] bkey = aKey.getBytes();        byte state[] = new byte[256];         for (int i = 0; i < 256; i++) {            state[i] = (byte) i;        }        int index1 = 0;        int index2 = 0;        if (bkey == null || bkey.length == 0) {            return null;        }        for (int i = 0; i < 256; i++) {            index2 = ((bkey[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;            byte tmp = state[i];            state[i] = state[index2];            state[index2] = tmp;            index1 = (index1 + 1) % bkey.length;        }        return state;    }     private static String toHexString(String s) {        String str = "";        for (int i = 0; i < s.length(); i++) {            int ch = (int) s.charAt(i);            String s4 = Integer.toHexString(ch & 0xFF);            if (s4.length() == 1) {                s4 = '0' + s4;            }            str = str + s4;        }        return str;// 0x表示十六进制    }     private static byte[] HexString2Bytes(String src) {        int size = src.length();        byte[] ret = new byte[size / 2];        byte[] tmp = src.getBytes();        for (int i = 0; i < size / 2; i++) {            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);        }        return ret;    }     private static byte uniteBytes(byte src0, byte src1) {        char _b0 = (char) Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();        _b0 = (char) (_b0 << 4);        char _b1 = (char) Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();        byte ret = (byte) (_b0 ^ _b1);        return ret;    }     private static byte[] RC4Base(byte[] input, String mKkey) {        int x = 0;        int y = 0;        byte key[] = initKey(mKkey);        int xorIndex;        byte[] result = new byte[input.length];         for (int i = 0; i < input.length; i++) {            x = (x + 1) & 0xff;            y = ((key[x] & 0xff) + y) & 0xff;            byte tmp = key[x];            key[x] = key[y];            key[y] = tmp;            xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;            result[i] = (byte) (input[i] ^ key[xorIndex]);        }        return result;    }}
三.测试代码
String encryStr = RC4Util.encryRC4String("测试", "123456");System.out.println("加密后得到得字符串:"+encryStr);String decryStr = RC4Util.decryRC4(encryStr, "123456");System.out.println("解密后得到得字符串:"+decryStr);
四.运行结果
加密后得到得字符串:e64df58f8ba6
解密后得到得字符串:测试    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 1
看完这篇文章有何感觉?已经有1人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved