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

来源:互联网 发布:统计数据库表的记录数 编辑:程序博客网 时间:2024/06/05 06:55

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

Java代码  收藏代码
  1. package cn.std.util;  
  2.   
  3. import java.nio.charset.Charset;  
  4.   
  5.   
  6. public class DeEnCode {  
  7.   
  8.     private static final String key0 = "FECOI()*&<MNCXZPKL";  
  9.     private static final Charset charset = Charset.forName("UTF-8");  
  10.     private static byte[] keyBytes = key0.getBytes(charset);  
  11.       
  12.     public static String encode(String enc){  
  13.         byte[] b = enc.getBytes(charset);  
  14.         for(int i=0,size=b.length;i<size;i++){  
  15.             for(byte keyBytes0:keyBytes){  
  16.                 b[i] = (byte) (b[i]^keyBytes0);  
  17.             }  
  18.         }  
  19.         return new String(b);  
  20.     }  
  21.       
  22.     public static String decode(String dec){  
  23.         byte[] e = dec.getBytes(charset);  
  24.         byte[] dee = e;  
  25.         for(int i=0,size=e.length;i<size;i++){  
  26.             for(byte keyBytes0:keyBytes){  
  27.                 e[i] = (byte) (dee[i]^keyBytes0);  
  28.             }  
  29.         }  
  30.         return new String(e);  
  31.     }  
  32.   
  33.     public static void main(String[] args) {  
  34.         String s="you are right";  
  35.         String enc = encode(s);  
  36.         String dec = decode(enc);  
  37.         System.out.println(enc);  
  38.         System.out.println(dec);  
  39.     }  
  40. }  
0 0