Question

Write a short Java program that uses private key (symmetric) encryption method to encrypt a short...

Write a short Java program that uses private key (symmetric) encryption method to encrypt a short string. Approximate algorithm:

Enter a text string

Use Java to generate a key and encrypt the string

Use Java (and the key) to decrypt the string

Compare original and decrypted string to make sure it is the same

Note: Please make it as simple as possible

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

package com.javapapers.java.security;

import java.security.Security;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import java.util.Scanner;

public class EncryptTextEntered {

//initializing cipher Text as null

private static Cipher cipherText = null;

public static void main(String[] args) throws Exception {

//generating key using DESede key generator

KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");

// size of key must be 112 or 168 for this provider

keyGenerator.init(168);

//generating private key

SecretKey privateKey = keyGenerator.generateKey();

cipherText = Cipher.getInstance("DESede");

Scanner ss = new Scanner(System.in);

System.out.print("Enter text string : ");

// Below Statement used for getting String including sentence

String text = ss.nextLine();

System.out.println("Plain Text Before Encryption: " + text);

//encrypting the string into UTF8 key format

byte[] plainTextByte = text.getBytes("UTF8");

byte[] encryptedBytes = encrypt(plainTextByte, privateKey);

String encryptText = new String(encryptedBytes, "UTF8");

System.out.println("Encrypted Text After Encryption: " + encryptText);

byte[] decryptedBytes = decrypt(encryptedBytes, privateKey);

String decryptText = new String(decryptedBytes, "UTF8");

System.out.println("Decrypted Text After Decryption: " + decryptText);

}

static byte[] encrypt(byte[] plainTextByte, SecretKey privateKey)

throws Exception {

cipherText.init(Cipher.ENCRYPT_MODE, privateKey);

byte[] encryptedBytes = cipherText.doFinal(plainTextByte);

return encryptedBytes;

}

static byte[] decrypt(byte[] encryptedBytes, SecretKey privateKey)

throws Exception {

cipherText.init(Cipher.DECRYPT_MODE, privateKey);

byte[] decryptedBytes = cipherText.doFinal(encryptedBytes);

return decryptedBytes;

}

}

output:

Enter Text string : This is a text file.

Plain Text Before Encryption: This is a text file.
Encrypted Text After Encryption: ‡(?ê?z@ou7ÿ—pø"é³.Õ0Ò;jVi¶‚
Decrypted Text After Decryption: This is a text file.
Add a comment
Know the answer?
Add Answer to:
Write a short Java program that uses private key (symmetric) encryption method to encrypt a short...
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
  • Write a message in a text file, encrypt it, and make a public or private key...

    Write a message in a text file, encrypt it, and make a public or private key to be sent for someone. Use GPG to encrypt a message in a text file. create a unique encryption key, and then use that key to encrypt your file.

  • Exercise 3.2 (Symmetric and Asymmetric Encryption) t In this exercise, you will send an encrypted...

    Exercise 3.2 (Symmetric and Asymmetric Encryption) t In this exercise, you will send an encrypted message from a socket client to a socket server. The message will be encrypted using AES symmetric encryption algorithm in the client. The server receives this ciphertext and decrypt it using the same symmetric key. Before communication over symmetric encryption, you need to first securely distribute the symmetric key between sender and receiver. You can use asymmetric encryption to help distribute the symmetric key. The...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

  • 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...

  • In this lab, you will write a C program to encrypt a file. The program prompts...

    In this lab, you will write a C program to encrypt a file. The program prompts the user to enter a key (maximum of 5 bytes), then the program uses the key to encrypt the file. Note that the user might enter more than 5 characters, but only the first 5 are used. If a new line before the five characters, the key is padded with 'a'; Note that the new line is not a part of the key, but...

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want...

    C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...

  • just need help with part c key and public key cryptography methods 2. (a) Explain the...

    just need help with part c key and public key cryptography methods 2. (a) Explain the difference between the symmetric (b) In the famou s RSA algorithm for public key cryptography, very large prime numbers are used so as to make ult for the attackers to find from their product the prime factors. However, for an illustration of the ideas behind the RSA algorithm, you could chooses two small prime numbers 7 and 11, and a public key e 13...

  • MASM Assembly language -- Message Encryption Pgm You are to write a program to input a...

    MASM Assembly language -- Message Encryption Pgm You are to write a program to input a text and a key and encrypt the text. I will supply you an encryption key consisting of multiple characters. Use this key to encrypt and decrypt the plain text by XOR-ing each character of the key against a corresponding byte in the message. Repeat the key as many times as necessary until all plain text bytes are translated. Suppose, for example, the key were...

  • 1.     This project will extend Project 3 and move the encryption of a password to a...

    1.     This project will extend Project 3 and move the encryption of a password to a user designed class. The program will contain two files one called Encryption.java and the second called EncrytionTester.java. 2.     Generally for security reasons only the encrypted password is stored. This program will mimic that behavior as the clear text password will never be stored only the encrypted password. 3.     The Encryption class: (Additionally See UML Class Diagram) a.     Instance Variables                                                i.     Key – Integer...

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