Created
April 6, 2019 18:44
-
-
Save vnkdj5/3a2b69f0224334747b143c88fc810932 to your computer and use it in GitHub Desktop.
Advanced Encyption Standard Client Server Implementation in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.net.Socket; | |
import java.net.UnknownHostException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.util.Base64; | |
import javax.crypto.*; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
public class AES { | |
public final static String SERVER="127.0.0.1"; | |
public static final int PORT=8088; | |
public static void main(String[] args) throws NoSuchAlgorithmException { | |
String plainText="Vaibhav Navnath Kumbhar"; | |
try { | |
Socket soc=new Socket(SERVER, PORT); | |
System.out.println("Just connected to " + soc.getRemoteSocketAddress()); | |
DataOutputStream out=new DataOutputStream(soc.getOutputStream()); | |
DataInputStream in=new DataInputStream(soc.getInputStream()); | |
//GENERATING KEYS | |
KeyGenerator keyGenerator=KeyGenerator.getInstance("AES"); | |
keyGenerator.init(128); | |
SecretKey key=keyGenerator.generateKey(); | |
// Generating IV.(INitializaion vector)/We are using CBC | |
byte[] IV = new byte[16]; | |
SecureRandom random = new SecureRandom(); | |
random.nextBytes(IV); | |
byte[] cipherText = encrypt(plainText.getBytes(),key, IV); | |
System.out.println("Plain Text: :"+plainText); | |
System.out.println("Encrypted Text : "+Base64.getEncoder().encodeToString(cipherText) ); | |
String cipherString=Base64.getEncoder().encodeToString(cipherText); | |
System.out.println("Symmetric Key "+Base64.getEncoder().encodeToString(key.getEncoded())); | |
out.writeUTF(Base64.getEncoder().encodeToString(key.getEncoded())); //key | |
out.write(IV); | |
out.writeUTF(cipherString); | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public static byte[] encrypt(byte[] plaintext, SecretKey key, byte[] IV) throws Exception{ | |
//Get Cipher Instance | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
//Create SecretKeySpec | |
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES"); | |
//Create IvParameterSpec | |
IvParameterSpec ivSpec = new IvParameterSpec(IV); | |
//Initialize Cipher for ENCRYPT_MODE | |
cipher.init(Cipher.ENCRYPT_MODE, keySpec,ivSpec ); | |
//Perform Encryption | |
byte[] cipherText = cipher.doFinal(plaintext); | |
return cipherText; | |
} | |
public static String decrypt (byte[] cipherText, SecretKey key,byte[] IV) throws Exception | |
{ | |
//Get Cipher Instance | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
//Create SecretKeySpec | |
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES"); | |
//Create IvParameterSpec | |
IvParameterSpec ivSpec = new IvParameterSpec(IV); | |
//Initialize Cipher for DECRYPT_MODE | |
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); | |
//Perform Decryption | |
byte[] decryptedText = cipher.doFinal(cipherText); | |
return new String(decryptedText); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.Base64; | |
import javax.crypto.Cipher; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
public class AES_SERVER { | |
public static void main(String[] args) { | |
final int PORT=8088; | |
try{ | |
ServerSocket serverSocket = new ServerSocket(PORT); | |
System.out.println("Started server on port "+PORT+"\nWaiting for client connection"); | |
Socket server = serverSocket.accept(); | |
System.out.println("Just connected to " + server.getRemoteSocketAddress()); | |
// Accepts the data from client | |
DataInputStream in = new DataInputStream(server.getInputStream()); | |
DataOutputStream out = new DataOutputStream(server.getOutputStream()); | |
String encodedkey = in.readUTF(); | |
byte[] decodedKey = Base64.getDecoder().decode(encodedkey); | |
// rebuild key using SecretKeySpec | |
SecretKey key = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); | |
byte[]IV=new byte[16]; | |
int noBytes=in.read(IV); | |
String cipherText=in.readUTF(); | |
System.out.println("Symmetric Key :"+encodedkey); | |
System.out.println("Received CipherText: "+cipherText); | |
String decryptedText=decrypt(Base64.getDecoder().decode(cipherText),key,IV);//todo | |
System.out.println("Decrypted text: "+decryptedText); | |
} | |
catch(Exception e){ | |
} | |
} | |
public static String decrypt (byte[] cipherText, SecretKey key,byte[] IV) throws Exception | |
{ | |
//Get Cipher Instance | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
//Create SecretKeySpec | |
SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES"); | |
//Create IvParameterSpec | |
IvParameterSpec ivSpec = new IvParameterSpec(IV); | |
//Initialize Cipher for DECRYPT_MODE | |
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); | |
//Perform Decryption | |
byte[] decryptedText = cipher.doFinal(cipherText); | |
return new String(decryptedText); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment