Encrpytitoin?!

This commit is contained in:
Libkyy
2022-03-23 19:56:11 +02:00
parent d182e7633d
commit e7a9c10a96
13 changed files with 184 additions and 32 deletions

View File

@@ -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;
}
}

View File

@@ -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()
{
}
}