Implémentation gsm

masterv Messages postés 1 Date d'inscription samedi 30 janvier 2010 Statut Membre Dernière intervention 21 juillet 2010 - 21 juil. 2010 à 12:30
bestabibi Messages postés 5 Date d'inscription samedi 25 avril 2009 Statut Membre Dernière intervention 28 juin 2012 - 28 juin 2012 à 13:52
slt c vianney je veux implémeter l'algorithme de A3,A5 et A8 en Java pour mobile avec le Netbeans mais je ne sais comment m'y prendre je suis débutant en Java si vous pouvez m'aider avec les codes sources et la manipulation de l'outil

1 réponse

bestabibi Messages postés 5 Date d'inscription samedi 25 avril 2009 Statut Membre Dernière intervention 28 juin 2012
28 juin 2012 à 13:52
voici le code source de 3 algorithmes en langage java
APPENDIX A
Java Implementation of A3/A8 Algorithm
public class A3A8 { // use for Authentication
byte tab[][] = new byte[5][512];
public A3A8 (byte rand[], byte key[], byte simoutput[]) {
byte[] table_0 = { 102,-79,-70,-94, 2,-100,112, 75, 55, 25, 8, 12,-5,-63,-0, -8,109,-43,-105, 53, 42, 79,-65,115,-23,-14,-92,-33,-47,-108,108,-95, -4, 37, -2, 47, 64,-45, 6,-19,- 1, -96,-117,113, 76,-118, 59, 70, 67, 26, 13,-99, 63,-7, -35, 30,-42, 36,-90, 69, -04,124,-49,116, -9,-62, 41, 84, 71, 1, 49, 14, 95, 35, -87 , 21, 96, 78,-41,-31, -74, -13, 28, 92,-55,118, 4, 74,-8,-128, 17, 11,-110, -24,-11, 48, -107, 90,120, 39, 87,-26,106, -24,-81, 19,126,-66,-54,-115,-119, -80, -6, 27,101, 40,-37,-29, 58, 20, 51,-78, 98,-40,-116, 22, 32,121, 61,103, -3, 72, 29,110, 85,-44,-76,-52,-106,-73, 15, 66,-84,-60, 56,-59,-98, 0,100, 45,-103, 7,-112,-34,-93,-89, 60,-121,-46,-25,-82,-91, 38,-7,-32, 34,-36,-27, -39,-48,-15, 68,-50,-67,125,-1,-17, 54,-88, 9,123,122, 73,-111,117,-22,-113, 99,-127,-56,-64, 82,104,-86,-120,-21, 93, 81,-51,-83,-20, 94,105, 52, 46,-28,-58, 5,57,-2, 97,-101,-114,-123,-57,-85,-69, 50, 65,-75,127,107,-109,-30,-72,-38,-125, 33, 77, 86, 31, 44, 88, 62,-18, 18, 24, 43, -102, 23, 80,-97,-122,111, 9,114, 3, 91, 16,-126, 83, 10,-61,-16,-3,119, -79,102, -94,-70, -100, 2, 75,112, 25, 55, 12, 8,-63,-5,-68,-10, -43,109, 53,-105,79,42,115,-65,-14,-23,-33,-92,-108,-47,-95,108,37,-4,47,-12,-45,64,-9,6,-96, 71,113,-117,-118, 76, 70, 59, 26, 67,-99, 13,-77, 63, 30,-35, 36,-42, 69,-90,124, -104,116,-49, -62,-9, 84, 41, 1, 71, 14, 49, 35, 95, 21,-87, 78, 96,-31,-41, -13,-74, 92, 28,118,-55, 74, 4,-128,-8, 11, 17,-124,-110, 48,-11, 90,-107, 39,120,-26, 87, -24,106, 19,-81,-66,126,-115,-54,-80,-119, 27,-6, 40,101, -29,-37, 20, 58,-78, 51, -40, 98, 22,-116,121, 32, 103, 61, 72,-53,110, 29,-44, 85,-52,-76,-73,-106, 66, 15,-60,-84, -59, 56, 0,-98, 45,100, 7,-103,-34, -112,-89,-93,-121, 60,-25,-46, -91, -82,-7, 38, 34,-32,-27,-36,-48,-39, 68, -15,-67,-50,-1,125, 54,-17, 89,-88,122,123,-111, 73,-22,117, 99,-113,-56, -127, 82,-64, -86,104,-21,-120, 81, 93,-83,-51, 94, -20, 52,105,-28, 46, 5,-58, -2, 57,-101, 97,-123,-114,-85,-57, 50,-69,-75, 65,107,127,-30,-109, -38, -72, 33,-125, 86, 77, 44, 31, 62, 88, 18,-18, 43, 24, 23, -102, -97, 80,111, -122,114, 9, 91, 3,-126, 16, 10, 83,-16,-61,119,-3};
byte table_1[] = { 19, 11, 80,114, 43, 1, 69, 94, 39, 18,127,117, 97, 3, 85, 43, 27,124, 70, 83, 47, 71, 63, 10, 47, 89, 79, 4, 14, 59, 11, 5, 35,107,103, 68, 21, 86, 36, 91, 85,126, 32, 50,109, 94,120, 6, 53, 79, 28, 45, 99, 95, 41, 34, 88, 68, 93, 55,110,125,105, 20, 90, 80, 76, 96, 23, 60, 89, 64,121, 56, 14, 74,101, 8, 19, 78, 76, 66,104, 46,111, 50, 32, 3, 39, 0, 58, 25, 92, 22, 18, 51, 57, 65,119,116, 22,109, 7, 86, 59, 93, 62,110, 78, 99, 77, 67, 12,113, 87, 98,102, 5, 88, 33, 38, 56, 23, 8, 75, 45, 13, 75, 95, 63, 28, 49,123,120, 20,112, 44, 30, 15, 98,106, 2,103, 29, 82,107, 42,124, 24, 30, 41, 16,108,100,117, 40, 73, 40, 7,114, 82,115, 36,112, 12,102,100, 84, 92, 48, 72, 97, 9, 54, 55, 74, 113,123, 17, 26, 53, 58, 4, 9, 69,122, 21,118, 42, 60, 27, 73, 118,125, 34, 15, 65,115, 84, 64, 62, 81, 70, 1, 24,111,121, 83, 104, 81, 49,127, 48,105, 31, 10, 6, 91, 87, 37, 16, 54,116,126, 31, 38, 13, 0, 72,106, 77, 61, 26, 67, 46, 29, 96, 37, 61, 52, 101, 17, 44,108, 71, 52, 66, 57, 33, 51, 25, 90, 2,119,122, 35};
byte table_2[] = { 52, 50, 44, 6, 21, 49, 41, 59, 39, 51, 25, 32, 51, 47, 52, 43, 37, 4, 40, 34, 61, 12, 28, 4, 58, 23, 8, 15, 12, 22, 9, 18, 55, 10, 33, 35, 50, 1, 43, 3, 57, 13, 62, 14, 7, 42, 44, 59, 62, 57, 27, 6, 8, 31, 26, 54, 41, 22, 45, 20, 39, 3, 16, 56, 48, 2, 21, 28, 36, 42, 60, 33, 34, 18, 0, 11, 24, 10, 17, 61, 29, 14, 45, 26, 55, 46, 11, 17, 54, 46, 9, 24, 30, 60, 32, 0, 20, 38, 2, 30, 58, 35, 1, 16, 56, 40, 23, 48, 13, 19, 19, 27, 31, 53, 47, 38, 63, 15, 49, 5, 37, 53, 25, 36, 63, 29, 5, 7};
byte table_3 [] = { 1, 5, 29, 6, 25, 1, 18, 23, 17, 19, 0, 9, 24, 25, 6, 31, 28, 20, 24, 30, 4, 27, 3, 13, 15, 16, 14, 18, 4, 3, 8, 9, 20, 0, 12, 26, 21, 8, 28, 2, 29, 2, 15, 7, 11, 22, 14, 10, 17, 21, 12, 30, 26, 27, 16, 31, 11, 7, 13, 23, 10, 5, 22, 19};
byte table_4 [] = { 15, 12, 10, 4, 1, 14, 11, 7, 5, 0, 14, 7, 1, 2, 13, 8, 10, 3, 4, 9, 6, 0, 3, 2, 5, 6, 8, 9, 11, 13, 15, 12};
byte table[][] = new byte[5][512];
table [0] = table_0; table[1] = table_1; table[2] = table_2;
table [3] = table_3; table[4] = table_4;
tab = table;
byte x[], bit[];
x = new byte[1024];
bit = new byte[128];
int i, j, k, l, m, n, y, z,e,d, next_bit;
/* ( Load RAND into last 16 bytes of input ) */
for (i=16; i<32; i++)
x[i] = rand[i-16];
/* ( Loop eight times ) */
for (i=1; i<9; i++) {
/* ( Load key into first 16 bytes of input ) */
for (j=0; j<16; j++)
x[j] = key[j];
/* ( Perform substitutions ) */
for (j=0; j<5; j++){
for (k=0; k<(1<<j); k++){
for (l=0; l<(1<<(4-j)); l++) {
m = l + k*(1<<(5-j));
n = m + (1<<(4-j));
e x[m]; d x[n];
if (x[m] < 0) e = x[m]+256;
if (x[n] < 0) d = x[n]+256;
y = (e+2*d) % (1<<(9-j));
z = (2*e+d) % (1<<(9-j));
x[m] = table[j][y];
x[n] = table[j][z];
} }}
/* (Form bits from bytes ) */
for (j=0; j<32; j++)
for (k=0; k<4; k++)
bit[4*j+k] =(byte) ((x[j]>>(3-k)) & 1);
/* (Permutation but not on the last loop) */
if (i < 8)
for (j=0; j<16; j++) {
x[j+16] = 0;
for (k=0; k<8; k++) {
next_bit = ((8*j + k)*17) % 128;
x[j+16] |= bit[next_bit] << (7-k);
} } }
for (i=0; i<4; i++)
simoutput[i] = (byte) ((x[2*i]<<4) | x[2*i+1]);
for (i=0; i<6; i++)
simoutput[4+i] =(byte) ( (x[2*i+18]<<6) | (x[2*i+18+1]<<2)| (x[2*i+18+2]>>2)) ;
simoutput[4+6] = (byte) ((x[2*6+18]<<6) | (x[2*6+18+1]<<2));
simoutput[4+7] = 0;
}
public static int hextoint(char x){
if (x >= 'A' && x <= 'F')
return x-'A'+10;
else if (x >= '0' && x <= '9')
return x-'0';
System.out.println("bad input.\n");
return 0;
}}





















APPENDIX B
Java Implementation of A5 Algorithm
public class A5 { // use for Encryption
public static int R1,R2,R3;
/* Masks for the three shift registers */
static int R1MASK = 524287; /* 19 bits, numbered 0..18 */
static int R2MASK = 4194303; /* 22 bits, numbered 0..21 */
static int R3MASK = 8388607; /* 23 bits, numbered 0..22 */
/* Middle bit of each of the three shift registers, for clock control */
static int R1MID = 256; /* bit 8 */
static int R2MID = 1024; /* bit 10 */
static int R3MID = 1024; /* bit 10 */
/* Feedback taps, for clocking the shift registers.
* These correspond to the primitive polynomials
* x^19 + x^5 + x^2 + x + 1, x^22 + x + 1,
* and x^23 + x^15 + x^2 + x + 1. */
static int R1TAPS = 466944; /* bits 18,17,16,13 */
static int R2TAPS = 3145728; /* bits 21,20 */
static int R3TAPS = 7340160; /* bits 22,21,20,7 */
/* Output taps, for output generation */
static int R1OUT = 262144; /* bit 18 (the high bit) */
static int R2OUT = 2097152; /* bit 21 (the high bit) */
static int R3OUT = 4194304; /* bit 22 (the high bit) */
public static int parity(int x) {
x ^= x>>>16;
x ^= x>>>8;
x ^= x>>>4;
x ^= x>>>2;
x ^= x>>>1;
x = x & 1;
return x ;
}
public static int clockone(int reg, int mask, int taps) {
int t = reg & taps;
reg = (reg << 1) & mask;
reg |= parity(t);
return reg;
}
public static boolean majority() {
int sum;
sum = parity(R1&R1MID) + parity(R2&R2MID) + parity(R3&R3MID);
if (sum >= 2)
return true;
else
return false;
}
public static void clock() {
boolean maj = majority();
if (((R1&R1MID)!=0) == maj)
R1 = clockone(R1, R1MASK, R1TAPS);
if (((R2&R2MID)!=0) == maj)
R2 = clockone(R2, R2MASK, R2TAPS);
if (((R3&R3MID)!=0) == maj)
R3 = clockone(R3, R3MASK, R3TAPS);
}
public static void clockallthree() {
R1 = clockone(R1, R1MASK, R1TAPS);
R2 = clockone(R2, R2MASK, R2TAPS);
R3 = clockone(R3, R3MASK, R3TAPS);
}
public static int getbit() {
return parity(R1&R1OUT)^parity(R2&R2OUT)^parity(R3&R3OUT);
}
public static void keysetup(byte key[], int frame) {
int i;
int keybit, framebit;
/* Zero out the shift registers. */
R1 R2 R3 = 0;
/* Load the key into the shift registers,
* LSB of first byte of key array first,
* clocking each register once for every
* key bit loaded. (The usual clock
* control rule is temporarily disabled.) */
for (i=0; i<64; i++) {
clockallthree(); /* always clock */
keybit = (int)((key[i/8] >> (i&7))) & 1; /* The i-th bit of the key */
R1 ^= keybit; R2 ^= keybit; R3 ^= keybit;
}
/* Load the frame number into the shift
* registers, LSB first,
* clocking each register once for every
* key bit loaded. (The usual clock
* control rule is still disabled.) */
for (i=0; i<22; i++) {
clockallthree(); /* always clock */ framebit = (frame >> i) & 1; /* The i-th bit of the frame # */ R1 ^= framebit; R2 ^= framebit; R3 ^= framebit; }
/* Run the shift registers for 100 clocks
* to mix the keying material and frame number * together with output generation disabled,
* so that there is sufficient avalanche.
* We re-enable the majority-based clock control
* rule from now on. */ for (i=0; i<100; i++) { clock(); }
/* Now the key is properly set up. */
} public static void run(int AtoBkeystream[], int BtoAkeystream[]) { int i;
/* Zero out the output buffers. */ for (i=0; i<=113/32; i++) { AtoBkeystream[i] = 0; BtoAkeystream[i] = 0; }
/* Generate 114 bits of keystream for the
* A->B direction. Store it, MSB first. */ for (i=0; i<114; i++) { clock(); AtoBkeystream[i/32] |= getbit() << (31-(i&31));}
/* Generate 114 bits of keystream for the
* B->A direction. Store it, MSB first. */ for (i=0; i<114; i++) { clock(); BtoAkeystream[i/32] |= getbit() << (31-(i&31));
}}public A5 (byte[] key, int frame, int AtoBkeystream[], int BtoAkeystream[]) {
keysetup (key,frame); run (AtoBkeystream, BtoAkeystream); }}






















APPENDIX C
Network Server and Mobile Station Model
Network Server
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ns extends JFrame { //used for Network Server
private JTextArea display;
String globel;
public ns()
{
super( "Network Server" );
Container c = getContentPane();
display = new JTextArea(); //creating TextArea Pane
c.add( new JScrollPane( display ), BorderLayout.CENTER );
setSize( 500, 350 );
show();
} //added the GUI
// now adding funtionality
public void runServer() { ServerSocket server; Socket connection;
byte[] key = new byte[16];
byte[] randv = new byte[16];
byte[] simoutput = new byte[12];
byte [] Kc = new byte[8]; int frame; int[] MstoNs = new int[4];
int[] NstoMs = new int[4];
try {
String keys = "123456abc5624fd4826da58745783217";
keys = keys.toUpperCase();
for (int i=0; i<16; i++){
char h = keys.charAt(2*i);
char u = keys.charAt(2*i+1);
key[i] = (byte) ((hextoint(h)<<4)| hextoint(u)); }
String rand = "123456abc5624fd4826da58745783217";
rand = rand.toUpperCase();
for (int i=0; i<16; i++){
char h = rand.charAt(2*i);
char u = rand.charAt(2*i+1);
randv[i] = (byte) ((hextoint(h)<<4)| hextoint(u));
}
A3A8 obj = new A3A8 (randv, key, simoutput);
String res= ""+simoutput[0]+simoutput[1]+simoutput[2]+simoutput[3];
server = new ServerSocket( 5000, 100 ); // Creating a ServerSocket
display.setText( "Waiting for connection\n" );
connection = server.accept();
display.append( "\nMobile user being attenticated!");
display.append( "\nSend me your TMSI\n");
// Working with input and output streams.
BufferedReader inFromMobile = new BufferedReader(new InputStreamReader (connection.getInputStream()));
DataOutputStream outToMobile = new DataOutputStream (connection.getOutputStream(
String TMI = inFromMobile.readLine();
display.append( "\nTMI received :" + TMI);
display.append( "\nsenbding random number");
outToMobile.writeBytes (rand+"\n");
display.append( "Waiting for SRES\n");
String SRES = inFromMobile.readLine();
display.append( "SRES received\n");
// testing recievied response
if (SRES.equals(res)){
display.append("\nkeys are equal\n");
outToMobile.writeBytes ("\nYou are authenticated\n");
//using A5 to send some bits
String recvdata inFromMobile.readLine(); int data Integer.parseInt(recvdata);
display.append("data received= "+data+"\n"); for (int j=0; j<8; j++) {
Kc[j] = simoutput[j+4]; } frame = 308;
A5 stream new A5 (Kc,frame,MstoNs,NstoMs); int orignaldata data^MstoNs[0]; display.append("data recovered= "+orignaldata+"\n");
} else{ outToMobile.writeBytes ("terminate");connection.close(); } }
catch ( EOFException eof ) { System.out.println( "Client terminated connection" ); } catch ( IOException io ) { io.printStackTrace(); } }
public static int hextoint(char x){ if (x >= 'A' && x <= 'F') return x-'A'+10; else if (x >= '0' && x <= '9')return x-'0'; System.out.println("bad input.\n");
return 0; } public static void main( String args[] ) { String rand; ns app = new ns(); app.runServer(); }}
Mobile Station
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ms extends JFrame { //used for Mobile Station
private JTextArea display;
int data;
public ms()
{ super( "Mobile Station" ); Container c getContentPane(); display new JTextArea();
c.add( new JScrollPane( display ), BorderLayout.CENTER ); setSize( 500, 350 ); show();
} public void runClient() { Socket client; byte[] key = new byte[16]; byte[] randv = new byte[16];
byte[] simoutput = new byte[12]; boolean f = false; int frame; int[] MstoNs = new int[4];
int[] NstoMs = new int[4]; byte [] Kc = new byte[8]; try { String keys = "123456abc5624fd4826da58745783217";
keys = keys.toUpperCase();for (int i=0; i<16; i++){ char h = keys.charAt(2*i);
char u keys.charAt(2*i+1); key[i] (byte) ((hextoint(h)<<4)| hextoint(u)); }
// Creating a Socket to make connection. display.setText( "\nAttempting connection\n" ); client = new Socket(
InetAddress.getByName( "127.0.0.1" ), 5000 ); display.append( "\nIn process of Aunthentication to: " +
client.getInetAddress().getHostName() ); // Getting the input and output streams. DataOutputStream outToNetwork = new DataOutputStream (client.getOutputStream());
BufferedReader inFromNetwork = new BufferedReader (new InputStreamReader (client.getInputStream()));
display.append( "\nSending TMI\n" ); outToNetwork.writeBytes ("abc"+"\n");
String rand = inFromNetwork.readLine(); display.append( "\nRAND recieved\n" );
rand = rand.toUpperCase(); for (int i=0; i<16; i++){ char h = rand.charAt(2*i);
char u rand.charAt(2*i+1); randv[i] (byte) ((hextoint(h)<<4)| hextoint(u)); } A3A8 obj = new A3A8 (randv, key, simoutput);
String SRES = ""+simoutput[0]+simoutput[1]+simoutput[2]
+simoutput[3];
display.append( "\nSending SRES\n" );outToNetwork.writeBytes (SRES+"\n");
display.append( "\nSRES sent\n" );String responce = inFromNetwork.readLine();
if (responce.equals("terminate")){client.close(); display.append( "\nConnection could not be established\n" );}
else { f = true; display.append("\n" + responce) ; display.append("\nMobile is authenticated") ;
} if (f){ for (int j=0; j<8; j++) { Kc[j] = simoutput[j+4];
} frame 308; A5 stream new A5 (Kc,frame,MstoNs,NstoMs);
display.append("\nEnter the data to send, using keypad. \n");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String d new String(); int data Keyboard.readInt();
int senddata data^MstoNs[0]; display.append("\ndata "+ data+"\n"); display.append("\n data send= "+ senddata+ " \n"); outToNetwork.writeBytes (""+senddata+"\n"); }}
catch ( EOFException eof ) { System.out.println( "Server terminated connection" );
} catch ( IOException e ) { e.printStackTrace(); } }public static int hextoint(char x){
if (x >= 'A' && x <= 'F') return x-'A'+10; else if (x >= '0' && x <= '9')
return x-'0';System.out.println("bad input.\n"); return 0; }public static void main( String args[] ) { ms app = new ms(); app.runClient(); }}
0
Rejoignez-nous