一个简单的php解密方法,效果很好并正确生成输出。这是代码。
function decryption($Str)
{
$key = "huasHIYhkasdho1";
$Newstr="";
$lenKey=strlen($key);
$KeyPos=1;
$LenStr=strlen($Str);
$str=strrev($Str);
for ($x=$LenStr; $x>=1; $x--)
{
//Step(-1); // WARNING: assuming Step is an external function
$DecCharNum=ord(substr($str,$x-1,1))-ord(substr($key,$KeyPos-1,1)) + 256;
$Newstr.=chr($DecCharNum % 256);
$KeyPos++;
if ($KeyPos > $lenKey)
{
$KeyPos=1;
}
}
$Newstr=strrev($Newstr);
return $Newstr;
}
但我正在尝试将其转换为java。大多数情况下我已经完成了,但是我遇到的问题是Charset,我无法弄清楚Charset在上述方法中传递的字符串。这是我需要解密的字符串的图像。
此图像是在Windows操作系统(Windows7)中拍摄的实际快照。 http://i41.tinypic.com/w9h2kj.png 相同的Java代码
import java.io.*;
public class FileToArrayOfBytes
{
public static void main( String[] args )
{
FileInputStream fileInputStream=null;
File file = new File("Report.txt");
String key = "huasHIYhkasdho1";
String Newstr = "";
int lenKey = key.length();
System.out.println("Key Length : "+lenKey);
int KeyPos=1;
byte[] bFile = new byte[(int) file.length()];
try {
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
System.out.println("bFile length : "+bFile.length);
int LenStr=bFile.length;
System.out.println("String Length : "+LenStr);
for (int x = bFile.length; x > 0; x--)
{
System.out.println("========================================");
int DecCharNum = ( (int)bFile[x-1] ) - ( (int)key.charAt(KeyPos-1) ) + 256;
System.out.println("sub 1 = "+ (char)bFile[x-1]);
System.out.println("sub 2 = " + key.charAt(KeyPos-1));
System.out.println("ord 1 = " + (int)bFile[x-1]);
System.out.println("ord 2 = " + (int)key.charAt(KeyPos-1));
System.out.println("Sub = "+DecCharNum);
Newstr += (char) (DecCharNum % 256);
System.out.println("Dec Value = "+DecCharNum);
System.out.println("New String = "+ Newstr );
KeyPos++;
System.out.println("Position "+KeyPos);
if (KeyPos > lenKey)
{
System.out.println("++++++++++++++++++++++++++++++++++++++");
KeyPos=1;
System.out.println("++++++++++++++++++++++++++++++++++++++");
}
System.out.println("========================================");
}
System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
}
}
}
以上代码的输出是
Key Length : 15
bFile length : 26
String Length : 26
========================================
sub 1 =
sub 2 = h
ord 1 = 10
ord 2 = 104
Sub = 162
Dec Value = 162
New String = ¢
Position 2
========================================
========================================
sub 1 = モ
sub 2 = u
ord 1 = -109
ord 2 = 117
Sub = 30
Dec Value = 30
New String = ¢
Position 3
========================================
========================================
sub 1 = ロ
sub 2 = a
ord 1 = -101
ord 2 = 97
Sub = 58
Dec Value = 58
New String = ¢:
Position 4
========================================
========================================
sub 1 = ル
sub 2 = s
ord 1 = -103
ord 2 = 115
Sub = 38
Dec Value = 38
New String = ¢:&
Position 5
========================================
========================================
sub 1 = ロ
sub 2 = H
ord 1 = -101
ord 2 = 72
Sub = 83
Dec Value = 83
New String = ¢:&S
Position 6
========================================
========================================
sub 1 = マ
sub 2 = I
ord 1 = -113
ord 2 = 73
Sub = 70
Dec Value = 70
New String = ¢:&SF
Position 7
========================================
========================================
sub 1 = z
sub 2 = Y
ord 1 = 122
ord 2 = 89
Sub = 289
Dec Value = 289
New String = ¢:&SF!
Position 8
========================================
========================================
sub 1 = ᆱ
sub 2 = h
ord 1 = -85
ord 2 = 104
Sub = 67
Dec Value = 67
New String = ¢:&SF!C
Position 9
========================================
========================================
sub 1 = メ
sub 2 = k
ord 1 = -110
ord 2 = 107
Sub = 39
Dec Value = 39
New String = ¢:&SF!C'
Position 10
========================================
========================================
sub 1 = ᆭ
sub 2 = a
ord 1 = -90
ord 2 = 97
Sub = 69
Dec Value = 69
New String = ¢:&SF!C'E
Position 11
========================================
========================================
sub 1 = ᆳ
sub 2 = s
ord 1 = -83
ord 2 = 115
Sub = 58
Dec Value = 58
New String = ¢:&SF!C'E:
Position 12
========================================
========================================
sub 1 = s
sub 2 = d
ord 1 = 115
ord 2 = 100
Sub = 271
Dec Value = 271
New String = ¢:&SF!C'E:
Position 13
========================================
========================================
sub 1 = ᅠ
sub 2 = h
ord 1 = -96
ord 2 = 104
Sub = 56
Dec Value = 56
New String = ¢:&SF!C'E:8
Position 14
========================================
========================================
sub 1 = ᅠ
sub 2 = o
ord 1 = -96
ord 2 = 111
Sub = 49
Dec Value = 49
New String = ¢:&SF!C'E:81
Position 15
========================================
========================================
sub 1 = ヤ
sub 2 = 1
ord 1 = -108
ord 2 = 49
Sub = 99
Dec Value = 99
New String = ¢:&SF!C'E:81c
Position 16
++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++
========================================
========================================
sub 1 = ᆪ
sub 2 = h
ord 1 = -93
ord 2 = 104
Sub = 59
Dec Value = 59
New String = ¢:&SF!C'E:81c;
Position 2
========================================
========================================
sub 1 = ラ
sub 2 = u
ord 1 = -105
ord 2 = 117
Sub = 34
Dec Value = 34
New String = ¢:&SF!C'E:81c;"
Position 3
========================================
========================================
sub 1 = ᄆ
sub 2 = a
ord 1 = -79
ord 2 = 97
Sub = 80
Dec Value = 80
New String = ¢:&SF!C'E:81c;"P
Position 4
========================================
========================================
sub 1 = ᆲ
sub 2 = s
ord 1 = -84
ord 2 = 115
Sub = 57
Dec Value = 57
New String = ¢:&SF!C'E:81c;"P9
Position 5
========================================
========================================
sub 1 = ヒ
sub 2 = H
ord 1 = -117
ord 2 = 72
Sub = 67
Dec Value = 67
New String = ¢:&SF!C'E:81c;"P9C
Position 6
========================================
========================================
sub 1 = y
sub 2 = I
ord 1 = 121
ord 2 = 73
Sub = 304
Dec Value = 304
New String = ¢:&SF!C'E:81c;"P9C0
Position 7
========================================
========================================
sub 1 = y
sub 2 = Y
ord 1 = 121
ord 2 = 89
Sub = 288
Dec Value = 288
New String = ¢:&SF!C'E:81c;"P9C0
Position 8
========================================
========================================
sub 1 = ᆭ
sub 2 = h
ord 1 = -90
ord 2 = 104
Sub = 62
Dec Value = 62
New String = ¢:&SF!C'E:81c;"P9C0 >
Position 9
========================================
========================================
sub 1 = ユ
sub 2 = k
ord 1 = -107
ord 2 = 107
Sub = 42
Dec Value = 42
New String = ¢:&SF!C'E:81c;"P9C0 >*
Position 10
========================================
========================================
sub 1 = ᆰ
sub 2 = a
ord 1 = -86
ord 2 = 97
Sub = 73
Dec Value = 73
New String = ¢:&SF!C'E:81c;"P9C0 >*I
Position 11
========================================
========================================
sub 1 = ン
sub 2 = s
ord 1 = -99
ord 2 = 115
Sub = 42
Dec Value = 42
New String = ¢:&SF!C'E:81c;"P9C0 >*I*
Position 12
========================================
Done
上述代码所需的输出是:201BF2811EB18006FD2013455
请帮我弄清楚问题,我知道问题是Charset使用的,我无法弄明白该怎么做才能正确输出。
答案 0 :(得分:1)
此问题与字符集无关。您将输入读取为字节而不是字符,预期输出主要是ASCII。对待它的方式,在byte
和char
之间进行转换,您将输出作为ISO-8859-1,就像PHP通常那样。
问题的核心是方向。 PHP代码反转字符串,然后从后到前迭代输入,然后反转结果。这与从前到后迭代输入相同。但是你将迭代保持在前面,同时没有移植strrev
个调用。将你的循环改为
for (int x = 1; x <= bFile.length; x++)
你应该没事。