首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java 简略加密

2013-03-28 
java 简单加密package com.test.timerimport java.util.HashMapimport java.util.Mapimport javax.cryp

java 简单加密
package com.test.timer;

import java.util.HashMap;
import java.util.Map;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

public class SecretKeyTest {
private static String pattern =   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static String patternAs = "WREAKHOVZCYBDFGIJLMNPQSTUXwreakhovzcybdfgijlmnpqstux8712904365";
    private static Map<String, String> mapEnc = new HashMap<String, String>();
    private static Map<String, String> mapDec = new HashMap<String, String>();
    public static void init() {
    for (int i = 0; i < pattern.length(); i++) {
            String key = pattern.substring(i, i + 1);
            String value = patternAs.substring(i, i + 1);
            mapEnc.put(key, value);
        }
        for (int i = 0; i < pattern.length(); i++) {
            String key = patternAs.substring(i, i + 1);
            String value = pattern.substring(i, i + 1);
            mapDec.put(key, value);
        }
    }
    public static String toEncryption(String str) {
    init();
        StringBuffer buff = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            String src = str.substring(i, i + 1);
            if (src.matches("[A-Z]")) {
                buff.append(mapEnc.get(src));
            } else if (src.matches("[a-z]")) {
            buff.append(mapEnc.get(src));
            } else if (src.matches("[0-9]")) {
            buff.append(mapEnc.get(src));
            } else {
                buff.append(src);
            }
        }
        return buff.toString();
    }

    public static String toDecrypt(String str) {
    init();
        StringBuffer buff = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            String src = str.substring(i, i + 1);
            if (src.matches("[A-Z]")) {
                buff.append(mapDec.get(src));
            } else if (src.matches("[a-z]")) {
            buff.append(mapDec.get(src));
            } else if (src.matches("[0-9]")) {
            buff.append(mapDec.get(src));
            } else {
            buff.append(src);
            }
        }
        return buff.toString();
    }
    public static void main(String[] args) throws Exception {
        String src = "ABCdef";
        String dest = toEncryption(src);
        src = toDecrypt(dest);
        System.out.println(dest);
        System.out.println(src);
    }

   

}

热点排行