Sie sind auf Seite 1von 8

Hill Cipher

Program :
import java.io.*;

class HillCipher { public static void main(String arg[])throws Exception{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int mat[][]=new int[3][3]; int key[]={1,3,3,1,4,3,1,3,4}; int i,j; int k=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { mat[i][j]=key[k]; k++; } } String cipher=""; System.out.println("enter plaintext:"); String plain=br.readLine(); int l=plain.length(); char a[]=new char[l]; a=plain.toCharArray(); int cut=(int) Math.ceil((float)l/3); j=0; k=0;

int temp[][]=new int[3][1]; int c[][]=new int[3][1]; while(j<cut){ for(i=0;i<3;i++){ if(k<l){ temp[i][0]=a[k]-97; k++; } else temp[i][0]='*'; } for(i=0;i<3;i++){ for(k=0;k<3;k++){ if(k==0){ c[i][0]=0; } c[i][0]+=mat[i][k]*temp[k][0]; } c[i][0]%=26; cipher+=(char)(c[i][0]+97); } j++; } System.out.println("cipher text is\t "+cipher); int x,y,m,n; int tmat[][]=new int[2][2]; int adj[][]=new int[3][3]; for(x=0;x<3;x++){ for(y=0;y<3;y++){ m=0;

for(i=0;i<3;i++){ if(i==x) continue; n=0; for(j=0;j<3;j++){ if(j==y) continue; tmat[m][n]=mat[i][j]; n++; } m++; } adj[x][y]=tmat[0][0]*tmat[1][1]-tmat[1][0]*tmat[0][1]; if((x+y)%2==1){ adj[x][y]= -adj[x][y]; } } } int det=0; for(i=0;i<3;i++){ det+=mat[0][i]*adj[0][i]; } int inv[][]=new int[3][3];

if(det==0){ System.out.println(" determinant of the matrix is zero. sorry"); } else { for(i=0;i<3;i++){ for(j=0;j<3;j++){ inv[i][j]=adj[j][i]/det;

while(inv[i][j]<0){ inv[i][j]+=26; } inv[i][j]%=26;

} } } String decipher=""; j=0; int z; z=0; k=0; while(j<cut){ for(i=0;i<3;i++){ for(k=0;k<3;k++){ if(k==0){ temp[i][0]=0; } temp[i][0]+=inv[i][k]*c[k][0]; }

temp[i][0]%=26; decipher+=(char)(temp[i][0]+97); } j++; } System.out.println("Decrypted text :: "+decipher); } }

Output :

Conclusion

Thus we can successfully implement Hill Cipher Cryptography technique in Lab .

RSA Algorithm
Program :
import java.io.*; import java.math.BigInteger; import java.util.*; class rsa{

public static void main (String args[])throws Exception{

BigInteger p; Random rng=new Random();

p=BigInteger.probablePrime(32,rng); BigInteger q; q=BigInteger.probablePrime(32,rng); System.out.println(p); BigInteger z=new BigInteger("1"); BigInteger n=p.multiply(q); BigInteger v=(p.subtract(z)).multiply(q.subtract(z));

BigInteger k=BigInteger.probablePrime(32,rng);

BigInteger d=k.modInverse(v);

System.out.println("Encryption KEY : " + k); System.out.println("Decrytion Key : " + d);

//BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //int mm =Integer.parseInt(br.readLine()); BigInteger m = new BigInteger("123456789");

System.out.println("\nEncrypted : " + m.modPow(k, n));

BigInteger l=m.modPow(k, n);

System.out.println("\nDecryted : " + l.modPow(d, n));

} }

Output :

Conclusion :
Thus we can successfully implement RSA algorithm in lab.

Das könnte Ihnen auch gefallen