Created
October 27, 2018 17:22
-
-
Save jerryalexis/e691e1d18ea19014e2bfe43a4c75c30e to your computer and use it in GitHub Desktop.
this is code for send encryption message between client and server program on java
This file contains 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
#### sender code: | |
****************************************************************************************************************** | |
package encryptionmessage; | |
import java.security.Key; | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.SecretKeySpec; | |
import javax.swing.JOptionPane; | |
import javax.xml.bind.DatatypeConverter; | |
import java.lang.String; | |
public class sender { | |
public static void main(String[] args)throws IOException { | |
int Port =Integer.parseInt(JOptionPane.showInputDialog("please create password")); | |
String IP = JOptionPane.showInputDialog("Input Your IP"); | |
Socket sock=new Socket("192.168.17",123456); | |
DataInputStream in= new DataInputStream(sock.getInputStream()); | |
System.out.println(in.readUTF()); | |
DataOutputStream out =new DataOutputStream(sock.getOutputStream()); | |
out.writeUTF("message sending"); | |
} | |
public static void main(String[] args) throws Exception { | |
String password = JOptionPane.showInputDialog("Input the key: "); | |
String message = JOptionPane.showInputDialog("Input Your message: "); | |
String encryptionMessage = encryptAES(messageText, keyEncryption); | |
JOptionPane.showMessageDialog(null,messageEncrypted); | |
} | |
private static byte[] convertToByte(String kunci) { | |
byte[] array_byte = new byte[20]; | |
int i = 0; | |
while (i < kunci.length()) { | |
array_byte[i] = (byte) kunci.charAt(i); | |
i++; } | |
if (i < 20) { | |
while (i < 20) { | |
array_byte[i] = (byte) i; | |
i++; | |
} } | |
return array_byte; | |
} | |
sock.close(); | |
} | |
} | |
************************************************************************************************ | |
#### receiver code: | |
************************************************************************************************ | |
package encryptionmessage; | |
import java.security.Key; | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.SecretKeySpec; | |
import javax.swing.JOptionPane; | |
import javax.xml.bind.DatatypeConverter; | |
import java.lang.String; | |
public class receiver { | |
public static void main(String[] args) throws IOException{ | |
int Port =Integer.parseInt(JOptionPane.showInputDialog("Input the passord")); | |
String IP = JOptionPane.showInputDialog("Input Your IP"); | |
ServerSocket serverSock=new ServerSocket(123456); | |
Socket Sock=serverSock .accept(); | |
DataOutputStream out =new DataOutputStream(Sock.getOutputStream()); | |
} | |
public static String decryptAES(String encryptedData, String encryptionKey) | |
throws Exception { | |
Cipher c = Cipher.getInstance("AES"); | |
Key key = generateKey(encryptionMessage); | |
c.init(Cipher.DECRYPT_MODE, key); | |
byte[] decordedValue = DatatypeConverter | |
.parseBase64Binary(encryptedData); | |
byte[] decValue = c.doFinal(decordedValue); | |
String decryptedValue = new String(decValue); | |
return decryptedValue; | |
JOptionPane.showMessageDialog(null,encryptionMessage); | |
JOptionPane.showMessageDialog(null,message); | |
out.writeUTF("you have message"); | |
DataInputStream in= new DataInputStream(Sock.getInputStream()); | |
System.out.println(in.readUTF()); | |
Sock.close(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment