Rc4加密解密

记录下,以后用到就好找了

package .....
import java.io.UnsupportedEncodingException;

public class RC4 {
	
	private static String CHARSET = "UTF-8";
	
    public static String decry_RC4(byte[] data, String key) {
        if (data == null || key == null) {
            return null;
        }
        return asString(RC4Base(data, key));
    }
   
   
    public static String decry_RC4(String data, String key) {
        if (data == null || key == null) {
            return null;
        }
        String tmp = null;
		try {
			tmp = new String(RC4Base(HexString2Bytes(data), key), CHARSET);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        return tmp;
    }
   
   
    public static byte[] encry_RC4_byte(String data, String key) {
        if (data == null || key == null) {
            return null;
        }
        byte b_data[] = null;
		try {
			b_data = data.getBytes(CHARSET);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        return RC4Base(b_data, key);
    }
   
   
    public static String encry_RC4_string(String data, String key) {
        if (data == null || key == null) {
            return null;
        }
        return toHexString(asString(encry_RC4_byte(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[] b_key = null;
		try {
			b_key = aKey.getBytes(CHARSET);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        byte state[] = new byte[256];

        for (int i = 0; i < 256; i++) {
            state[i] = (byte) i;
        }
        int index1 = 0;
        int index2 = 0;
        if (b_key == null || b_key.length == 0) {
            return null;
        }
        for (int i = 0; i < 256; i++) {
            index2 = ((b_key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
            byte tmp = state[i];
            state[i] = state[index2];
            state[index2] = tmp;
            index1 = (index1 + 1) % b_key.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 = null;
		try {
			tmp = src.getBytes(CHARSET);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		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;
    }
    
    public static void main(String[] args) {
    	    String inputStr = "你好";
    	   // String inputStr = "{DATE:\"2013-12-15\",APPCount:-1,DevCount:-1,Info:\"11\"}";
    	    //String key = "9957E7BD4D8DD000C733B796AB2AD9B7";   
    	    String key = "111";   
    	    String str=encry_RC4_string(inputStr,key);
    	    //String str="706C9397AE41F243DAC6936A9A02DEDBBD64A91AB58E03304C36BF7964B0285031F240E53E85BD988FB04CAF204BE5C13CA473BA8346B5AF7432E2B26054D68FB5B3B48570DD6541734461";
    	    //打印加密后的字符串       
    	    System.out.println(str); 
    	    //String licenseStr = "706c9397ae41f243dac6936a9a01dedbb064a91ab58e03304c36bf796bb0342476a847ef3da8a6d7ccf55ac47f1dbacb2eb76ed190";
    		//String keyMD5 = "D0D242E005AC3C12EA219D1E1D6A3DEC";
    	    //打印解密后的字符串       
    	    
    	    str= "4358B4ABE693C24D2F2523AE64275096D57E4C5F59E0A4693EE510C43BE9E60093605873F0284D9610B5706995AEEB28832CEFBAE9DF55F423BEF1AFC2755E80246861B62AE87F648D3828FB4969B0A108C714B5F9CB65C5590EBF4CC78DA97C49FB";
        	key = "9999999999999"; 
    	    System.out.println(decry_RC4(str,key));
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RC4是一种流加密算法,由Ronald Rivest于1987年所设计。它被广泛应用于SSL、TLS、WEP、WPA等安全协议中,是一种高效的加密算法。Python支持RC4加密解密算法,只需要使用相应的库即可。 在Python中,使用RC4加密解密算法需要安装pycryptodome库,该库支持多种加密解密算法。使用以下命令可安装该库: pip install pycryptodome 安装完成后,可以使用pycryptodome库中的Crypto.Cipher包中的ARC4类进行RC4加密解密操作。其中,ARC4.new(key)用于创建一个ARC4实例,key为RC4加密密钥。encrypt(plain)方法用于加密明文,plain为要加密的明文字符串;decrypt(cipher)方法用于解密密文,cipher为要解密的密文字符串。 下面是一个简单的RC4加密解密示例: from Crypto.Cipher import ARC4 # RC4加密 def rc4_encrypt(key, plain): rc4 = ARC4.new(key) cipher = rc4.encrypt(plain) return cipher # RC4解密 def rc4_decrypt(key, cipher): rc4 = ARC4.new(key) plain = rc4.decrypt(cipher) return plain # 测试 key = b'123456' plain = b'Hello, World!' cipher = rc4_encrypt(key, plain) print(cipher) # b'h\xab\x0e\xce\xf1d\xa7\x8fo\xa2K\xf7-Q\xcc' plain = rc4_decrypt(key, cipher) print(plain) # b'Hello, World!' 在该示例中,我们使用了ARC4类进行RC4加密解密操作,可以发现在加密解密时,使用的是同一个密钥。密钥的长度可以任意指定。需要注意的是,由于RC4算法不适合用于长数据的加密,因此,在使用RC4加密时,建议每次仅加密一小段数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值