Question

Create a one time pad encryption using chat-server so that two clients are communicating through a...

Create a one time pad encryption using chat-server so that two clients are communicating through a server sending private encrypted messages (button) and each have the ability to decrypt (button) the message.

JAVA PLEASE

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find below the code to encrypt and decrypt using one time pad encryption method. In a chat-server application, the only two additions will be the following:

  • Encrypt the message before a client sends it using a key
  • Decrypt the message before a client receives it using the same key

public class otp {
   private static String encrypt(String message, String key) {
       char[] encryptedMessage = new char[message.length()];
       char[] messageArray = message.toCharArray();
       char[] keyArray = key.toCharArray();
       int result = 0;
       for(int i=0; i<message.length();i++)
       {
           //97 is the ascii value of a. It can be subtracted from another ascii value to obtain the positional values (0,1....25)
           result = ((int)messageArray[i] - 97 + (int)keyArray[i] - 97) % 26;
           //In Java modulus of a number can be negative
           if (result <= 0)
           {
                   result = result + 26;  
           }
           encryptedMessage[i] = (char)(result+97);
       }
       return new String(encryptedMessage);
   }
  
   private static String decrypt(String encryptedMessage, String key) {
       char[] decryptedMessage = new char[encryptedMessage.length()];
       char[] keyArray = key.toCharArray();
       char[] encryptedMessageArray = encryptedMessage.toCharArray();
       int result = 0;
       for(int i=0; i<encryptedMessage.length();i++)
       {
           // Subtract to decrypt
           result = ((int)encryptedMessageArray[i] - 97 - ((int)keyArray[i] - 97))%26;
           if (result <= 0)
           {
                   result = result + 26;  
           }
           decryptedMessage[i] = (char)(result+97);
       }
       return new String(decryptedMessage);
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       String message = "helloworld";
       String key = "abcdefghij";
       String encryptedMessage = encrypt(message,key);
       System.out.println(encryptedMessage);
       String decryptedMessage = decrypt(encryptedMessage,key);
       System.out.println(decryptedMessage);
   }

}

Add a comment
Know the answer?
Add Answer to:
Create a one time pad encryption using chat-server so that two clients are communicating through a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab you will write a simple chat application in Python using TCP sockets. The...

    In this lab you will write a simple chat application in Python using TCP sockets. The chat application consists of a chat server and a chat client. At any one time, the chat server is communicating with just one chat client. When the client and the server are done chatting, they exchange end messages which ends the session. The client and server read and write Unicode strings using the readUTF() and writeUTF() methods of DataInputStream and DataOutputStream respectively. After compilation,...

  • Create a chat-server gui that implements a hamming code where each client has the ability to...

    Create a chat-server gui that implements a hamming code where each client has the ability to send a message of 4-bits , a 7-bit hamming code is generated, randomly one of the bits get messed up, is sent to reciever, and reciever automically finds location of error, fixes the hamming code, and shows the original message. JAVA PLEASE

  • Java programming problem. I've managed to get through the basic level of encryption, but the advanced...

    Java programming problem. I've managed to get through the basic level of encryption, but the advanced and military grade sections are beyond me. If someone can show me what the hell this code is supposed to look like I'd greatly appreciate it. In other words I need to write a code that can decrypt a string message by the following parameters. Basic: Using Caesar Cipher with a static shift of 1-25. This is easy to decrypt as there are only...

  • Two parties, A and B, need to perform encryption. They have one symmetric key between them. An in...

    Two parties, A and B, need to perform encryption. They have one symmetric key between them. An intruder acquires knowledge of the key. What is the impact on secrecy of previously exchanged messages? What would be the impact on secrecy of future messages? Is there any harm in A sending a plaintext message to B saying the key is no longer secret? Should A send that message encrypted with the now-exposed key? Explain your answers. (Computer Security II)

  • I've managed to get through the basic level of encryption, but the advanced and military grade...

    I've managed to get through the basic level of encryption, but the advanced and military grade sections are beyond me. If someone can show me what the hell this code is supposed to look like I'd greatly appreciate it. In other words I need to write a code that can decrypt a string message by the following parameters. Basic: Using Caesar Cipher with a static shift of 1-25. This is easy to decrypt as there are only 25 possibilities. Advanced:...

  • Describe how an attacker can obtain the one-time pad that is used to encrypt a message,...

    Describe how an attacker can obtain the one-time pad that is used to encrypt a message, given both the message and the ciphertext, and explain why your method works. Suppose that two equal-sized messages M1 and M2 are encrypted with the same one-time pad and let C1 and C2 be the resulting ciphertexts. Suppose further that an attacker captures both ciphertexts C1 and C2, and knows one of the two messages, say M1. Based on Part a), describe how the...

  • The Diffie-Hellman public-key encryption algorithm is an alternative key exchange algorithm that is used by protocols...

    The Diffie-Hellman public-key encryption algorithm is an alternative key exchange algorithm that is used by protocols such as IPSec for communicating parties to agree on a shared key. The DH algorithm makes use of a large prime number p and another large number, g that is less than p. Both p and g are made public (so that an attacker would know them). In DH, Alice and Bob each independently choose secret keys, ?? and ??, respectively. Alice then computes...

  • An e-mail message can travel through one of two server routes. The probability of transmission error...

    An e-mail message can travel through one of two server routes. The probability of transmission error in each of the servers and the proportion of messages that travel each route are shown in the following table. Assume that the servers are independent. A message must travel through Server 1 and 2 for Route 1 or through Server 3 and 4 for Route 2 (i.e., cannot travel through just one of the servers in the route). Percentage of Messages Probability of...

  • 1.Which of the following statements about asymmetric-key encryption is correct? a When using asym...

    1.Which of the following statements about asymmetric-key encryption is correct? a When using asymmetric-key encryption method, a total of two keys are necessary in electronic communication between two parties. b Employees in the same company share the same public key. c Most companies would like to manage the private keys for their employees. d Most companies would like to use a Certificate Authority to manage the public keys of their employees. e Two of the above are correct. 2 Which...

  • Java(Eclipse). The task will create a server and client and send a message from the client...

    Java(Eclipse). The task will create a server and client and send a message from the client to the server. We will have two classes NetworkClient and NetworkServerListener classes. These will each have a main to run from the command line The NetworkServerListener class will listen for a client connection from NetworkClient , receive a message, display the message to the console (stdout) and exit. The NetworkClient class will create a connection to the NetworkServerListener and send a message to the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT