码迷,mamicode.com
首页 > 编程语言 > 详细

Java简单的加密解密算法,使用异或运算

时间:2014-12-22 10:54:09      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

package cn.std.util;

import java.nio.charset.Charset;


public class DeEnCode {

    private static final String key0 = "FECOI()*&<MNCXZPKL";
    private static final Charset charset = Charset.forName("UTF-8");
    private static byte[] keyBytes = key0.getBytes(charset);
    
    public static String encode(String enc){
        byte[] b = enc.getBytes(charset);
        for(int i=0,size=b.length;i<size;i++){
            for(byte keyBytes0:keyBytes){
                b[i] = (byte) (b[i]^keyBytes0);
            }
        }
        return new String(b);
    }
    
    public static String decode(String dec){
        byte[] e = dec.getBytes(charset);
        byte[] dee = e;
        for(int i=0,size=e.length;i<size;i++){
            for(byte keyBytes0:keyBytes){
                e[i] = (byte) (dee[i]^keyBytes0);
            }
        }
        return new String(e);
    }

    public static void main(String[] args) {
        String s="you are right";
        String enc = encode(s);
        String dec = decode(enc);
        System.out.println(enc);
        System.out.println(dec);
    }
}

 

Java简单的加密解密算法,使用异或运算

标签:

原文地址:http://www.cnblogs.com/ziwuxian/p/4177476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!