Last active
August 25, 2016 07:44
-
-
Save wei-spring/847180e1504ebd6532235d47bcddc8fa to your computer and use it in GitHub Desktop.
AES加密解密
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
# AES加密解密 | |
import javax.crypto.*; | |
import java.security.*; | |
public class Java { | |
private static SecretKey key = null; | |
private static Cipher cipher = null; | |
public static void main(String[] args) throws Exception | |
{ | |
Security.addProvider(new com.sun.crypto.provider.SunJCE()); | |
KeyGenerator keyGenerator = | |
KeyGenerator.getInstance("DESede"); | |
keyGenerator.init(168); | |
SecretKey secretKey = keyGenerator.generateKey(); | |
cipher = Cipher.getInstance("DESede"); | |
String clearText = "I am an Employee"; | |
byte[] clearTextBytes = clearText.getBytes("UTF8"); | |
cipher.init(Cipher.ENCRYPT_MODE, secretKey); | |
byte[] cipherBytes = cipher.doFinal(clearTextBytes); | |
String cipherText = new String(cipherBytes, "UTF8"); | |
cipher.init(Cipher.DECRYPT_MODE, secretKey); | |
byte[] decryptedBytes = cipher.doFinal(cipherBytes); | |
String decryptedText = new String(decryptedBytes, "UTF8"); | |
System.out.println("Before encryption: " + clearText); | |
System.out.println("After encryption: " + cipherText); | |
System.out.println("After decryption: " + decryptedText); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment