Skip to content

Instantly share code, notes, and snippets.

@ahmedengu
Created July 11, 2016 20:02
Show Gist options
  • Save ahmedengu/ea9dd8b918578f6b05571601cbf33b0d to your computer and use it in GitHub Desktop.
Save ahmedengu/ea9dd8b918578f6b05571601cbf33b0d to your computer and use it in GitHub Desktop.
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
class DesEncrypter {
Cipher ecipher, dcipher;
DesEncrypter(SecretKey key) throws Exception {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
}
}
public class Main {
public static void main(String[] argv) throws Exception {
DesEncrypter encrypter = new DesEncrypter(KeyGenerator.getInstance("DES").generateKey());
System.out.println(new sun.misc.BASE64Encoder().encode(encrypter.ecipher.doFinal("secret".getBytes("UTF8"))));
byte[] utf8 = encrypter.dcipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(new sun.misc.BASE64Encoder().encode(encrypter.ecipher.doFinal("secret".getBytes("UTF8")))));
System.out.println(new String(utf8, "UTF8"));
}
}
1k2SZ1ltr2k=
secret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment