zoukankan      html  css  js  c++  java
  • RC4 对称加密

    public class RC4 {
        byte[] s = new byte[256];
        byte[] key;
        byte keylen;// 4 ~ 16
        int pi = 0;
        int pj = 0;
    
        public RC4() {
            this.keylen = 16;
            randKey();
            ksa();
        }
    
        public RC4(byte keylen) {
            this.keylen = keylen;
            randKey();
            ksa();
        }
    
        public RC4(byte[] key) {
            this.key = key;
            keylen = (byte) key.length;
            ksa();
        }
    
        private void randKey() {
    //        SecureRandom r = new SecureRandom("abc".getBytes());
            Random r = new Random(0xdeadbeef);
            key = new byte[keylen];
            r.nextBytes(key);
        }
    
        public byte[] getKey() {
            return key;
        }
    
        private void ksa() {
            for (int i = 0; i < s.length; i++) {
                s[i] = (byte) i;
            }
            int j = 0;
            for (int i = 0; i < s.length; i++) {                    //使用密钥来交换s的元素
                j = ((j + s[i] & 0xff + key[i % keylen] & 0xff) % 256);    //重复使用密钥
    
                byte tmp = s[i];                                    //交换
                s[i] = s[j];
                s[j] = tmp;
            }
        }
    
        public byte prgaOneByte(byte in) {
            pi = pi + 1 % 256;
            pj = (pj + s[pi] & 0xff);
    
            byte tmp = s[pi];
            s[pi] = s[pj];
            s[pj] = tmp;
    
            int t = (s[pi] & 0xff + s[pj] & 0xff) % 256;
            int k = s[t] & 0xff;
            return (byte) ((k ^ in & 0xff) & 0xff);
        }
    
    
        public void prga(InputStream is, OutputStream os) throws IOException {
            int bufsize = 4 * 1024;
            byte[] buffer = new byte[bufsize]; // 4K
            byte[] outBuffer;
            int len = 0;
            while ((len = is.read(buffer, 0, bufsize)) != -1) { //读取一块
                outBuffer = prga(buffer, 0, len);
                os.write(outBuffer);
            }
        }
    
        public byte[] prga(byte[] buffer, int off, int len) throws IOException {
            byte[] outBuffer = new byte[buffer.length]; // 4K
            for (int i = 0 + off; i < len; i++) {                    //处理这块
                outBuffer[i] = prgaOneByte(buffer[i]);
            }
            return outBuffer;
        }
    
        public static void main(String[] args) throws IOException {
            String msg = "hello wold!";
            byte[] bmsg = msg.getBytes();
            byte[] key;
            System.out.println("bmsg :" + Arrays.toString(bmsg));   //加密前
    
            RC4 rc4 = new RC4();
            key = rc4.getKey();
            byte[] ebmsg = rc4.prga(bmsg, 0, bmsg.length);
            System.out.println("ebmsg :" + Arrays.toString(ebmsg)); //加密后
    
            RC4 rc42 = new RC4(key);
            byte[] debmsg = rc42.prga(ebmsg, 0, ebmsg.length);
            System.out.println("debmsg:" + Arrays.toString(debmsg));//解密后
            System.out.println("debmsg:" + new String(debmsg));
        }
    }

    输出

    bmsg :[104, 101, 108, 108, 111, 32, 119, 111, 108, 100, 33]
    ebmsg :[-76, 34, -32, -95, -127, 85, -93, -16, -126, 110, -56]
    debmsg:[104, 101, 108, 108, 111, 32, 119, 111, 108, 100, 33]
    debmsg:hello wold!
  • 相关阅读:
    31天重构学习笔记28. 为布尔方法命名
    .NET 技术社区之我见(中文篇)
    31天重构学习笔记26. 避免双重否定
    31天重构学习笔记25. 引入契约式设计
    31天重构学习笔记20. 提取子类
    31天重构学习笔记18. 使用条件判断代替异常
    31天重构学习笔记19. 提取工厂类
    31天重构学习笔记24. 分解复杂判断
    31天重构学习笔记23. 引入参数对象
    31天重构学习笔记17. 提取父类
  • 原文地址:https://www.cnblogs.com/cyy12/p/11627756.html
Copyright © 2011-2022 走看看