diff --git a/.idea/libraries/Java_EE_6_Java_EE_6.xml b/.idea/libraries/Java_EE_6_Java_EE_6.xml new file mode 100644 index 0000000..2ee8ace --- /dev/null +++ b/.idea/libraries/Java_EE_6_Java_EE_6.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/DeansBequest.iml b/DeansBequest.iml index 011b29b..92034cc 100644 --- a/DeansBequest.iml +++ b/DeansBequest.iml @@ -58,5 +58,6 @@ + \ No newline at end of file diff --git a/lib/javax.annotation.jar b/lib/javax.annotation.jar new file mode 100644 index 0000000..52dca7f Binary files /dev/null and b/lib/javax.annotation.jar differ diff --git a/lib/javax.ejb.jar b/lib/javax.ejb.jar new file mode 100644 index 0000000..4ebf5ec Binary files /dev/null and b/lib/javax.ejb.jar differ diff --git a/lib/javax.jms.jar b/lib/javax.jms.jar new file mode 100644 index 0000000..d31451a Binary files /dev/null and b/lib/javax.jms.jar differ diff --git a/lib/javax.persistence.jar b/lib/javax.persistence.jar new file mode 100644 index 0000000..21d80e0 Binary files /dev/null and b/lib/javax.persistence.jar differ diff --git a/lib/javax.resource.jar b/lib/javax.resource.jar new file mode 100644 index 0000000..696a234 Binary files /dev/null and b/lib/javax.resource.jar differ diff --git a/lib/javax.servlet.jar b/lib/javax.servlet.jar new file mode 100644 index 0000000..0519e4a Binary files /dev/null and b/lib/javax.servlet.jar differ diff --git a/lib/javax.servlet.jsp.jar b/lib/javax.servlet.jsp.jar new file mode 100644 index 0000000..9c0631c Binary files /dev/null and b/lib/javax.servlet.jsp.jar differ diff --git a/lib/javax.servlet.jsp.jstl.jar b/lib/javax.servlet.jsp.jstl.jar new file mode 100644 index 0000000..7be17cc Binary files /dev/null and b/lib/javax.servlet.jsp.jstl.jar differ diff --git a/lib/javax.transaction.jar b/lib/javax.transaction.jar new file mode 100644 index 0000000..729c695 Binary files /dev/null and b/lib/javax.transaction.jar differ diff --git a/src/Encryption/Encyptonator.java b/src/Encryption/Encyptonator.java index c1118f6..0b01a1e 100644 --- a/src/Encryption/Encyptonator.java +++ b/src/Encryption/Encyptonator.java @@ -1,29 +1,79 @@ package Encryption; import javax.crypto.Cipher; +import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; -import javax.crypto.spec.SecretKeySpec; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; import java.util.Base64; +import java.util.Scanner; public class Encyptonator { - private static Cipher ecipher; - private static Cipher dcipher; + protected static Cipher ecipher; + protected static Cipher dcipher; - private static SecretKey key; + protected static SecretKey key; + + public static void main(String[] args) + { + Scanner input = new Scanner(System.in); + + + try + { + + // generate secret key using DES algorithm +// key = KeyGenerator.getInstance("DES").generateKey(); + System.out.println("Enter key: "); + key = convertKey(input.nextLine()); + + + ecipher = Cipher.getInstance("DES"); + dcipher = Cipher.getInstance("DES"); + + // initialize the ciphers with the given key + + ecipher.init(Cipher.ENCRYPT_MODE, key); + + dcipher.init(Cipher.DECRYPT_MODE, key); + + System.out.println("Enter plaintext: "); + String encrypted = encrypt(input.nextLine()); + + String decrypted = decrypt(encrypted); + System.out.println("Encrypted: " + encrypted); + System.out.println("Decrypted: " + decrypted); + + + } catch (NoSuchAlgorithmException e) + { + System.out.println("No Such Algorithm:" + e.getMessage()); + } catch (NoSuchPaddingException e) + { + System.out.println("No Such Padding:" + e.getMessage()); + } catch (InvalidKeyException e) + { + System.out.println("Invalid Key:" + e.getMessage()); + } + + } public static String encrypt(String str) { - - try { + try + { // encode the string into a sequence of bytes using the named charset // storing the result into a new byte array. - byte[] utf8 = str.getBytes(StandardCharsets.UTF_8); + byte[] utf8 = str.getBytes(); byte[] enc = ecipher.doFinal(utf8); @@ -33,51 +83,63 @@ public class Encyptonator return new String(enc); + } catch (Exception e) + { + + e.printStackTrace(); + } - - catch (Exception e) - { - - e.printStackTrace(); - - } - - return null; + return null; } - public static String decrypt(String str) { + public static String decrypt(String str) + { - try { + try + { - // decode with base64 to get bytes + // decode with base64 to get bytes -byte[] dec = Base64.getDecoder().decode(str.getBytes()); + byte[] dec = Base64.getDecoder().decode(str.getBytes()); -byte[] utf8 = dcipher.doFinal(dec); + byte[] utf8 = dcipher.doFinal(dec); // create new string based on the specified charset -return new String(utf8, StandardCharsets.UTF_8); + return new String(utf8, StandardCharsets.UTF_8); - } + } catch (Exception e) + { - catch (Exception e) { + e.printStackTrace(); - e.printStackTrace(); + } - } - - return null; + return null; } - public static SecretKey convertStringToSecretKey(String encodedKey) + + public static SecretKey convertKey(String HexaKey) { - byte[] decodedKey = Base64.getDecoder().decode(encodedKey); - SecretKey originalKey = new SecretKeySpec(decodedKey,0,decodedKey.length,"DES"); - return originalKey; + try + { + byte[] keyData = HexaKey.getBytes(); + SecretKeyFactory keyF = SecretKeyFactory.getInstance("DES"); + DESKeySpec desKeySpec = new DESKeySpec(keyData); + + + return keyF.generateSecret(desKeySpec); + + + } catch (NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException e) + { + e.printStackTrace(); + } + return null; } + } diff --git a/src/Encryption/EncyptonatorTest.java b/src/Encryption/EncyptonatorTest.java new file mode 100644 index 0000000..3c84082 --- /dev/null +++ b/src/Encryption/EncyptonatorTest.java @@ -0,0 +1,72 @@ +package Encryption; + +import org.junit.jupiter.api.Test; + +import javax.crypto.Cipher; +import javax.crypto.NoSuchPaddingException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +import static org.testng.AssertJUnit.assertEquals; + +class EncyptonatorTest extends Encyptonator +{ + + @Test + void manyquestions() + { + try + { + + // generate secret key using DES algorithm +// key = KeyGenerator.getInstance("DES").generateKey(); + key = convertKey("3b3898371520f75e"); + String plaintext = "123abc456def7890"; + + + ecipher = Cipher.getInstance("DES"); + dcipher = Cipher.getInstance("DES"); + + // initialize the ciphers with the given key + + ecipher.init(Cipher.ENCRYPT_MODE, key); + + dcipher.init(Cipher.DECRYPT_MODE, key); + + String encrypted = encrypt(plaintext); + System.out.println(encrypted); + + String decrypted = decrypt(encrypted); + assertEquals(decrypted, plaintext); + String dec = decrypt("LJrviRhD0DtdRy9DL8HoxNHK37k7D3/t"); + assertEquals(plaintext, dec); + } + catch (NoSuchAlgorithmException e) + { + System.out.println("No Such Algorithm:" + e.getMessage()); + } catch (NoSuchPaddingException e) + { + System.out.println("No Such Padding:" + e.getMessage()); + } catch (InvalidKeyException e) + { + System.out.println("Invalid Key:" + e.getMessage()); + } + + } + + @Test + void encrypt() + { + + } + + @Test + void decrypt() + { + } + + @Test + void convertStringToSecretKey() + { + } +} \ No newline at end of file