DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: cuiqingbo
今日帖子: 17
在线用户: 9
导航: 论坛 -> Web应用开发 斑竹:bodies  
作者:
男 aknightchen (.) ★☆☆☆☆ -
盒子活跃会员
2023/2/24 16:40:34
标题:
Java的Base64与Delphi自带的, 在处理汉字时, 不一样的结果, DELPHI中如何能改?或JAVA中如何能改? 浏览:1144
加入我的收藏
楼主: Java中的base64.js :

Java的转码例子, 可以用在线BASE64计算:

https://base64.us/

例如:
今天天气不错==>Base64是:5LuK5aSp5aSp5rCU5LiN6ZSZ

delphi中,可以用标准单元EncdDecd.pas
却是:

vfHM7Mzsxviyu7Tt


二者不一样, 我想用java做前端, delphi做中间层.
但二者在处理汉字时不一样的结果,

有哪位大哥知道如何调整吗?
不论是改java,还是改delphi,都行.




----------
/*
 *  base64.js
 *
 *  Licensed under the BSD 3-Clause License.
 *    http://opensource.org/licenses/BSD-3-Clause
 *
 *  References:
 *    http://en.wikipedia.org/wiki/Base64
 */
;
(function(global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ?
    module.exports = factory(global) :
    typeof define === 'function' && define.amd ?
    define(factory) : factory(global)
}((
  typeof self !== 'undefined' ? self :
  typeof window !== 'undefined' ? window :
  typeof global !== 'undefined' ? global :
  this
), function(global) {
  'use strict';
  // existing version for noConflict()
  global = global || {};
  var _Base64 = global.Base64;
  var version = "2.5.1";
  // if node.js and NOT React Native, we use Buffer
  var buffer;
  if (typeof module !== 'undefined' && module.exports) {
    try {
      buffer = eval("require('buffer').Buffer");
    } catch (err) {
      buffer = undefined;
    }
  }
  // constants
  var b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  var b64tab = function(bin) {
    var t = {};
    for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;
    return t;
  }(b64chars);
  var fromCharCode = String.fromCharCode;
  // encoder stuff
  var cb_utob = function(c) {
    if (c.length < 2) {
      var cc = c.charCodeAt(0);
      return cc < 0x80 ? c :
        cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6)) +
          fromCharCode(0x80 | (cc & 0x3f))) :
        (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f)) +
          fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +
          fromCharCode(0x80 | (cc & 0x3f)));
    } else {
      var cc = 0x10000 +
        (c.charCodeAt(0) - 0xD800) * 0x400 +
        (c.charCodeAt(1) - 0xDC00);
      return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07)) +
        fromCharCode(0x80 | ((cc >>> 12) & 0x3f)) +
        fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) +
        fromCharCode(0x80 | (cc & 0x3f)));
    }
  };
  var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;
  var utob = function(u) {
    return u.replace(re_utob, cb_utob);
  };
  var cb_encode = function(ccc) {
    var padlen = [0, 2, 1][ccc.length % 3],
      ord = ccc.charCodeAt(0) << 16 |
      ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8) |
      ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),
      chars = [
        b64chars.charAt(ord >>> 18),
        b64chars.charAt((ord >>> 12) & 63),
        padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),
        padlen >= 1 ? '=' : b64chars.charAt(ord & 63)
      ];
    return chars.join('');
  };

  var btoa = global.btoa ? function(b) {
    return global.btoa(b);
  } : function(b) {
    return b.replace(/[\s\S]{1,3}/g, cb_encode);
  };
  var _encode = buffer ?
    buffer.from && Uint8Array && buffer.from !== Uint8Array.from ?
    function(u) {
      return (u.constructor === buffer.constructor ? u : buffer.from(u))
        .toString('base64')
    } :
    function(u) {
      return (u.constructor === buffer.constructor ? u : new buffer(u))
        .toString('base64')
    } :
    function(u) {
      return btoa(utob(u))
    };
  var encode = function(u, urisafe) {
    return !urisafe ?
      _encode(String(u)) :
      _encode(String(u)).replace(/[+\/]/g, function(m0) {
        return m0 == '+' ? '-' : '_';
      }).replace(/=/g, '');
  };
  var encodeURI = function(u) {
    return encode(u, true)
  };
  // decoder stuff
  var re_btou = new RegExp([
    '[\xC0-\xDF][\x80-\xBF]',
    '[\xE0-\xEF][\x80-\xBF]{2}',
    '[\xF0-\xF7][\x80-\xBF]{3}'
  ].join('|'), 'g');
  var cb_btou = function(cccc) {
    switch (cccc.length) {
      case 4:
        var cp = ((0x07 & cccc.charCodeAt(0)) << 18) |
          ((0x3f & cccc.charCodeAt(1)) << 12) |
          ((0x3f & cccc.charCodeAt(2)) << 6) |
          (0x3f & cccc.charCodeAt(3)),
          offset = cp - 0x10000;
        return (fromCharCode((offset >>> 10) + 0xD800) +
          fromCharCode((offset & 0x3FF) + 0xDC00));
      case 3:
        return fromCharCode(
          ((0x0f & cccc.charCodeAt(0)) << 12) |
          ((0x3f & cccc.charCodeAt(1)) << 6) |
          (0x3f & cccc.charCodeAt(2))
        );
      default:
        return fromCharCode(
          ((0x1f & cccc.charCodeAt(0)) << 6) |
          (0x3f & cccc.charCodeAt(1))
        );
    }
  };
  var btou = function(b) {
    return b.replace(re_btou, cb_btou);
  };
  var cb_decode = function(cccc) {
    var len = cccc.length,
      padlen = len % 4,
      n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0) |
      (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0) |
      (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0) |
      (len > 3 ? b64tab[cccc.charAt(3)] : 0),
      chars = [
        fromCharCode(n >>> 16),
        fromCharCode((n >>> 8) & 0xff),
        fromCharCode(n & 0xff)
      ];
    chars.length -= [0, 0, 2, 1][padlen];
    return chars.join('');
  };
  var _atob = global.atob ? function(a) {
    return global.atob(a);
  } : function(a) {
    return a.replace(/\S{1,4}/g, cb_decode);
  };
  var atob = function(a) {
    return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g, ''));
  };
  var _decode = buffer ?
    buffer.from && Uint8Array && buffer.from !== Uint8Array.from ?
    function(a) {
      return (a.constructor === buffer.constructor ?
        a : buffer.from(a, 'base64')).toString();
    } :
    function(a) {
      return (a.constructor === buffer.constructor ?
        a : new buffer(a, 'base64')).toString();
    } :
    function(a) {
      return btou(_atob(a))
    };
  var decode = function(a) {
    return _decode(
      String(a).replace(/[-_]/g, function(m0) {
        return m0 == '-' ? '+' : '/'
      })
      .replace(/[^A-Za-z0-9\+\/]/g, '')
    );
  };
  var noConflict = function() {
    var Base64 = global.Base64;
    global.Base64 = _Base64;
    return Base64;
  };
  // export Base64
  global.Base64 = {
    VERSION: version,
    atob: atob,
    btoa: btoa,
    fromBase64: decode,
    toBase64: encode,
    utob: utob,
    encode: encode,
    encodeURI: encodeURI,
    btou: btou,
    decode: decode,
    noConflict: noConflict,
    __buffer__: buffer
  };
  // if ES5 is available, make Base64.extendString() available
  if (typeof Object.defineProperty === 'function') {
    var noEnum = function(v) {
      return {
        value: v,
        enumerable: false,
        writable: true,
        configurable: true
      };
    };
    global.Base64.extendString = function() {
      Object.defineProperty(
        String.prototype, 'fromBase64', noEnum(function() {
          return decode(this)
        }));
      Object.defineProperty(
        String.prototype, 'toBase64', noEnum(function(urisafe) {
          return encode(this, urisafe)
        }));
      Object.defineProperty(
        String.prototype, 'toBase64URI', noEnum(function() {
          return encode(this, true)
        }));
    };
  }
  //
  // export Base64 to the namespace
  //
  if (global['Meteor']) { // Meteor.js
    Base64 = global.Base64;
  }
  // module.exports and AMD are mutually exclusive.
  // module.exports has precedence.
  if (typeof module !== 'undefined' && module.exports) {
    module.exports.Base64 = global.Base64;
  } else if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define([], function() {
      return global.Base64
    });
  }
  // that's it!
  return {
    Base64: global.Base64
  }
}));

----------





同时,附一个我的DELPHI单元的自定义函数(与标准单元一样的base64结果)


function kBase64Encode(const S: String;iType:Integer=0): String;
const
  BASE_64_CHARSET='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var   //Knight.Chen xe5 验证通过了!!!
  R, C : Byte;
  F, L, M, N, U : Integer;
  P : PAnsiChar;
  s1,sAnsiResult,xx:AnsiString;
begin

  xx:=BASE_64_CHARSET;

  s1:=AnsiString(s);

  L := Length(S1);
  if L > 0 then
  begin
    M := L mod 3;
    N := (L div 3) * 4 + M;
    if M > 0 then Inc(N);
    U := N mod 4;
    if U > 0 then
    begin
      U := 4 - U;
      Inc(N, U);
    end;
    SetLength(sAnsiResult, N);
    P := Pointer(sAnsiResult);
    R := 0;
    for F := 0 to L - 1 do
    begin
      C := Byte(S1 [F + 1]);
      case F mod 3 of
        0 : begin
          P^ := xx[C shr 2 + 1];
          Inc(P);
          R := (C and 3) shl 4;
          end;
        1 : begin
          P^ := xx[C shr 4 + R + 1];
          Inc(P);
          R := (C and $0F) shl 2;
          end;
        2 : begin
          P^ := xx[C shr 6 + R + 1];
          Inc(P);
          P^ := xx[C and $3F + 1];
          Inc(P);
          end;
      end;
    end;
    if M > 0 then
    begin
      P^ := xx[R + 1];
      Inc(P);
    end;
    for F := 1 to U do
    begin
      P^ := '=';
      Inc(P);
    end;
  end else
    sAnsiResult := '';

  Result:=string(sAnsiResult);
end;
----------------------------------------------
...
作者:
男 janker (janker) ★☆☆☆☆ -
盒子活跃会员
2023/2/24 19:52:04
1楼: 你是用什么版本的DELPHI?
现在的版本,10.3以上:
直接:
TNetEncoding.Base64.Encode('今天天气不错')
输出:5LuK5aSp5aSp5rCU5LiN6ZSZ
----------------------------------------------
-
作者:
男 janker (janker) ★☆☆☆☆ -
盒子活跃会员
2023/2/24 19:57:44
2楼: 印象中,XE7(还是XE3?)以上版本,TEncoding和TNetEncoding就挺好用了
----------------------------------------------
-
作者:
男 aknightchen (.) ★☆☆☆☆ -
盒子活跃会员
2023/2/24 20:50:11
3楼: 我用的: XE5
----------------------------------------------
...
作者:
男 aknightchen (.) ★☆☆☆☆ -
盒子活跃会员
2023/2/24 20:58:42
4楼: 有没有类似的XE10.3中的,TNetEncoding.Base64.Encode, 但能支持XE5的BASE64
----------------------------------------------
...
作者:
男 zhoutler (苦行僧) ★☆☆☆☆ -
普通会员
2023/2/24 21:49:25
5楼: 打了一段话,有非法文字,就发了附件图了
此帖子包含附件:
PNG 图像
大小:21.7K
----------------------------------------------
-
作者:
男 janker (janker) ★☆☆☆☆ -
盒子活跃会员
2023/2/24 21:49:55
5楼: XE5没有System.NetEncoding这个单元吗?
----------------------------------------------
-
作者:
男 janker (janker) ★☆☆☆☆ -
盒子活跃会员
2023/2/24 22:01:52
6楼: xe5如果没有这个单元,那估计是从xe7开始才有了。
从高版本直接考System.NetEncoding单元估计都能用。
----------------------------------------------
-
作者:
男 mystery (艾伦希亚) ★☆☆☆☆ -
普通会员
2023/2/25 10:27:33
7楼: 都转换成同一个字符集就好了。utf8或gbk。
----------------------------------------------
-
作者:
男 aknightchen (.) ★☆☆☆☆ -
盒子活跃会员
2023/2/25 11:58:37
8楼: 谢谢各位的指点,我终于搞定了.我的修改如下:

加密时:  要先将传入的文本,转为UTF8String
解密时:  接收结果的变量,必须定义为UTF8String

以下是我整理的UTF8的base64,与网页中的java一模一样的结果:







function kBase64Encode_UTF8(const S: String): String;
const
  BASE_64_CHARSET ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var
  R, C : Byte;
  F, L, M, N, U : Integer;
  P : PAnsiChar;
  sAnsiResult,xx:AnsiString;
  s1:UTF8String;
begin
  //Knight.Chen xe5 验证通过了!!!
  xx:=AnsiString(BASE_64_CHARSET);

  s1:=UTF8String(s); //这里,必须先把传进来的STRING,转为为UTF8,

  L := Length(S1);
  if L > 0 then
  begin
    M := L mod 3;
    N := (L div 3) * 4 + M;
    if M > 0 then Inc(N);
    U := N mod 4;
    if U > 0 then
    begin
      U := 4 - U;
      Inc(N, U);
    end;
    SetLength(sAnsiResult, N);
    P := Pointer(sAnsiResult);
    R := 0;
    for F := 0 to L - 1 do
    begin
      C := Byte(S1 [F + 1]);
      case F mod 3 of
        0 : begin
          P^ := xx[C shr 2 + 1];
          Inc(P);
          R := (C and 3) shl 4;
          end;
        1 : begin
          P^ := xx[C shr 4 + R + 1];
          Inc(P);
          R := (C and $0F) shl 2;
          end;
        2 : begin
          P^ := xx[C shr 6 + R + 1];
          Inc(P);
          P^ := xx[C and $3F + 1];
          Inc(P);
          end;
      end;
    end;
    if M > 0 then
    begin
      P^ := xx[R + 1];
      Inc(P);
    end;
    for F := 1 to U do
    begin
      P^ := '=';
      Inc(P);
    end;
  end else
    sAnsiResult := '';

  Result:=string(sAnsiResult);
end;

function kBase64Decode_UTF8(const S: String): String;
const
  BASE_64_CHARSET ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var
  F, L, M, P: Integer;
  B, OutPos: Byte;
  OutB: Array[1..3] of Byte;
  Lookup: Array[Char] of Byte;
  R: PAnsiChar;
  s1,xx:AnsiString;
  sUTF8Result:UTF8String;
begin
  //Knight.Chen xe5 验证通过了!!!

  xx:=AnsiString(BASE_64_CHARSET);

  s1:=AnsiString(s);

  L := Length(S1);
  P := 0;
  while (L - P > 0) and (S1[L - P] = '=') do Inc(P);
  M := L - P;
  if M <> 0 then
  begin
    SetLength(sUTF8Result, (M * 3) div 4);
    FillChar(Lookup, Sizeof(Lookup), #0);
    for F := 0 to 63 do
      Lookup[xx[F + 1]] := F;
    R := Pointer(sUTF8Result);
    OutPos := 0;
    for F := 1 to L - P do
    begin
      B := Lookup[S1[F]];
      case OutPos of
          0 : OutB[1] := B shl 2;
          1 : begin
          OutB[1] := OutB[1] or (B shr 4);
          R^ := AnsiChar(OutB[1]);
          Inc(R);
          OutB[2] := (B shl 4) and $FF;
          end;
          2 : begin
          OutB[2] := OutB[2] or (B shr 2);
          R^ := AnsiChar(OutB[2]);
          Inc(R);
          OutB[3] := (B shl 6) and $FF;
          end;
          3 : begin
          OutB[3] := OutB[3] or B;
          R^ := AnsiChar(OutB[3]);
          Inc(R);
          end;
        end;
      OutPos := (OutPos + 1) mod 4;
    end;
    if (OutPos > 0) and (P = 0) then
      if OutB[OutPos] <> 0 then
        sUTF8Result := sUTF8Result + AnsiChar(OutB[OutPos]);
  end else
    sUTF8Result := '';

  Result:=string(sUTF8Result);
end;
----------------------------------------------
...
作者:
男 ddrfan (若苗瞬) ▲▲▲▲▲ -
普通会员
2023/2/25 14:22:10
9楼: 看标题就猜是编码格式不同,然后发现LZ已经解决了,果然是:)
----------------------------------------------
Bye bye DDRFAN...
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行164.0625毫秒 RSS