预览加载中,请您耐心等待几秒...
1/3
2/3
3/3

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

packagecom.encryp; importjava.security.InvalidKeyException; importjava.security.NoSuchAlgorithmException; importjava.security.Security; importjavax.crypto.BadPaddingException; importjavax.crypto.Cipher; importjavax.crypto.IllegalBlockSizeException; importjavax.crypto.KeyGenerator; importjavax.crypto.NoSuchPaddingException; importjavax.crypto.SecretKey; publicclassEncrypAES{ //KeyGenerator提供对称密钥生成器的功能,支持各种算法 privateKeyGeneratorkeygen; //SecretKey负责保存对称密钥 privateSecretKeydeskey; //Cipher负责完成加密或解密工作 privateCipherc; //该字节数组负责保存加密的结果 privatebyte[]cipherByte; publicEncrypAES()throwsNoSuchAlgorithmException,NoSuchPaddingException{ Security.addProvider(newcom.sun.crypto.provider.SunJCE()); //实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常) keygen=KeyGenerator.getInstance("AES"); //生成密钥 deskey=keygen.generateKey(); //生成Cipher对象,指定其支持的DES算法 c=Cipher.getInstance("AES"); } /** *对字符串加密 * *@paramstr *@return *@throwsInvalidKeyException *@throwsIllegalBlockSizeException *@throwsBadPaddingException */ publicbyte[]Encrytor(Stringstr)throwsInvalidKeyException,IllegalBlockSizeException,BadPaddingException{ //根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式 c.init(Cipher.ENCRYPT_MODE,deskey); byte[]src=str.getBytes(); //加密,结果保存进cipherByte cipherByte=c.doFinal(src); returncipherByte; } /** *对字符串解密 * *@parambuff *@return *@throwsInvalidKeyException *@throwsIllegalBlockSizeException *@throwsBadPaddingException */ publicbyte[]Decryptor(byte[]buff)throwsInvalidKeyException,IllegalBlockSizeException,BadPaddingException{ //根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式 c.init(Cipher.DECRYPT_MODE,deskey); cipherByte=c.doFinal(buff); returncipherByte; } /** *@paramargs *@throwsNoSuchPaddingException *@throwsNoSuchAlgorithmException *@throwsBadPaddingException *@throwsIllegalBlockSizeException *@throwsInvalidKeyException */ publicstaticvoidmain(String[]args)throwsException{ EncrypAESde1=newEncrypAES(); Stringmsg="郭XX-搞笑相声全集"; byte[]encontent=de1.Encrytor(msg); byte[]decontent=de1.Decryptor(encontent); System.out.pr