In: Computer Science
Choose one of the following cryptography techniques and
implement it using (java )programming language. Your program should
provide the user with two options Encryption and Decryption, with a
simple UI to get the input from the user, and view the result. You
can use any restriction you need for the user input but you need to
clarify that and validate the user input base on your
restriction.
● Feistel ● Keyword columnar ● Any cryptosystem of your choice
(needs to be approved by the instructor)
NOTE:-Following Java program accepts text from user, encrypts it using RSA algorithm and, prints the cipher of the given text, decrypts the cipher and prints the decrypted text again.( It may have some compiling error solve them sorry but shortage of time)
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Signature;
import javax.crypto.Cipher;
public class CipherDecrypt {
public static void main(String args[]) throws Exception{
//Creating a Signature object
Signature sign = Signature.getInstance("SHA256withRSA");
//Creating KeyPair generator object
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
//Initializing the key pair generator
keyPairGen.initialize(2048);
//Generate the pair of keys
KeyPair pair = keyPairGen.generateKeyPair();
//Getting the public key from the key pair
PublicKey publicKey = pair.getPublic();
//Creating a Cipher object
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
//Initializing a Cipher object
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//Add data to the cipher
byte[] input = "Please upvote it will be great of you".getBytes();
cipher.update(input);
//encrypting the data
byte[] cipherText = cipher.doFinal();
System.out.println( new String(cipherText, "UTF8"));
//Initializing the same cipher for decryption
cipher.init(Cipher.DECRYPT_MODE, pair.getPrivate());
//Decrypting the text
byte[] decipheredText = cipher.doFinal(cipherText);
System.out.println(new String(decipheredText));
}
}
Output
Encrypted Text: ]/[?F3?D?p v?w?!?H???^?A??????P?u??FA? ? ???_?? ???_jMH-??>??OP?'?j?_?n` ?_??'`????o??_GL??g???g_f?????f|???LT?|?Vz_TDu#??\?<b,,?$C2???Bq?#?lDB`??g,^??K?_?v???`} ?;LX?a?_5e???#???_?6?/B&B_???^?__Ap^#_?q?IEh????_?,??*??]~_?_?D? _y???lp??a?P_U{ Decrypted Text: Please upvote it will be great of you
Thanks i hope this will help you.
All the best