我需要从Base64解码到十六进制(例如,AAAAAAAAnH0= -> 0000000000009C7D,AAAAAAAAALQ= -> 00000000000000B4),因为在这个站点上,解码功能存储在这个站点上的纯文本:
<script type="text/javascript">
<!--
var hD='0123456789ABCDEF';
function dec2hex(d) {
var h = hD.substr(d&15,1);
while (d>15) {
d>>=4;
h=hD.substr(d&15,1)+h;
}
if (document.frmConvert.chbLowercaseOutput.checked) {
h = h.toLowerCase();
}
return h;
}
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
function base64_decode(input) {
var output = new Array();
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
var orig_input = input;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
if (orig_input != input)
alert ("Warning! Characters outside Base64 range in input string ignored.");
if (input.length % 4) {
alert ("Error: Input length is not a multiple of 4 bytes.");
return "";
}
var j=0;
while (i < input.length) {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output[j++] = chr1;
if (enc3 != 64)
output[j++] = chr2;
if (enc4 != 64)
output[j++] = chr3;
}
return output;
}
function ShowDecodedAsText(val)
{
var target = document.getElementById('divDecodedText');
target.innerHTML = val;
}
function Convert() {
var output = base64_decode(document.frmConvert.encoded.value);
var separator = "";
if (document.frmConvert.chbSeparator.checked)
separator = " 0x";
var hexText = "";
var htmlStr = "<p style=\"font-family: courier new, monospace\"><b>";
for (i=0; i<output.length; i++) {
hexText = hexText + separator + (output[i]<16?"0":"") + dec2hex(output[i]);
if (output[i] >= 32 && output[i] <= 126)
{
switch(String.fromCharCode(output[i])) {
case '&': htmlStr += "&";
break;
case '<': htmlStr += "<";
break;
case '>': htmlStr += ">";
break;
case '"': htmlStr += """;
break;
case '\'': htmlStr += "'";
break;
default: htmlStr += String.fromCharCode(output[i]);
}
}
else {
htmlStr += "<i>[" + output[i] + "]<" + "/i>";
}
}
htmlStr += "<" + "/b><" + "/p>"; // stupid W3C validator...
document.frmConvert.hex.value = hexText;
ShowDecodedAsText(htmlStr);
}
function ConvertToFile() {
Convert();
createFileFromHex(document.frmConvert.hex.value, document.frmConvert.filename.value);
}
</script>我需要用Java实现所有的东西,我编写了类:
public class TomaszDecoder {
private final String hD="0123456789ABCDEF";
private final String keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
private final int HEX_COUNT = 15;
private String dec2hex(int d) {
String h = substr(d & HEX_COUNT);
while (d>15) {
d>>=4;
h = substr(d & HEX_COUNT)+h;
}
return h;
}
private String substr(int start) {
if (start < 0)
start = HEX_COUNT + start;
return hD.substring(start,start+1);
}
private String base64_decode(String input) {
StringBuilder output = new StringBuilder();
int chr1, chr2, chr3;
int enc1, enc2, enc3, enc4;
int i = 0;
/* var orig_input = input;
replace(input,'/[^A-Za-z0-9\+\/\=]/g', "");
if (input.length % 4) {
status = "Error: Input length is not a multiple of 4 bytes";
return [output, status];
}*/
int j=0;
while (i < input.length()) {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output.append(chr1);
if (enc3 != 64)
output.append(chr2);
if (enc4 != 64)
output.append(chr3);
}
/* var status;
if (orig_input != input) {
status = "Warning! Characters outside Base64 range in input string ignored.";
} else {
status = "";
}*/
return output.toString();
}
public String convertFromBase64(String base64String) {
StringBuilder hexText = new StringBuilder();
String separator = "";
/*if (document.frmConvert.chbSeparator.checked)
separator = " 0x";*/
// var lines = document.frmConvert.encoded.value.split('\n');
char[] output = base64_decode(base64String).toCharArray();
for (int j=0; j<output.length; j++) {
hexText.append(separator + (output[j]<16?"0":"") + dec2hex(output[j]));
}
return hexText.toString();
}
}但是他给出了不同的结果,我真的希望你能帮我解决这个问题。
输入- AAAAAAAAALQ=,输出- 30303030303030313830 (正确-000000000000B4)
发布于 2017-12-01 11:16:43
我自己解决了我的问题。我使用犀牛
private void runScript(String javaScriptCode,String functionNameInJavaScriptCode,Object[] params) {
org.mozilla.javascript.Context rhino = org.mozilla.javascript.Context.enter();
rhino.setOptimizationLevel(-1);
try {
Scriptable scope = rhino.initStandardObjects();
rhino.evaluateString(scope,javaScriptCode , "JavaScript", 1, null);
// Get the functionName defined in JavaScriptCode
Object obj = scope.get(functionNameInJavaScriptCode, scope);
if (obj instanceof org.mozilla.javascript.Function) {
org.mozilla.javascript.Function jsFunc = (org.mozilla.javascript.Function) obj;
// Call the function with params
Object jsResult =
jsFunc.call(rhino, scope, scope, params);
// Parse the jsResult object to a String
String result = org.mozilla.javascript.Context.toString(jsResult);
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
org.mozilla.javascript.Context.exit();
}
}呼叫法
String script = readTextFromAssetsFile("tomasz.js");
runScript(script,"convert",new Object[]{"AAAAAAAAnH0="});https://stackoverflow.com/questions/47547368
复制相似问题