qq238472837 2017-10-30 04:49 采纳率: 0%
浏览 1738
已采纳

Python程序转成java程序输出结果不对

Python程序如下:

import base64
import hashlib
def encode_64(string,key):
    key = hashlib.md5(key.encode('utf-8')).hexdigest()
    print key
    key_length = len(key)
    string = hashlib.md5((string+key).encode('utf-8')).hexdigest()[0:8]+string
    print string
    rndkey = []
    box = []
    for i in range(0,256):
        rndkey.append(ord(key[i%key_length]))
        box.append(i)
    j = 0
    for i in range(0,256):
        j = (j+box[i]+rndkey[i])%256
        tmp = box[i]
        box[i] = box[j]
        box[j] = tmp
    a = 0
    j = 0
    result = ""
    for i in range(len(string)):
        a = (a+1)%256
        j = (j+box[a])%256
        tmp = box[a]
        box[a] = box[j]
        box[j] = tmp
        result += chr(ord(string[i])^(box[(box[a]+box[j])%256]))
    result = base64.b64encode(result)
    return result.replace('=','')
string = encode_64("passw0rd","toyou")
print string

java程序如下:

 package nimp;

import java.security.MessageDigest;
import sun.misc.BASE64Encoder;

public class JM {

    private static MessageDigest md5 = null;
    static {
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 用于获取一个String的md5值
     * @param string
     * @return
     */
    public static String getMd5(String str) {
        byte[] bs = md5.digest(str.getBytes());
        StringBuilder sb = new StringBuilder(40);
        for(byte x:bs) {
            if((x & 0xff)>>4 == 0) {
                sb.append("0").append(Integer.toHexString(x & 0xff));
            } else {
                sb.append(Integer.toHexString(x & 0xff));
            }
        }
        return sb.toString();
    }

    public static String enc(String string) throws Exception{
        String result = "";

        String key = getMd5("toyou");
        System.out.println(key);
        int key_length = key.length();
        char[] keyc = key.toCharArray();
        string = getMd5(string+key).substring(0, 8) + string;
        System.out.println(string);
        int string_length = string.length();
        int[] rndkey = new int[256];
        int[] box = new int[256];
        int i,j;
        for(i=0;i<=255;i++){
            rndkey[i] = (int)keyc[i%key_length];
            box[i] = i;
        }
        i = 0;j = 0;
        for(;i<=255;i++){
            j = (j+box[i]+rndkey[i])%256;
            int x = box[i];
            box[i] = box[j];
            box[j] = x;
        }
        int a=0;
        i = 0;j = 0;
        int[] r = new int[string_length];
        char[] re = new char[string_length];
        System.out.println(string_length);
        for(;i<string_length;i++){
            a = (a+1)%256;
            j = (j+box[a])%256;
            int x = box[a];
            box[a] = box[j];
            box[j] = x;
            int y = ((int)string.toCharArray()[i])^(box[(box[a]+box[j])%256]);
            r[i] = y;
            System.out.println(y);
            result += (char)y;
            re[i] = (char)y;
            System.out.println(re[i]);
        }
        System.out.println(result);
        System.out.println(String.valueOf(re));
        result = new BASE64Encoder().encode(result.getBytes("utf-8"));
        result.replace("=", "");
        return result;
    }

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        System.out.println(enc("passw0rd"));
    }

}

我自己测了测是从ASCII转字符那开始两个程序不同的,求解决办法

  • 写回答

4条回答 默认 最新

  • chapple_cnm 2017-10-31 08:05
    关注
    // String enc(String string)这里的编码改一下
    result = new BASE64Encoder().encode(result.getBytes("ISO8859-1")); 
    

    Python IDLE的默认编码一般是None,即ASCII编码,对应Java里的“ISO8859-1"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 深度学习运行代码直接中断
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥20 需要完整的共散射点成像代码
  • ¥15 编写vba代码实现数据录入工作
  • ¥15 做过TCL海信电视小米电视相关影视会员软件私我
  • ¥15 Mapreduce是正常的,在运行其他jar包时并没有任何问题,只是在做LogCount.jar 时出的问题。如图所示
  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢