Posts

Showing posts from 2019

MD5 in java

import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5 { public static String getMD5(String input) { try { MessageDigest   md=MessageDigest.getInstance("MD5"); byte[] messageDigest = md.digest(input.getBytes()); BigInteger number = new BigInteger(1, messageDigest); String hashtext = number.toString(16); // Now we need to zero pad it if you actually want the full 32 chars. while (hashtext.length() < 32) { hashtext = "0" + hashtext; } return hashtext; } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } public static void main(String[] args) throws NoSuchAlgorithmException { String s="hello world"; System.out.println("your hashcode generated by MD5 is:"+getMD5(s)); } }

DES,Blowfish,AES(rijndael),RC4

import javax.crypto.*; import java.util.*; public class Blowfish { public static void main(String[] args)throws Exception { SecretKey key=KeyGenerator.getInstance("BlowFish").generateKey(); Cipher cip=Cipher.getInstance("BlowFish"); cip.init(Cipher.ENCRYPT_MODE,key); Scanner s=new Scanner(System.in); String pt=s.next(); byte[] encrypted=cip.doFinal(pt.getBytes()); cip.init(Cipher.DECRYPT_MODE,key); byte[] ct=cip.doFinal(encrypted); System.out.println("the new encrypted string is"+new String(encrypted)+"old string is"+new String(ct)); } }

Diffie Hellman java code

import java.util.*;   class Diffie_Hellman { public static void main( String args[]) { Scanner sc= new Scanner(System. in ); System.out.println( "Enter modulo(p)" ); int p=sc.nextInt(); System.out.println( "Enter primitive root of " +p); int g=sc.nextInt(); System.out.println( "Choose 1st secret no(Alice)" ); int a=sc.nextInt(); System.out.println( "Choose 2nd secret no(BOB)" ); int b=sc.nextInt(); int A = ( int )Math.pow(g,a)%p; int B = ( int )Math.pow(g,b)%p; int S_A = ( int )Math.pow(B,a)%p; int S_B =( int )Math.pow(A,b)%p; if (S_A==S_B) { System.out.println( "ALice and Bob can communicate with each other!!!" ); System.out.println( "They share a secret no = " +S_A); } else { System.out.println( "ALice and Bob cannot communicate with each other!!!" ); } } }