如何将大字符串转换为十六进制然后转换为字节? [英] How do I convert a large string into hex and then into byte?

查看:210
本文介绍了如何将大字符串转换为十六进制然后转换为字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用手机工作,每天处理MEID号码。因此,我没有在网上搜索一个MEID(长度为14的十六进制数)到伪ESN(一个长度为8的十六进制数)计算器,我想我可以制作自己的程序。从MEID获取pESN的方法理论上相当简单。例如,给定MEID 0xA0000000002329,为了生成pESN,需要将SHA-1应用于MEID。 A0000000002329上的SHA-1给出了e3be267a2cd5c861f3c7ea4224df829a3551f1ab。取这个结果的最后6个十六进制数字,并将其附加到0x80 - 结果是0x8051F1AB。



现在这里是我到目前为止的代码:

  public void sha1()throws NoSuchAlgorithmException {

String hexMEID =A0000000002329;

MessageDigest mDigest = MessageDigest.getInstance(SHA1);

byte [] result = mDigest.digest(hexMEID.getBytes());
StringBuilder sb = new StringBuilder(); (int i = 0; i< result.length; i ++){
sb.append(Integer.toString((result [i]& 0xff)+ 0x100,16 ).substring(1));
}

System.out.println(sb.toString());





$ b

问题是使用这种方法,A0000000002329上的SHA-1给出了a89b611b421f57705bd013297ce3fc835f706ab0的e3be267a2cd5c861f3c7ea4224df829a3551f1ab。我在这里做错了什么?



有人给了我一个暗示,的技巧是将SHA-1应用于表示MEID的数字,而不是字符串表示MEID,你需要逐字节地处理它,所以你必须一次给它两个十六进制数字(因为两个十六进制数字是一个字节)并且确保它们被解释为数字而不是ASCII字符。如果这是真的,那么我该如何将字符串更改为十六进制,然后将其转换为字节,以便SHA1可以给我正确的结果???

解决方案

您可以使用以下两种方法:

  public static synchronized String bytesToHex(byte [] buf){
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i; (((int)buf [i]& 0xff)< 0x10){
strbuf.append($(i = 0; i< buf.length; ( 0);

strbuf.append(Long.toString((int)buf [i]& 0xff,16));
}
return strbuf.toString();


public synchronized static byte [] hexToBytes(String hexString){
byte [] b = new BigInteger(hexString,16).toByteArray();
return b;
}


I work with cellphones and deal with MEID numbers on a daily basis. So instead of searching online for a MEID (a hex number of length 14) to pseudo ESN (a hex number of length 8) calculator, I figured I can make my own program. The way to obtain a pESN from MEID is fairly simple in theory. For example, given MEID 0xA0000000002329, to make a pESN, SHA-1 needs to be applied to the MEID. SHA-1 on A0000000002329 gives e3be267a2cd5c861f3c7ea4224df829a3551f1ab. Take the last 6 hex numbers of this result, and append it to 0x80 - the result is 0x8051F1AB.

Now here is the code I have so far:

public void sha1() throws NoSuchAlgorithmException {

    String hexMEID = "A0000000002329";

    MessageDigest mDigest = MessageDigest.getInstance("SHA1");      

    byte[] result = mDigest.digest(hexMEID.getBytes());
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < result.length; i++) {
        sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
    }

    System.out.println(sb.toString());
}  

The problem is that using this method, SHA-1 on A0000000002329 gives a89b611b421f57705bd013297ce3fc835f706ab0 instead of e3be267a2cd5c861f3c7ea4224df829a3551f1ab. What am I doing wrong here??

Someone gave me a hint that "the trick is to apply SHA-1 to the number representing the MEID, not the string representing the MEID. You'll need to process it byte-by-byte, so you must give it two hex numbers at a time (since two hex numbers make a byte) and make sure they are interpreted as numbers and not ASCII characters". If this is true then how do I change my string into hex and then into byte so that SHA1 can give me the correct result???

解决方案

You can use the following two methods

    public static synchronized String bytesToHex(byte [] buf){
        StringBuffer strbuf = new StringBuffer(buf.length * 2);
        int i;
        for (i = 0; i < buf.length; i++) {
            if (((int) buf[i] & 0xff) < 0x10){
                strbuf.append("0");
            }
            strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
        }
        return strbuf.toString();
    }

    public synchronized static byte[] hexToBytes(String hexString) {
         byte[] b = new BigInteger(hexString,16).toByteArray();     
         return b;
    }

这篇关于如何将大字符串转换为十六进制然后转换为字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆