KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestMac


1 import javax.crypto.Mac;
2 import javax.crypto.spec.SecretKeySpec;
3 import java.util.logging.Level JavaDoc;
4 import sync4j.framework.notification.NotificationException;
5
6 public class TestMac {
7
8
9     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
10         TestMac testmac = new TestMac();
11
12         // your data
13
String JavaDoc hex = "0200006A1D2D2F2F53594E434D4C2F2F4454442053796E634D4C20312E312F2F454E6D6C7103312E3100017203444D2F312E31000165033000015B033000016E5703494D45493A303030303030303131323334353634000101675703687474703A2F2F36352E3234312E35352E3130303A383038302F73796E63346A2F73796E63000101016B454B03310001546E57032E2F53796E634D4C2F444D4163632F313130390001015A000147036E6F6465000101010000546E57032E2F53796E634D4C2F444D4163632F313130392F416464720001014F03687474703A2F2F36352E3234312E35352E3130302F73796E63346A2F73796E63000101546E57032E2F53796E634D4C2F444D4163632F313130392F41646472547970650001014F0331000101546E57032E2F53796E634D4C2F444D4163632F313130392F506F72744E62720001014F0338303830000101546E57032E2F53796E634D4C2F444D4163632F313130392F436F6E5265660001010F01546E57032E2F53796E634D4C2F444D4163632F313130392F53657276657249640001014F0373796E63346A000101546E57032E2F53796E634D4C2F444D4163632F313130392F53657276657250570001014F034933303150436432527945746354412F4A6B6C6F57513D3D000101546E57032E2F53796E634D4C2F444D4163632F313130392F5365727665724E6F6E63650001015A0001470362696E00010100004FC310294A57252775592F762B5221465F72640101546E57032E2F53796E634D4C2F444D4163632F313130392F557365724E616D650001014F0331313039000101546E57032E2F53796E634D4C2F444D4163632F313130392F436C69656E7450570001014F03666F7461000101546E57032E2F53796E634D4C2F444D4163632F313130392F436C69656E744E6F6E63650001015A0001470362696E00010100004FC3106C5F40517727444B495B332E7E7057420101546E57032E2F53796E634D4C2F444D4163632F313130392F41757468507265660001014F0373796E636D6C3A617574682D4D4143000101546E57032E2F53796E634D4C2F444D4163632F313130392F4E616D650001014F033131303900010101120101";
14         // String hex = "0200006A1D2D2F2F53594E434D4C2F2F4454442053796E634D4C20312E312F2F454E6D6C7103312E3100017203444D2F312E31000165033000015B033000016E5703494D45493A303030303030303131323334353634000101675703687474703A2F2F36352E3234312E35352E3130303A383038302F73796E63346A2F73796E63000101016B454B03310001546E57032E2F53796E634D4C2F444D4163632F313130390001015A000147036E6F6465000101010000546E57032E2F53796E634D4C2F444D4163632F313130392F416464720001014F03687474703A2F2F36352E3234312E35352E3130302F73796E63346A2F73796E63000101546E57032E2F53796E634D4C2F444D4163632F313130392F41646472547970650001014F0331000101546E57032E2F53796E634D4C2F444D4163632F313130392F506F72744E62720001014F0338303830000101546E57032E2F53796E634D4C2F444D4163632F313130392F436F6E5265660001010F01546E57032E2F53796E634D4C2F444D4163";
15

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         // the imsi
25
String JavaDoc imsi = "310260801674539";
26
27         // your used semi-octet
28
String JavaDoc semioctet = "3901628010765493";
29
30         byte[] mac = computeHmacSha1(semioctet.getBytes(), data);
31
32         // your expected mac
33
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         // our mac
41
System.out.println("Our Mac using semi-octet as byte[]: " + bytesToHex(mac));
42     }
43
44
45     private static byte[] convertHexString(String JavaDoc hex) {
46         int length = hex.length();
47         byte[] bHex = new byte[length/2];
48         String JavaDoc 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     /**
59      * Compute digest using HMAC-Sha1 keyed-hashing algorithm; see RFC 2104
60      * @param bKey the key
61      * @param message the message
62      * @throws GeneralSecurityException
63      * @return byte[] the mac
64      */

65     public static byte[] computeHmacSha1(byte[] bKey, byte[] message) throws java.security.
66
JavaDoc        GeneralSecurityException {
67
68         String JavaDoc algorithm = "HmacSha1";
69         byte[] digest = null;
70
71         // Generate a key for the HMAC-MD5 keyed-hashing algorithm; see RFC 2104
72
SecretKeySpec key = new SecretKeySpec(bKey, algorithm);
73
74         // Create a MAC object using HMAC-MD5 and initialize with key
75
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 JavaDoc bytesToHex(byte[] b) {
84         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("");
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 JavaDoc byteToHex(byte b) {
91         // Returns hex String representation of byte b
92
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 JavaDoc(array);
98     }
99
100
101     /**
102      * Computes the bytes array to use as key in the mac calculation
103      * based on imsi. The IMSI MUST be on semi-octet representation as defined
104      * in Digital cellular Telecommunications system (Phase 2+); Specification of the Subscriber
105      * Identity Module - Mobile Equipment (SIM - ME) interface
106      * (GSM 11.11 version 7.2.0 Release 1998)
107      *
108      * @param imsi String
109      * @return byte[]
110      */

111     private static byte[] getKeyFromIMSI(String JavaDoc 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 JavaDoc temp = null;
125         char c1 = 0;
126         char c2 = 0;
127         byte b = 0;
128         byte[] key = new byte[numDigit / 2]; // always even
129
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 JavaDoc 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