Skip to content

Instantly share code, notes, and snippets.

@WanghongLin
Created February 3, 2015 11:44
Show Gist options
  • Save WanghongLin/88d73d168bcb0f4d2bc9 to your computer and use it in GitHub Desktop.
Save WanghongLin/88d73d168bcb0f4d2bc9 to your computer and use it in GitHub Desktop.
DES example
public class PrivateExample {
public static void main(String[] args) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
SecretKey secretKey = keyGenerator.generateKey();
String desString = Base64.encodeBase64String(secretKey.getEncoded());
System.out.println("DES Base64 String: " + desString);
byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec ivspec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(Base64.decodeBase64(desString), "DES"), ivspec);
byte[] encryptedData = cipher.doFinal("123456".getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
System.out.println(new String(cipher.doFinal(encryptedData)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment