1

我有一个 XOR“加密”类的以下 JAVA 类:

import java.io.PrintStream;

public class Encryptor
{

    private static final String m_strPrivateKey = "4p0L@r1$";

    public Encryptor()
    {
    }

    public static String encrypt(String pass)
    {
        String strTarget = XORString(pass);
        strTarget = StringToHex(strTarget);
        return strTarget;
    }

    public static String decrypt(String pass)
    {
        String strTarget = HexToString(pass);
        strTarget = XORString(strTarget);
        return strTarget;
    }

    private static String GetKeyForLength(int nLength)
    {
        int nKeyLen = "4p0L@r1$".length();
        int nRepeats = nLength / nKeyLen + 1;
        String strResult = "";
        for(int i = 0; i < nRepeats; i++)
        {
            strResult = strResult + "4p0L@r1$";
        }

        return strResult.substring(0, nLength);
    }

    private static String HexToString(String str)
    {
        StringBuffer sb = new StringBuffer();
        char buffDigit[] = new char[4];
        buffDigit[0] = '0';
        buffDigit[1] = 'x';
        int length = str.length() / 2;
        byte bytes[] = new byte[length];
        for(int i = 0; i < length; i++)
        {
            buffDigit[2] = str.charAt(i * 2);
            buffDigit[3] = str.charAt(i * 2 + 1);
            Integer b = Integer.decode(new String(buffDigit));
            bytes[i] = (byte)b.intValue();
        }

        return new String(bytes);
    }

    private static String XORString(String strTarget)
    {
        int nTargetLen = strTarget.length();
        String strPaddedKey = GetKeyForLength(nTargetLen);
        String strResult = "";
        byte bytes[] = new byte[nTargetLen];
        for(int i = 0; i < nTargetLen; i++)
        {
            int b = strTarget.charAt(i) ^ strPaddedKey.charAt(i);
            bytes[i] = (byte)b;
        }

        String result = new String(bytes);
        return result;
    }

    private static String StringToHex(String strInput)
    {
        StringBuffer hex = new StringBuffer();
        int nLen = strInput.length();
        for(int i = 0; i < nLen; i++)
        {
            char ch = strInput.charAt(i);
            int b = ch;
            String hexStr = Integer.toHexString(b);
            if(hexStr.length() == 1)
            {
                hex.append("0");
            }
            hex.append(Integer.toHexString(b));
        }

        return hex.toString();
    }

    public static void main(String args[])
    {
        if(args.length < 1)
        {
            System.err.println("Missing password!");
            System.exit(-1);
        }
        String pass = args[0];
        String pass2 = encrypt(pass);
        System.out.println("Encrypted: " + pass2);
        pass2 = decrypt(pass2);
        System.out.println("Decrypted: " + pass2);
        if(!pass.equals(pass2))
        {
            System.out.println("Test Failed!");
            System.exit(-1);
        }
    }
}

我试图像这样将它移植到 Perl:

#!/usr/bin/perl

use strict;
use warnings;

my $pass = shift || die "Missing password!\n";
my $pass2 = encrypt($pass);
print "Encrypted: $pass2\n";
$pass2 = decrypt($pass2);
print "Decrypted: $pass2\n";
if ($pass ne $pass2) {
    print "Test Failed!\n";
    exit(-1);
}

sub encrypt {
    my $pass = shift;
    my $strTarget = XORString($pass);
    $strTarget = StringToHex($strTarget);
    return $strTarget;
}

sub decrypt {
    my $pass = shift;
    my $strTarget = HexToString($pass);
    $strTarget = XORString($strTarget);
    return $strTarget;
}

sub GetKeyForLength {
    my $nLength = shift;
    my $nKeyLen = length '4p0L@r1$';
    my $nRepeats = $nLength / $nKeyLen + 1;
    my $strResult = '4p0L@r1$' x $nRepeats;
    return substr $strResult, 0, $nLength;
}

sub HexToString {
    my $str = shift;
    my @bytes;

    while ($str =~ s/^(..)//) {
        my $b = eval("0x$1");
        push @bytes, chr sprintf("%d", $b);
    }
    return join "", @bytes;
}

sub XORString {
    my $strTarget = shift;
    my $nTargetLen = length $strTarget;
    my $strPaddedKey = GetKeyForLength($nTargetLen);
    my @bytes;

    while ($strTarget) {
        my $b = (chop $strTarget) ^ (chop $strPaddedKey);
        unshift @bytes, $b;
    }
    return join "", @bytes;
}

sub StringToHex {
    my $strInput = shift;
    my $hex = "";
    for my $ch (split //, $strInput) {
        $hex .= sprintf("%02x", ord $ch);
    }
    return $hex;
}

代码看起来不错,但问题是 JAVA 类输出的结果与 Perl 代码不同。在 JAVA 中,我有纯文本密码

薄荷糖

它被编码为

&4\=80CHB'

我应该如何处理我的 Perl 脚本以获得相同的结果?我哪里做错了?

另外两个例子:纯文本

07ch4ssw3bby

被编码为:

,#(0\=DM.'@'8WQ2T

(注意@后面的空格)

最后一个例子,纯文本:

conf75

编码为:

&7]P0G-#!

感谢帮助!

最后,感谢 Joni Salonen:

#!/usr/bin/perl
# XOR password decoder
# Greets: Joni Salonen @ stackoverflow.com

$key = pack("H*","3cb37efae7f4f376ebbd76cd");

print "Enter string to decode: ";
$str=<STDIN>;chomp $str; $str =~ s/\\//g;
$dec = decode($str);
print "Decoded string value: $dec\n";

sub decode{ #Sub to decode
    @subvar=@_;
    my $sqlstr = $subvar[0];
    $cipher = unpack("u", $sqlstr);
    $plain = $cipher^$key;
    return substr($plain, 0, length($cipher));
}

我唯一也是最后一个问题是,当找到“\”(实际上是“\\”作为转义真实字符的字符)时,解密出错:-\ 示例编码字符串:

"(4\\4XB\:7"G@, "

(我用双引号对其进行了转义,字符串的最后一个字符是一个空格,它应该解码为

“ovFsB6mu”

更新:感谢 Joni Salonen,我有 100% 的最终版本:

#!/usr/bin/perl
# XOR password decoder
# Greets: Joni Salonen @ stackoverflow.com

$key = pack("H*","3cb37efae7f4f376ebbd76cd");

print "Enter string to decode: ";
$str=<STDIN>;chomp $str; $str =~s/\\(.)/$1/g;
$dec = decode($str);
print "Decoded string value: $dec\n";

sub decode{ #Sub to decode
    @subvar=@_;
    my $sqlstr = $subvar[0];
    $cipher = unpack("u", $sqlstr);
    $plain = $cipher^$key;
    return substr($plain, 0, length($cipher));
}
4

1 回答 1

1

您的加密循环会跳过$strTargetif 它的第一个字符'0'。您可以将其与空字符串进行比较,而不是检查它是否为“真”:

while ($strTarget ne '') {
    my $b = (chop $strTarget) ^ (chop $strPaddedKey);
    unshift @bytes, $b;
}

更新:这个程序解密你的字符串:

use feature ':5.10';

$key = pack("H*","3cb37efae7f4f376ebbd76cd");

say decrypt("&4\=80CHB'");          # mentos
say decrypt(",#(0\=DM.'@ '8WQ2T");  # 07ch4ssw3bby
say decrypt("&7]P0G-#!");           # conf75

sub decrypt {
    $in = shift;
    $cipher = unpack("u", $in);
    $plain = $cipher^$key;
    return substr($plain, 0, length($cipher));
}
于 2012-04-10T08:14:01.477 回答