徘徊、居家家 2020-03-20 21:45 采纳率: 100%
浏览 1676
已采纳

Php如何实现base64加密数据和java互通

现在需要对接对方的java平台,有个接口用到了图片数据的传输,需要将图片数据转换为base64编码后传输,发现使用PHP自带的base64_encode()之后,传输到对方平台,对方无法解密;

对方java使用的(new sun.misc.BASE64Encoder()).encode(data)该方法base64编码(对方不换加解密方法);

在网上搜了一圈,说PHP的Base64编码和java的这一个编码结果无法一致,请各位大神帮忙看看有什么解决办法。

  • 写回答

2条回答 默认 最新

  • zqbnqsdsmd 2020-03-20 23:45
    关注
    <?php
    /**
     * Base64 编码/解码
     * @author liruixing
     */
    class Base64{
        private $_base64hash = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; /*这是Base64编码使用的标准字典*/
        private $_DecodeTable = array( /* 这是php源码中使用的解码表,包含了256个字符对应的编码 */
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
            52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
            -2,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
            15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
            -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
            41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
            -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
        );
        private $_encode_data = 0xfc0000;
        private $_debug       = false;
        private $_encode_result = '';
        public function encode($str) {
            $len = strlen($str);
            $num = 0;
            $bin = 0;
            $arr = array();
            if($len >= 3) {
                for($i=0;$i<$len;$i++) {
                    $bin = $bin << 8;
                    if($this->_debug) {
                        echo '$bin = ',decbin($bin),"\n";
                        echo 'binary = ', decbin(ord($str[$i])),"\n";
                    }
                    $bin = $bin | ord($str{$i});
                    if(($i+1)%3 == 0) {
                        $this->_encode_func($bin,3);
                        $bin = 0;
                    }
                }
            }
            if($len%3 == 1) {
                $bin = ord($str[$len-1]);
                $bin = $bin << 4;
                $this->_encode_func($bin,1);
                $this->_encode_result .= '==';
            } else if($len%3 == 2) {
                $bin = ord($str[$len-2]);
                $bin = $bin << 8;
                $bin = $bin | ord($str[$len-1]);
                $bin = $bin << 2;
                $this->_encode_func($bin,2);
                $this->_encode_result .= '=';
            }
            return $this->_encode_result;
        }
        private function _encode_func($bin,$bytes = 3) {
            $num = 3;
            $matches = 0;
            $bits1 = ($num - $bytes) * 6;
            $bits2 = $bytes * 6;
            $matches = $this->_encode_data >> $bits1;
            while( $matches ) {
                $result = $bin & $matches;
                $result = $result >> $bits2;
                $bytes--;
                $bits1 = ($num - $bytes) * 6;
                $bits2 = $bytes * 6;
                $matches = $this->_encode_data >> $bits1;
                if($this->_debug) {
                    echo '$result = ',$result,' binary = ',decbin($result),"\n";
                }
                $this->_encode_result .= $this->_base64hash[$result];
            }
        }
        public function decode($str) {
            $bin = 0;
            $length = strlen($str)-1;
            $_decode_result = '';
            $len = 0;
            $i = 0;
            while( ($len <= $length) ) {
                $ch = $str[$len++];
                if ($ch == '=') { // 当前一个字符是“=”号
                    /*
                    先说明一个概念:在解码时,4个字符为一组进行一轮字符匹配。
                        如果某一轮匹配的第二个是“=”且第三个字符不是“=”,说明这个带解析字符串不合法,直接返回空
                    */
                    if ($str[$len] != '=' && (($i % 4) == 1)) {
                        return NULL;
                    }
                    continue;
                }
                $ch = $this->_DecodeTable[ord($ch)];
                // 下面这连个条件,只有 ch < 0 会起作用,ch == -2 永远不会起作用,即所有不合法的字符均跳过。
                if ($ch < 0 || $ch == -1) { /* a space or some other separator character, we simply skip over */
                    continue;
                } else if ($ch == -2) {
                    return NULL;
                }
                switch($i % 4) {
                    case 0:
                        $bin = intval($ch) << 2;
                        break;
                    case 1:
                        $bin = intval($bin) | intval($ch) >> 4;
                        $_decode_result .= chr($bin);
                        $bin = ( intval($ch) & 0x0f ) << 4;
                        break;
                    case 2:
                        $bin = intval($bin) | intval($ch) >> 2;
                        $_decode_result .= chr($bin);
                        $bin = ( intval($ch) & 0x03 ) << 6;
                        break;
                    case 3:
                        $bin = intval($bin) | intval($ch);
                        $_decode_result .= chr($bin);
                        break;
                }
                $i++;
            }
            return $_decode_result;
        }
        public function debug($open = true) {
            $this->_debug = $open;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问
  • ¥15 初学者如何快速上手学习stm32?
  • ¥15 如何自动更换布娃娃图片上的衣服
  • ¥15 心理学eprime编程