- Создание ключевого объекта с использованием KeyGenerator для алгоритма DESede.
- Создание шифра, реализующего преобразование DESede, с помощью
getInstance(String algorithm)
метода API.
Вот код: -
public class Main {
static String algorithm = "DESede";
public static void main(String[] args) throws Exception {
Key symKey = KeyGenerator.getInstance(algorithm).generateKey();
Cipher c = Cipher.getInstance(algorithm);
byte[] encryptionBytes = encryptF("texttoencrypt", symKey, c);
System.out.println("Decrypted: " + decryptF(encryptionBytes, symKey, c));
}
private static byte[] encryptF(String input, Key pkey, Cipher c) throws InvalidKeyException, BadPaddingException,
IllegalBlockSizeException {
c.init(Cipher.ENCRYPT_MODE, pkey);
byte[] inputBytes = input.getBytes();
return c.doFinal(inputBytes);
}
private static String decryptF(byte[] encryptionBytes, Key pkey, Cipher c) throws InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
c.init(Cipher.DECRYPT_MODE, pkey);
byte[] decrypt = c.doFinal(encryptionBytes);
String decrypted = new String(decrypt);
return decrypted;
}
}