LINK STATE PROTOCOL
import [Link].*; public class link { public static void main(String[] args) { try { int cost[][]=new int[20][20]; int dist[][]=new int[20][20]; int n,i,j,k,m,count=0; [Link]("LINK STATE"); [Link]("Enter the number of nodes:"); DataInputStream d=new DataInputStream([Link]); n=[Link]([Link]()); for(i=0;i<n;i++) { for(j=0;j<n;j++) { [Link]("Enter the cost from:"+(i+1)+"to"+(j+1)); cost[i][j]=[Link]([Link]()); cost[i][i]=0; cost[i][j]=cost[i][j]; dist[i][j]=j; } } [Link]("Number of nodes:"+n); [Link]("The cost matrix is:"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { [Link](cost[i][j]+"\t"); } [Link]("\n"); } [Link]("Enter the source router: "); m=[Link]([Link]()); do { count=0; for(j=0;j<n;j++) { for(k=0;k<n;k++) if(cost[m][j]>cost[m][k]+cost[k][j]) { cost[m][j]=cost[m][k]+cost[k][j]; dist[m][j]=k;
count++; } } }while(count!=0); for(j=0;j<n;j++) { [Link]("Distance from "+(m+1)+" to "+(j+1)+" is through "+(dist[m][j]+1)+" and the cost is "+(cost[m][j])); } } catch(Exception e) {} } }
SIMPLE DES ALGORITHM
class SDES { public int K1, K2;
public static final int P10[] = { 3, 5, 2, 7, 4, 10, 1, 9, 8, 6}; public static final int P10max = 10;
public static final int P8[] = { 6, 3, 7, 4, 8, 5, 10, 9}; public static final int P8max = 10;
public static final int P4[] = { 2, 4, 3, 1}; public static final int P4max = 4;
public static final int IP[] = { 2, 6, 3, 1, 4, 8, 5, 7}; public static final int IPmax = 8;
public static final int IPI[] = { 4, 1, 3, 5, 7, 2, 8, 6}; public static final int IPImax = 8;
public static final int EP[] = { 4, 1, 2, 3, 2, 3, 4, 1}; public static final int EPmax = 4;
public static final int S0[][] = { { 1, 0, 3, 2}, { 3, 2, 1, 0}, { 0, 2, 1, 3}, { 3, 1, 3, 2} };
public static final int S1[][] = { { 0, 1, 2, 3}, { 2, 0, 1, 3}, { 3, 0, 1, 2}, { 2, 1, 0, 3} };
public static int permute( int x, int p[], int pmax) { int y = 0;
for( int i = 0; i < [Link]; ++i) { y <<= 1;
y |= (x >> (pmax - p[i])) & 1; }
return y; }
public static int F( int R, int K) { int t = permute( R, EP, EPmax) ^ K; int t0 = (t >> 4) & 0xF; int t1 = t & 0xF;
t0 = S0[ ((t0 & 0x8) >> 2) | (t0 & 1) ][ (t0 >> 1) & 0x3 ]; t1 = S1[ ((t1 & 0x8) >> 2) | (t1 & 1) ][ (t1 >> 1) & 0x3 ];
t = permute( (t0 << 2) | t1, P4, P4max);
return t; }
public static int fK( int m, int K) { int L = (m >> 4) & 0xF; int R = m & 0xF;
return ((L ^ F(R,K)) << 4) | R;
public static int SW( int x) { return ((x & 0xF) << 4) | ((x >> 4) & 0xF); }
public byte encrypt( int m) { m = permute( m, IP, IPmax); m = fK( m, K1); m = SW( m); m = fK( m, K2); m = permute( m, IPI, IPImax);
return (byte) m; }
public byte decrypt( int m) { m = permute( m, IP, IPmax); m = fK( m, K2); m = SW( m); m = fK( m, K1); m = permute( m, IPI, IPImax);
return (byte) m; }
public static void printb( int x, int n) { int mask = 1 << (n-1);
while( mask > 0) { [Link]( ((x & mask) == 0) ? '0' : '1'); mask >>= 1; } }
public SDES( int K) { K = permute( K, P10, P10max);
int t1 = (K >> 5) & 0x1F; int t2 = K & 0x1F;
t1 = ((t1 & 0xF) << 1) | ((t1 & 0x10) >> 4); t2 = ((t2 & 0xF) << 1) | ((t2 & 0x10) >> 4);
K1 = permute( (t1 << 5) | t2, P8, P8max);
t1 = ((t1 & 0x7) << 2) | ((t1 & 0x18) >> 3);
t2 = ((t2 & 0x7) << 2) | ((t2 & 0x18) >> 3);
K2 = permute( (t1 << 5) | t2, P8, P8max); }
public class SKT_SDES { public static void main( String args[]) throws Exception {
if( [Link] != 2) { [Link]( "Usage: SKT_SDES key(10 bit) plaintext(8 bit)"); [Link](1); }
int K = [Link]( args[0], 2); SDES A = new SDES( K); int m = [Link]( args[1], 2);
[Link]("Key K1: "); [Link]( A.K1, 8); [Link]("\nKey K2: "); [Link]( A.K2, 8); m = [Link]( m);
[Link]("\nEncrypted Message: "); [Link]( m, 8); m = [Link]( m); [Link]("\nDecrypted Message: "); [Link]( m, 8); } }
DES ALGORITHM
import [Link].*; import [Link].*; import [Link].*; class DES { public static void main(String[] a) { if ([Link]<2) { [Link]("Insufficient Arguments....There should 2 arguments"); return; } String test = a[0]; String algorithm = a[1]; try { byte[] theKey = null; byte[] theMsg = null; byte[] theExp = null; if ([Link]("1")) { theKey = hexToBytes("133457799BBCDFF1"); theMsg = hexToBytes("0123456789ABCDEF"); theExp = hexToBytes("85E813540F0AB405"); } else if ([Link]("2")) { theKey = hexToBytes("38627974656B6579"); // "8bytekey" theMsg = hexToBytes("6D6573736167652E"); // "message." theExp = hexToBytes("7CF45E129445D451"); } else if ([Link]("3")) { theKey = hexToBytes("133457799BBCDFF1"); // = test 1 theMsg = hexToBytes("0808080808080808"); // PKCS5Padding theExp = hexToBytes("FDF2E174492922F8"); } else { [Link]("Wrong option.1,2,3 are the valid arguments....");
return; } KeySpec ks = new DESKeySpec(theKey); SecretKeyFactory kf = [Link]("DES"); SecretKey ky = [Link](ks); Cipher cf = [Link](algorithm); [Link](Cipher.ENCRYPT_MODE,ky); byte[] theCph = [Link](theMsg); [Link]("Key : "+bytesToHex(theKey)); [Link]("Message : "+bytesToHex(theMsg)); [Link]("Cipher : "+bytesToHex(theCph)); [Link]("Expected: "+bytesToHex(theExp)); } catch (Exception e) { [Link](); return; } } public static byte[] hexToBytes(String str) { if (str==null) { return null; } else if ([Link]() < 2) { return null; } else { int len = [Link]() / 2; byte[] buffer = new byte[len]; for (int i=0; i<len; i++) { buffer[i] = (byte) [Link]( [Link](i*2,i*2+2),16); } return buffer; } } public static String bytesToHex(byte[] data) { if (data==null) { return null; } else { int len = [Link]; String str = ""; for (int i=0; i<len; i++) { if ((data[i]&0xFF)<16) str = str + "0" + [Link](data[i]&0xFF); else str = str + [Link](data[i]&0xFF); } return [Link](); } } }
RSA ALGORITHM
import [Link].*; import [Link].*; class RSA { public static void main(String arg[]) throws Exception { long fi; long N; long p,q; long i,e=2; long d=1; DataInputStream din=new DataInputStream([Link]); [Link]("Enter the value two Prime Numbers p and q: "); p=[Link]([Link]()); q=[Link]([Link]()); N=p*q; int c=-1; fi=((p-1)*(q-1)); //[Link]("fi is "+fi); for(i=2;i<(fi);i++) { if((((fi%i)==0)&&(e%i==0)&&(i!=e))||(fi%e==0)) { e++; i=2; } else { for(d=2;d<N;d++) { if(d*e==(fi+1)) { c=0; break; } } if(c==-1) { e++; i=2; } } } [Link]("Public Key KU:{"+e+","+N+"}"); [Link]("Secret Private Key KR:{"+d+","+p+","+q+"}"); [Link]("Enter the message :"); long msg,prod=1,res; msg=[Link]([Link]());
if(msg>=N) [Link]("Enter a lesser value : "); else { for(i=0;i<e;i++) { prod*=msg; } res=prod%N; [Link]("Encrypted message is "+res); prod=1; for(i=0;i<d;i++) { prod*=res; }
res=prod%N; [Link]("Decrypted message is "+msg); } } }