0% found this document useful (0 votes)
75 views11 pages

Import Java

This document contains code for implementing three encryption algorithms: Link State Protocol, Simple Data Encryption Standard (SDES), and RSA. The Link State Protocol code calculates shortest paths between nodes in a network. The SDES code implements the basic steps of the SDES algorithm including key generation, encryption, and decryption. The RSA code performs encryption and decryption using the RSA public/private key algorithm.

Uploaded by

Yamuna Jayabalan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views11 pages

Import Java

This document contains code for implementing three encryption algorithms: Link State Protocol, Simple Data Encryption Standard (SDES), and RSA. The Link State Protocol code calculates shortest paths between nodes in a network. The SDES code implements the basic steps of the SDES algorithm including key generation, encryption, and decryption. The RSA code performs encryption and decryption using the RSA public/private key algorithm.

Uploaded by

Yamuna Jayabalan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LINK STATE PROTOCOL

import java.io.*; 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; System.out.println("LINK STATE"); System.out.println("Enter the number of nodes:"); DataInputStream d=new DataInputStream(System.in); n=Integer.parseInt(d.readLine()); for(i=0;i<n;i++) { for(j=0;j<n;j++) { System.out.println("Enter the cost from:"+(i+1)+"to"+(j+1)); cost[i][j]=Integer.parseInt(d.readLine()); cost[i][i]=0; cost[i][j]=cost[i][j]; dist[i][j]=j; } } System.out.println("Number of nodes:"+n); System.out.println("The cost matrix is:"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { System.out.print(cost[i][j]+"\t"); } System.out.println("\n"); } System.out.println("Enter the source router: "); m=Integer.parseInt(d.readLine()); 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++) { System.out.println("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 < p.length; ++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) { System.out.print( ((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( args.length != 2) { System.err.println( "Usage: SKT_SDES key(10 bit) plaintext(8 bit)"); System.exit(1); }

int K = Integer.parseInt( args[0], 2); SDES A = new SDES( K); int m = Integer.parseInt( args[1], 2);

System.out.print("Key K1: "); SDES.printb( A.K1, 8); System.out.print("\nKey K2: "); SDES.printb( A.K2, 8); m = A.encrypt( m);

System.out.print("\nEncrypted Message: "); SDES.printb( m, 8); m = A.decrypt( m); System.out.print("\nDecrypted Message: "); SDES.printb( m, 8); } }

DES ALGORITHM
import java.security.spec.*; import javax.crypto.*; import javax.crypto.spec.*; class DES { public static void main(String[] a) { if (a.length<2) { System.out.println("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 (test.equals("1")) { theKey = hexToBytes("133457799BBCDFF1"); theMsg = hexToBytes("0123456789ABCDEF"); theExp = hexToBytes("85E813540F0AB405"); } else if (test.equals("2")) { theKey = hexToBytes("38627974656B6579"); // "8bytekey" theMsg = hexToBytes("6D6573736167652E"); // "message." theExp = hexToBytes("7CF45E129445D451"); } else if (test.equals("3")) { theKey = hexToBytes("133457799BBCDFF1"); // = test 1 theMsg = hexToBytes("0808080808080808"); // PKCS5Padding theExp = hexToBytes("FDF2E174492922F8"); } else { System.out.println("Wrong option.1,2,3 are the valid arguments....");

return; } KeySpec ks = new DESKeySpec(theKey); SecretKeyFactory kf = SecretKeyFactory.getInstance("DES"); SecretKey ky = kf.generateSecret(ks); Cipher cf = Cipher.getInstance(algorithm); cf.init(Cipher.ENCRYPT_MODE,ky); byte[] theCph = cf.doFinal(theMsg); System.out.println("Key : "+bytesToHex(theKey)); System.out.println("Message : "+bytesToHex(theMsg)); System.out.println("Cipher : "+bytesToHex(theCph)); System.out.println("Expected: "+bytesToHex(theExp)); } catch (Exception e) { e.printStackTrace(); return; } } public static byte[] hexToBytes(String str) { if (str==null) { return null; } else if (str.length() < 2) { return null; } else { int len = str.length() / 2; byte[] buffer = new byte[len]; for (int i=0; i<len; i++) { buffer[i] = (byte) Integer.parseInt( str.substring(i*2,i*2+2),16); } return buffer; } } public static String bytesToHex(byte[] data) { if (data==null) { return null; } else { int len = data.length; String str = ""; for (int i=0; i<len; i++) { if ((data[i]&0xFF)<16) str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF); else str = str + java.lang.Integer.toHexString(data[i]&0xFF); } return str.toUpperCase(); } } }

RSA ALGORITHM
import java.io.*; import java.math.*; 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(System.in); System.out.println("Enter the value two Prime Numbers p and q: "); p=Integer.parseInt(din.readLine()); q=Integer.parseInt(din.readLine()); N=p*q; int c=-1; fi=((p-1)*(q-1)); //System.out.println("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; } } } System.out.println("Public Key KU:{"+e+","+N+"}"); System.out.println("Secret Private Key KR:{"+d+","+p+","+q+"}"); System.out.println("Enter the message :"); long msg,prod=1,res; msg=Integer.parseInt(din.readLine());

if(msg>=N) System.out.println("Enter a lesser value : "); else { for(i=0;i<e;i++) { prod*=msg; } res=prod%N; System.out.println("Encrypted message is "+res); prod=1; for(i=0;i<d;i++) { prod*=res; }

res=prod%N; System.out.println("Decrypted message is "+msg); } } }

You might also like