Password OOP
Password OOP
Password OOP
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;
private RSAConstants()
class RSAKeyPair
generator =
KeyPairGenerator.getInstance(RSAConstants.ALGORITHM);
catch (Exception e)
e.printStackTrace();
if (generator != null)
generator.initialize(RSAConstants.ALGORITHM_BITS);
return keyPair;
return null;
class RSAEncryptDecrypt
{
if (original != null && privateKey != null)
byte[] bs = original.getBytes();
return encData;
return null;
return decData;
return null;
try
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;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
try
setKey(secret);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return
Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
catch (Exception e)
return null;
{
try
setKey(secret);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
catch (Exception e)
return null;
Result