shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.walker.remote;
 
import com.walker.infrastructure.utils.StringUtils;
 
import java.io.UnsupportedEncodingException;
 
/**
 * 抽象加密解密编码器-默认实现
 * @author shikeying
 * @date 2015-2-5
 *
 */
public abstract class AbstractByteCoder {
    
//    protected final static String CHARSET_UTF8 = "utf-8";
    
    public byte[] encrypt(String paramString){
        try{
          return encrypt(paramString.getBytes(StringUtils.DEFAULT_CHARSET_UTF8));
        } catch (UnsupportedEncodingException e){
            throw new RuntimeException();
        }
    }
 
    public String encryptToString(byte[] paramArrayOfByte){
        byte[] arrayOfByte = encrypt(paramArrayOfByte);
        if (arrayOfByte == null)
          return null;
        try{
          return new String(arrayOfByte, StringUtils.DEFAULT_CHARSET_UTF8);
        }catch (UnsupportedEncodingException e){
            throw new RuntimeException();
        }
    }
 
    /**
     * 把给定的字节数组加密
     * @param sourceBytes 原始字节数组
     * @return
     */
    public abstract byte[] encrypt(byte[] sourceBytes);
 
//    public final String b(String paramString){
//        try{
//          byte[] arrayOfByte = encrypt(paramString);
//          if (arrayOfByte == null)
//            return null;
//          return new String(arrayOfByte, CHARSET_UTF8);
//        }
//        catch (UnsupportedEncodingException e){
//            throw new RuntimeException();
//        }
//    }
 
    /**
     * 把已经加密(通过encrypt方法)的字节数组解密
     * @param encodeBytes 加密过的字节数据组
     * @return
     */
    public abstract byte[] decrypt(byte[] encodeBytes);
 
    /**
     * 解密给定字符串
     * @param paramString 给定加过密的字符串
     * @return
     */
    public byte[] decrypt(String paramString){
        if (paramString == null)
          return null;
        try{
          return decrypt(paramString.getBytes(StringUtils.DEFAULT_CHARSET_UTF8));
        }
        catch (UnsupportedEncodingException e){
            throw new RuntimeException();
        }
    }
}