1 import javax.crypto.Mac; 2 import javax.crypto.spec.SecretKeySpec; 3 import java.util.logging.Level ; 4 import sync4j.framework.notification.NotificationException; 5 6 public class TestMac { 7 8 9 public static void main(String [] args) throws Exception { 10 TestMac testmac = new TestMac(); 11 12 String hex = "0200006A1D2D2F2F53594E434D4C2F2F4454442053796E634D4C20312E312F2F454E6D6C7103312E3100017203444D2F312E31000165033000015B033000016E5703494D45493A303030303030303131323334353634000101675703687474703A2F2F36352E3234312E35352E3130303A383038302F73796E63346A2F73796E63000101016B454B03310001546E57032E2F53796E634D4C2F444D4163632F313130390001015A000147036E6F6465000101010000546E57032E2F53796E634D4C2F444D4163632F313130392F416464720001014F03687474703A2F2F36352E3234312E35352E3130302F73796E63346A2F73796E63000101546E57032E2F53796E634D4C2F444D4163632F313130392F41646472547970650001014F0331000101546E57032E2F53796E634D4C2F444D4163632F313130392F506F72744E62720001014F0338303830000101546E57032E2F53796E634D4C2F444D4163632F313130392F436F6E5265660001010F01546E57032E2F53796E634D4C2F444D4163632F313130392F53657276657249640001014F0373796E63346A000101546E57032E2F53796E634D4C2F444D4163632F313130392F53657276657250570001014F034933303150436432527945746354412F4A6B6C6F57513D3D000101546E57032E2F53796E634D4C2F444D4163632F313130392F5365727665724E6F6E63650001015A0001470362696E00010100004FC310294A57252775592F762B5221465F72640101546E57032E2F53796E634D4C2F444D4163632F313130392F557365724E616D650001014F0331313039000101546E57032E2F53796E634D4C2F444D4163632F313130392F436C69656E7450570001014F03666F7461000101546E57032E2F53796E634D4C2F444D4163632F313130392F436C69656E744E6F6E63650001015A0001470362696E00010100004FC3106C5F40517727444B495B332E7E7057420101546E57032E2F53796E634D4C2F444D4163632F313130392F41757468507265660001014F0373796E636D6C3A617574682D4D4143000101546E57032E2F53796E634D4C2F444D4163632F313130392F4E616D650001014F033131303900010101120101"; 14 16 byte[] data = convertHexString(hex); 17 System.out.println("Hex length: " + hex.length()); 18 System.out.println("Hex: " + hex); 19 20 if (!hex.equals(bytesToHex(data))) { 21 System.out.println("Error converting the string in bytes array"); 22 } 23 24 String imsi = "310260801674539"; 26 27 String semioctet = "3901628010765493"; 29 30 byte[] mac = computeHmacSha1(semioctet.getBytes(), data); 31 32 System.out.println("Your expected Mac using semi-octet as string: " + bytesToHex(mac)); 34 35 36 byte[] key = getKeyFromIMSI(imsi); 37 38 mac = computeHmacSha1(key, data); 39 40 System.out.println("Our Mac using semi-octet as byte[]: " + bytesToHex(mac)); 42 } 43 44 45 private static byte[] convertHexString(String hex) { 46 int length = hex.length(); 47 byte[] bHex = new byte[length/2]; 48 String temp = null; 49 int t = 0; 50 for (int i=0; i<length; i++) { 51 temp = "" + hex.charAt(i) + hex.charAt(++i); 52 bHex[t++] = (byte)Integer.parseInt(temp, 16); 53 } 54 return bHex; 55 } 56 57 58 65 public static byte[] computeHmacSha1(byte[] bKey, byte[] message) throws java.security. 66 GeneralSecurityException { 67 68 String algorithm = "HmacSha1"; 69 byte[] digest = null; 70 71 SecretKeySpec key = new SecretKeySpec(bKey, algorithm); 73 74 Mac mac = Mac.getInstance(algorithm); 76 mac.init(key); 77 78 digest = mac.doFinal(message); 79 80 return digest; 81 } 82 83 public static String bytesToHex(byte[] b) { 84 StringBuffer buf = new StringBuffer (""); 85 for (int i = 0; i < b.length; i++) 86 buf.append(byteToHex(b[i])); 87 return buf.toString(); 88 } 89 90 public static String byteToHex(byte b) { 91 char hexDigit[] = { 93 '0', '1', '2', '3', '4', '5', '6', '7', 94 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 95 }; 96 char[] array = {hexDigit[ (b >> 4) & 0x0f], hexDigit[b & 0x0f]}; 97 return new String (array); 98 } 99 100 101 111 private static byte[] getKeyFromIMSI(String imsi) throws NotificationException { 112 113 imsi = imsi.trim(); 114 115 if ( (imsi.length() % 2) == 1 ) { 116 imsi = "9" + imsi; 117 } else { 118 imsi = "1" + imsi; 119 imsi = imsi + "F"; 120 121 } 122 123 int numDigit = imsi.length(); 124 String temp = null; 125 char c1 = 0; 126 char c2 = 0; 127 byte b = 0; 128 byte[] key = new byte[numDigit / 2]; int t = 0; 130 for (int i = 0; i < numDigit; i++) { 131 c1 = imsi.charAt(i); 132 c2 = imsi.charAt(++i); 133 temp = "" + c2 + c1; 134 try { 135 key[t] = (byte) (Integer.parseInt(temp, 16)); 136 } catch (NumberFormatException ex) { 137 throw new NotificationException("IMSI isn't valid (only numbers are permitted)"); 138 } 139 t++; 140 } 141 142 143 return key; 144 } 145 } 146 | Popular Tags |