Base32加解密算法

一、参考文档 :http://hi.baidu.com/xiaoyaoyiyiyun/item/1df00a0dba2e7c78bfe97ef7

 

二、代码实现:

 

Base32.java

 

package com.xiaxing.learning.Base32;

public class Base32 {
    
    private static final char[] ALPHABET = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
            'Y', 'Z', '2', '3', '4', '5', '6', '7'
    };
    
    private static final byte[] DECODE_TABLE;
    
    static {
        DECODE_TABLE = new byte[128];
        
        for (int i = 0; i < DECODE_TABLE.length; i++) {
            DECODE_TABLE[i] = (byte)0xFF;
        }
        for (int i = 0; i < ALPHABET.length; i++) {
            DECODE_TABLE[(int)ALPHABET[i]] = (byte)i;
            if (i < 24) {
                DECODE_TABLE[(int)Character.toLowerCase(ALPHABET[i])] = (byte)i;
            }
        }
    }

    public static String encode(byte[] data) {
        
        char[] chars = new char[((data.length * 8) / 5) + ((data.length % 5) != 0 ? 1 : 0)];
        
        for (int i = 0, j = 0, index = 0; i < chars.length; i++) {
            if (index > 3) {
                int b = data[j] & (0xFF >> index);
                index = (index + 5) % 8;
                b <<= index;
                if (j < data.length - 1) {
                    b |= (data[j + 1] & 0xFF) >> (8 - index);
                }
                chars[i] = ALPHABET[b];
                j++;
            } else {
                chars[i] = ALPHABET[((data[j] >> (8 - (index + 5))) & 0x1F)];
                index = (index + 5) % 8;
                if (index == 0) {
                    j++;
                }
            }
        }
        
        return new String(chars);
    }

    public static byte[] decode(String s) throws Exception {
        
        char[] stringData = s.toCharArray();
        byte[] data = new byte[(stringData.length * 5) / 8];
        
        for (int i = 0, j = 0, index = 0; i < stringData.length; i++) {
            int val;
            
            try {
                val = DECODE_TABLE[stringData[i]];
            } catch (ArrayIndexOutOfBoundsException e) {
                throw new Exception("Illegal character");
            }
            
            if (val == 0xFF) {
                throw new Exception("Illegal character");
            }
            
            if (index <= 3) {
                index = (index + 5) % 8;
                if (index == 0) {
                    data[j++] |= val;
                }
                else {
                    data[j] |= val << (8 - index);
                }
            } else {
                index = (index + 5) % 8;
                data[j++] |= (val >> index);
                if (j < data.length) {
                    data[j] |= val << (8 - index);
                }
            }
        }
        
        return data;
    }
}
 

Base32Test.java

 

package com.xiaxing.learning.Base32;

import java.io.UnsupportedEncodingException;

public class Base32Test {

    public static class Logger {
        public Logger() {
        }

        public static void v(String string) {
            System.out.println(string);
            return;
        }
    }
    
    public static void main(String[] args) {
        String input = "bhst";
        byte[] inputBuf = input.getBytes();
        
        try {
            String s = new String(inputBuf, "utf-8");
            Logger.v("s : " + s);
        } catch (UnsupportedEncodingException e1) {}
        
        String encoded = Base32.encode(inputBuf);
        
        try {
            byte[] outputBuf = Base32.decode(encoded);
            String s = new String(outputBuf, "utf-8");
            Logger.v("s : " + s);
        } catch (Exception e) {
            Logger.v("Illegal character");
        }
    }
}
 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值