Kay_Xie 2021-10-02 10:02 采纳率: 33.3%
浏览 62
已结题

已知由对称密钥加密算法加密的密文和密钥,求用java写解密的代码 java 有问必答

密文kxFIF1Iuj95KJ15SyWIcsA==
密钥HceDKgR0qoenzsk46l3Uxg==
用AES CBC加密模式
求不用软件包com.sun.org.apache.xml.internal.security.utils.Base64 的代码
有人能帮忙吗

  • 写回答

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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月10日
  • 已采纳回答 10月2日
  • 创建了问题 10月2日

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP