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.
}
}
Base64位解密算法
最新推荐文章于 2024-07-27 17:56:15 发布