我尝试使用convertHextoString代码将我的HexString更改为字符串并像你一样放入函数,但转换后的String来自convertHextoString似乎有许多特殊字符,例如" \ n"或":"或"%"等等,我认为这就是我输出错误的原因。这是我的convertHextoString函数
public String convertHexToString(String hex){
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
//49204c6f7665204a617661 split into two characters 49, 20, 4c...
for( int i=0; i<hex.length()-1; i+=2 ){
//grab the hex in pairs
String output = hex.substring(i, (i + 2));
//convert hex to decimal
int decimal = Integer.parseInt(output, 16);
//convert the decimal to character
sb.append((char)decimal);
temp.append(decimal);
}
System.out.println("Decimal : " + temp.toString());
return sb.toString();
}
和HMAC的功能:
static String hash_hmac(String type, String value, String key) {
try { Log.i("Hien - value - hmac",value);
javax.crypto.Mac mac = javax.crypto.Mac.getInstance(type);
javax.crypto.spec.SecretKeySpec secret = new javax.crypto.spec.SecretKeySpec(key.getBytes(), type);
mac.init(secret);
byte[] digest = mac.doFinal(value.getBytes());
StringBuilder sb = new StringBuilder(digest.length*2);
String s;
for (byte b : digest){
s = Integer.toHexString(b & 0xff);
if(s.length() == 1) sb.append('0');
sb.append(s);
}
return sb.toString();} catch (Exception e) { android.util.Log.v("TAG","Exception ["+e.getMessage()+"]", e); } return ""; }
请帮忙!