欢迎来到天天文库
浏览记录
ID:57686316
大小:17.00 KB
页数:5页
时间:2020-09-01
《AES加密算法的java实现.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、packagecom.encryp;importjava.security.InvalidKeyException;importjava.security.NoSuchAlgorithmException;importjava.security.Security;importjavax.crypto.BadPaddingException;importjavax.crypto.Cipher;importjavax.crypto.IllegalBlockSizeException;importjavax.crypto.KeyGenerator;i
2、mportjavax.crypto.NoSuchPaddingException;importjavax.crypto.SecretKey;publicclassEncrypAES{//KeyGenerator提供对称密钥生成器的功能,支持各种算法privateKeyGeneratorkeygen;//SecretKey负责保存对称密钥privateSecretKeydeskey;//Cipher负责完成加密或解密工作privateCipherc;//该字节数组负责保存加密的结果privatebyte[]cipherByte;publicEnc
3、rypAES()throwsNoSuchAlgorithmException,NoSuchPaddingException{Security.addProvider(newcom.sun.crypto.provider.SunJCE());//实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)keygen=KeyGenerator.getInstance("AES");//生成密钥deskey=keygen.generateKey();//生成Cipher对象,指定其支持的DES算法c=Cipher.getInstance(
4、"AES");}/***对字符串加密**@paramstr*@return*@throwsInvalidKeyException*@throwsIllegalBlockSizeException*@throwsBadPaddingException*/publicbyte[]Encrytor(Stringstr)throwsInvalidKeyException,IllegalBlockSizeException,BadPaddingException{//根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式c.init
5、(Cipher.ENCRYPT_MODE,deskey);byte[]src=str.getBytes();//加密,结果保存进cipherBytecipherByte=c.doFinal(src);returncipherByte;}/***对字符串解密**@parambuff*@return*@throwsInvalidKeyException*@throwsIllegalBlockSizeException*@throwsBadPaddingException*/publicbyte[]Decryptor(byte[]buff)throw
6、sInvalidKeyException,IllegalBlockSizeException,BadPaddingException{//根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式c.init(Cipher.DECRYPT_MODE,deskey);cipherByte=c.doFinal(buff);returncipherByte;}/***@paramargs*@throwsNoSuchPaddingException*@throwsNoSuchAlgorithmException*@throwsBadP
7、addingException*@throwsIllegalBlockSizeException*@throwsInvalidKeyException*/publicstaticvoidmain(String[]args)throwsException{EncrypAESde1=newEncrypAES();Stringmsg="郭XX-搞笑相声全集";byte[]encontent=de1.Encrytor(msg);byte[]decontent=de1.Decryptor(encontent);System.out.println("明文
8、是:"+msg);System.out.println("加密后:"+newString(encontent));System.out.println
此文档下载收益归作者所有