You are on page 1of 9

Dwi Putri Wahyuningsih

2019390004

Password Encrypted

Create a class called Password that implements the Encrypt able interface from this chapter.
Then create a main driver that instantiates a Secret object and a Password object, using the same
reference variable, and exercises their methods. Use any type of encryption desired for Password,
other than the Caesar cipher used by Secret.

Code RSA

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.Key;

import java.util.Arrays;

import javax.crypto.Cipher;

import java.util.Scanner;

public class RSA

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

String password = sc.nextLine();

KeyPair keyPair = RSAKeyPair.keyPairRSA();

Key publicKey = keyPair.getPublic();


Key privateKey = keyPair.getPrivate();

System.out.println("Original: " + password);

byte[] encrypted = RSAEncryptDecrypt.encrypt(password, privateKey);

System.out.println("Encrypted: " + new String(encrypted));

byte[] decrypted = RSAEncryptDecrypt.decrypt(encrypted, publicKey);

System.out.println("Decrypted: " + new String(decrypted));

final class RSAConstants

private RSAConstants()

public static final String ALGORITHM = "RSA";

public static final int ALGORITHM_BITS = 2048;

class RSAKeyPair

public static KeyPair keyPairRSA()

KeyPairGenerator generator = null;


try

generator =
KeyPairGenerator.getInstance(RSAConstants.ALGORITHM);

catch (Exception e)

e.printStackTrace();

if (generator != null)

generator.initialize(RSAConstants.ALGORITHM_BITS);

KeyPair keyPair = generator.genKeyPair();

return keyPair;

return null;

class RSAEncryptDecrypt

public static byte[] encrypt(String original, Key privateKey)

{
if (original != null && privateKey != null)

byte[] bs = original.getBytes();

byte[] encData = convert(bs, privateKey, Cipher.ENCRYPT_MODE);

return encData;

return null;

public static byte[] decrypt(byte[] encrypted, Key publicKey)

if (encrypted != null && publicKey != null)

byte[] decData = convert(encrypted, publicKey,


Cipher.DECRYPT_MODE);

return decData;

return null;

private static byte[] convert(byte[] data, Key key, int mode)

try

Cipher cipher = Cipher.getInstance(RSAConstants.ALGORITHM);


cipher.init(mode, key);

byte[] newData = cipher.doFinal(data);

return newData;

catch (Exception e)

e.printStackTrace();

return null;

Result

Code AES

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import java.util.Base64;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import java.util.Scanner;

public class AES {

private static SecretKeySpec secretKey;

private static byte[] key;

public static void setKey(String myKey)

MessageDigest sha = null;

try {

key = myKey.getBytes("UTF-8");

sha = MessageDigest.getInstance("SHA-1");

key = sha.digest(key);

key = Arrays.copyOf(key, 16);

secretKey = new SecretKeySpec(key, "AES");

catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}
catch (UnsupportedEncodingException e) {

e.printStackTrace();

public static String encrypt(String strToEncrypt, String secret)

try

setKey(secret);

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, secretKey);

return
Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));

catch (Exception e)

System.out.println("Error while encrypting: " + e.toString());

return null;

public static String decrypt(String strToDecrypt, String secret)

{
try

setKey(secret);

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");

cipher.init(Cipher.DECRYPT_MODE, secretKey);

return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));

catch (Exception e)

System.out.println("Error while decrypting: " + e.toString());

return null;

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

final String secretKey = "thisisoursecretkey";

System.out.print("Enter Your dedicated string: ");

String originalString = sc.nextLine();

String encryptedString = AES.encrypt(originalString, secretKey) ;

String decryptedString = AES.decrypt(encryptedString, secretKey) ;

System.out.println("Original : " + originalString);


System.out.println("Encrypted : " + encryptedString);

System.out.println("Decrypted : " + decryptedString);

Result

You might also like