Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.hyperion.util;
- import java.security.Key;
- import javax.crypto.Cipher;
- import javax.crypto.KeyGenerator;
- /**
- * Encrypts and decrypts using the DESede algorithm.
- *
- * @author Ryan Greene
- *
- */
- public class Cryption {
- /**
- * The sentence being crypted and decrypted.
- */
- private static final String sentence = "Ryan is such a fucking pro if this works.";
- /**
- * The algorithm being used to crypt and decrypt.
- */
- private static final String algorithm = "DESede";
- /**
- * The key instance.
- */
- private static Key key = null;
- /**
- * The cipher instance.
- */
- private static Cipher cipher = null;
- /**
- * Decrypts a string using the DESede algorithm.
- *
- * @param bytes
- * The encrypted string.
- * @return The decrypted string.
- */
- private static String decrypt(byte[] bytes) {
- try {
- cipher.init(Cipher.DECRYPT_MODE, key);
- return new String(cipher.doFinal(bytes));
- } catch (Exception e) {
- System.err.println(e);
- return null;
- }
- }
- /**
- * Encrypts a string using the DESede algorithm.
- *
- * @param string
- * The string being encrypted.
- * @return The encrypted string.
- */
- private static byte[] encrypt(String string) {
- try {
- cipher.init(Cipher.ENCRYPT_MODE, key);
- return cipher.doFinal(string.getBytes());
- } catch (Exception e) {
- System.err.println(e);
- return null;
- }
- }
- /**
- * Runs the program.
- *
- * @param args
- * The running arguments.
- */
- public static void main(String[] args) {
- try {
- key = KeyGenerator.getInstance(algorithm).generateKey();
- cipher = Cipher.getInstance(algorithm);
- System.out.println(encrypt(sentence));
- System.out.println(decrypt(encrypt(sentence)));
- } catch (Exception e) {
- System.err.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement