您的位置:首页 > 其它

AES加密解密

2016-08-25 16:11 316 查看
# 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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: