how to send encrypted messages between two parties in java

in #utopian-io6 years ago

Repository

https://github.com/dmlloyd/openjdk

What Will I Learn?

  • You will learn how to connect two program on Netbeans.
  • You will learn how to encrypt message.
  • You will learn about hill cipher method

Requirements

State the requirements the user needs in order to follow this tutorial.

  • you must know basic of java programming.
  • you must know how to implement encryption method.

Difficulty

Choose one of the following options:

  • Basic

Tutorial Contents

on this occasion I will make a program to encrypt messages between two parties or more using the hill chipper method. the first party will write a message that will be converted into codes that cannot be understood because it has been encrypted, and then the second user will enter the password, the message can be read.


  • the first step you have to do is create a package for the two programs that we will create later.



  • okay, then create a new class in the package that we made earlier.
  • then type this code, I will explain it at the bottom.
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();
      
    }
}
  • Okay, I will explain a little about the program above.
  • This is a declaration of package and class names.
package encryptionmessage;
public class sender {
  • and this is the process of making parts to enter passwords and servers between the two programs.
int Port =Integer.parseInt(JOptionPane.showInputDialog("please create password"));
String IP = JOptionPane.showInputDialog("Input Your IP");
  • and this is the part to display notifications that the message has been sent.
DataOutputStream out =new DataOutputStream(sock.getOutputStream());
out.writeUTF("message sending");
  • the code below serves to display the message input window.
String message = JOptionPane.showInputDialog("Input Your message: ");
  • This serves to display messages that have been encryptedJOptionPane.showMessageDialog(null,messageEncrypted);
  • and this is a very important part of the formula for converting messages into incomprehensible codes.
  private static byte[] convertToByte(String key) {
        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;
 }
  • for the sender program is finish, then we will make the second program in the same package, the recipient program.
  • create a new class with the name receiver and type the following 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());
       out.writeUTF("you have message");
       DataInputStream in= new DataInputStream(Sock.getInputStream());
           
    }
    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);
      System.out.println(in.readUTF());
       Sock.close(); 
    }
}

  • actually the second program is almost the same as the first program, the difference is only in the method.
  • I will explain a little about the program above.
  • this is class encryption.
public static String decryptAES(String encryptedData, String encryptionKey)
   throws Exception {
  • this is the process of calling the cipher methodCipher c = Cipher.getInstance("AES");
  • and this is the generate key process
Key key = generateKey(encryptionMessage);
        c.init(Cipher.DECRYPT_MODE, key);
  • and this is the message conversion process
byte[] decordedValue = DatatypeConverter
                .parseBase64Binary(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
  • the last is the section to display messages that have been decrypted.
JOptionPane.showMessageDialog(null,encryptionMessage);
        JOptionPane.showMessageDialog(null,message);
  • Okay, the two programs have been finished, now we will run it.
  • first we will run the sender program, right click and select run file.


  • then it will appear like this, enter the key.

  • then enter the message.

  • this is a message sent, encrypted.

  • the next step is running the program receiver
  • then enter the password.

  • then the message will appear.

  • Okay, the tutorial is complete, don't forget to try.

Curriculum

Include a list of related tutorials you have already shared on Utopian that make up a Course Curriculum, if applicable.


Proof of Work Done

https://gist.github.com/jerryalexis/e691e1d18ea19014e2bfe43a4c75c30e

Sort:  

Thank you for your contribution @jerryloyal.
After analyzing your tutorial we suggest the following points:

  • We suggest you put comments in your code, it helps users a lot to understand what they are developing.

  • Put more theory into your tutorial so the user can understand the technology and functionality that you are implementing.

  • Better detail your tutorial, don't forget you are teaching people to develop what you are explaining in your tutorial.

  • Certain parts of your code already exist on the internet. Always put the references of the articles you've been searching for. Link

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your review, @portugalcoin! Keep up the good work!

Hi @jerryloyal!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @jerryloyal!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!