Created
February 3, 2015 11:44
-
-
Save WanghongLin/88d73d168bcb0f4d2bc9 to your computer and use it in GitHub Desktop.
DES example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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