卖火箭的小女孩 2021-04-15 11:26 采纳率: 71.4%
浏览 62
已结题

java加解密问题(SM4)

加解密方式是SM4,加密没有问题,解密的时候为什么明文后面有一串方框。明文是正确的,

明文后面的方框是多余的,有什么方法给去掉。为什么会出现这种问题,原因是什么?

  • 写回答

3条回答 默认 最新

  • 张小帅和刘美美 2021-04-15 11:48
    关注
    /**
         * @description: 不限密文长度的SMS4解密,获得byte类型的明文
         * @param: cipherText(密文)
         * @param: key(密钥)
         * @return: byte[]
         */
        private static byte[] decodeSMS4(byte[] cipherText, byte[] key) {
            byte[] plaintext = new byte[cipherText.length];
            int k = 0;
            int cipherLen = cipherText.length;
            while (k + 16 <= cipherLen) {
                byte[] cellCipher = new byte[16];
                for (int i = 0; i < 16; i++) {
                    cellCipher[i] = cipherText[k + i];
                }
                byte[] cellPlain = decode16(cellCipher, key);
                for (int i = 0; i < cellPlain.length; i++) {
                    plaintext[k + i] = cellPlain[i];
                }
                k += 16;
            }
            return plaintext;
        }
    
        /**
         * @description: 解密,获得明文字符串
         * @param: cipherText(密文)
         * @param: key(密钥)
         * @return: java.lang.String
         */
        private static String decodeSMS4toString(byte[] cipherText, byte[] key) {
            byte[] plaintext = new byte[cipherText.length];
            plaintext = decodeSMS4(cipherText, key);
            return new String(plaintext);
        }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月5日
  • 已采纳回答 7月28日

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端