Base64位解密算法


package mains;


/***
* Base64位解密算法
* @author Administrator xlaohe1
*
*/
public class Base64Decrypt {

/**
* 64位字符
*/
private final static String Base64Ret = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/**
* 放入加密的字符转换为二进制
* @author Administrator xlaohe1
*
* @param str 加密后的字符
* @return 加密后的字符的ascii码的二进制
*/
private static String getAsciiByPWD(String str) {
str = str.replaceAll("=", "");
String ss = "";
for (int i = 0; i < str.length(); i++) {
String s = Integer.toBinaryString(
Base64Ret.indexOf(str.substring(i, i + 1))); // 转换为二进制
int slen = s.length();
if (slen < 6)
for (int l = 0; l < 6 - slen; l++)
s = "0" + s; // 不足6位+0
ss += s;
}
return ss.substring(0, ss.length() - ss.length() % 8); // 因为base64是8字节,不足加的0,所以多出来的舍弃
}

/**
* 二进制转换为ascii码
* @author Administrator xlaohe1
*
* @param str getAsciiByPWD后的二进制
*/
private static void TwoToASCII(String str) {
// String str = "011011010110010101101101";
System.out.print((char) Integer.parseInt(Integer.valueOf(
str.substring(0, 8), 2).toString())); // 这里输出的就是解密后的字符串
str = str.substring(8);
if (str.length() != 0)
TwoToASCII(str);
}

public static void main(String args[]) throws Exception {
try{
String str = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
String slst = getAsciiByPWD(str);// bWVt==
System.out.println(slst);
TwoToASCII(slst);
System.out.println();
}catch(Exception e) {
System.out.println("输入错误");
}
//输出结果为:Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值