毕业课题中需要使用加解密算法,要求加解密前后的数据长度不会变化,查了一些资料,发现可以采用AES加密的CFB跟OFB模式是无填充的模式,可以保持加解密前后数据的长度相等。下面上代码:

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; public class Encrypt_Decrypt_AES { /**
* AES加密算法,调用Java自有类库,采用CFB模式密文反馈无填充模式,可以保证数据长度在加密前后是相同的
* @param content 待加密内容
* @param key 密钥
* @return byte[] 加密结果用byte数组表示
*
*/
public static byte[] encrypt_AES(String content, String key) {
try {
Cipher aesECB = Cipher.getInstance("AES/CFB/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(key.getBytes());
aesECB.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
byte[] result = aesECB.doFinal(content.getBytes());
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return null;
} /**
*
* @param content 待解密内容,字符串形式
* @param key 解密用的密钥
* @return 使用字符串形式返回解密内容
* @throws UnsupportedEncodingException
*/ public static String decrypt_AES(byte[] content, String key) throws UnsupportedEncodingException {
try {
Cipher aesECB = Cipher.getInstance("AES/CFB/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec(key.getBytes());
aesECB.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
byte[] result = aesECB.doFinal(content);
String AES_decode=new String(result,"utf-8");
/************************************/
// System.out.println("解密结果:"+AES_decode);
return AES_decode;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return null;
} public static void main(String[] str) throws InvalidKeyException,
InvalidAlgorithmParameterException, IllegalBlockSizeException,
BadPaddingException, UnsupportedEncodingException { String s = "fire fox 22";
byte[] r = encrypt_AES(s, "@93456781234567A");
for (byte i : r){
String l = Integer.toBinaryString((i & 0xFF) + 0x100).substring(1);
System.out.print(l+" ");
}
System.out.println();
decrypt_AES(r, "@93456781234567A");
} }

最新文章

  1. jQuery-1.9.1源码分析系列(十一) DOM操作
  2. java中集合的使用
  3. 循序渐进Python3(十一) --2-- web之javascript
  4. GetStartedWithWin10Develop
  5. Java集合之ArrayList和LinkedList的实现原理以及Iterator详解
  6. Oracle外部表的使用
  7. android 对象传输及parcel机制
  8. ListHelper
  9. Pagodas(等差数列)
  10. magento 小问题解决方案集
  11. Linux 监控文件事件
  12. Es6的用法
  13. 「CodeForces - 717E」Paint it really, really dark gray (dfs)
  14. SQL日期时间和字符串函数
  15. 搭建ZooKeeper
  16. ngx_http_upstream_keepalive
  17. java写简单Excel 首行是目录 然后前台下载
  18. 2017-2018 ACM-ICPC Latin American Regional Programming Contest Solution
  19. JAVA , TOMCAT , AXIS2 环境变量配置
  20. Github.com sshkey 生成与添加

热门文章

  1. gitbook安装与使用,并使用docker部署
  2. iOS 渐变提示。错误弹出提示 几秒自动消失
  3. Android 手机版 ssr
  4. atitit r9 doc on home ntpc .docx
  5. Atitit php java python nodejs错误日志功能的比较
  6. myeclipse中的项目 如何在项目视窗中显示setting,classpath等配置文件
  7. phpmyadmin 上传超过50m限制
  8. 服务器tail输出正常,vim打开中文乱码
  9. SpringMVC Controller单例和多例
  10. Mysql分组查询group by语句详解