密文kxFIF1Iuj95KJ15SyWIcsA==
密钥HceDKgR0qoenzsk46l3Uxg==
用AES CBC加密模式
求不用软件包com.sun.org.apache.xml.internal.security.utils.Base64 的代码
有人能帮忙吗
已知由对称密钥加密算法加密的密文和密钥,求用java写解密的代码 java 有问必答
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 神仙别闹 2021-10-02 10:47关注
import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import Decoder.BASE64Encoder; import Decoder.BASE64Decoder; import com.alibaba.fastjson.JSONObject; public class Test { private String ALGO = "AES"; private String ALGO_MODE = "AES/CBC/NoPadding"; private String akey = "kxFIF1Iuj95KJ15SyWIcsA=="; private String aiv = "HceDKgR0qoenzsk46l3Uxg=="; public static void main(String[] args) throws Exception { Test aes = new Test();//创建AES JSONObject data = new JSONObject();//创建Json的加密对象 data.put("haha", "hehe"); System.out.println("原始数据:"+data.toJSONString()); String rstData = pkcs7padding(data.toJSONString());//进行PKCS7Padding填充 String passwordEnc = aes.encrypt(rstData);//进行java的AES/CBC/NoPadding加密 String passwordDec = aes.decrypt(passwordEnc);//解密 System.out.println("加密之后的字符串:"+passwordEnc); System.out.println("解密后的数据:"+passwordDec); } public String encrypt(String Data) throws Exception { try { byte[] iv = toByteArray(aiv);//因为要求IV为16byte,而此处aiv串为32位字符串,所以将32位字符串转为16byte Cipher cipher = Cipher.getInstance(ALGO_MODE); int blockSize = cipher.getBlockSize(); byte[] dataBytes = Data.getBytes(); int plaintextLength = dataBytes.length; if (plaintextLength % blockSize != 0) { plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); } byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO); IvParameterSpec ivspec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); String EncStr = (new BASE64Encoder()).encode(encrypted);//将cipher加密后的byte数组用base64加密生成字符串 return EncStr ; } catch (Exception e) { e.printStackTrace(); return null; } } public String decrypt(String encryptedData) throws Exception { try { byte[] encrypted1 = (new BASE64Decoder()).decodeBuffer(encryptedData); byte[] iv = toByteArray(aiv); Cipher cipher = Cipher.getInstance(ALGO_MODE); SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO); IvParameterSpec ivspec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original); return originalString.trim();//此处添加trim()是为了去除多余的填充字符,就不用去填充了,具体有什么问题我还没有遇到,有强迫症的同学可以自己写一个PKCS7UnPadding函数 } catch (Exception e) { e.printStackTrace(); return null; } } //此函数是将字符串每两个字符合并生成byte数组 public static byte[] toByteArray(String hexString) { hexString = hexString.toLowerCase(); final byte[] byteArray = new byte[hexString.length() >> 1]; int index = 0; for (int i = 0; i < hexString.length(); i++) { if (index > hexString.length() - 1) return byteArray; byte highDit = (byte) (Character.digit(hexString.charAt(index), 16) & 0xFF); byte lowDit = (byte) (Character.digit(hexString.charAt(index + 1), 16) & 0xFF); byteArray[i] = (byte) (highDit << 4 | lowDit); index += 2; } System.out.println(byteArray.length); return byteArray; } //此函数是pkcs7padding填充函数 public static String pkcs7padding(String data) { int bs = 16; int padding = bs - (data.length() % bs); String padding_text = ""; for (int i = 0; i < padding; i++) { padding_text += (char)padding; } return data+padding_text; } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 机器学习能否像多层线性模型一样处理嵌套数据
- ¥20 西门子S7-Graph,S7-300,梯形图
- ¥50 用易语言http 访问不了网页
- ¥50 safari浏览器fetch提交数据后数据丢失问题
- ¥15 matlab不知道怎么改,求解答!!
- ¥15 永磁直线电机的电流环pi调不出来
- ¥15 用stata实现聚类的代码
- ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
- ¥20 docker里部署springboot项目,访问不到扬声器
- ¥15 netty整合springboot之后自动重连失效