Java:Base64使用键对字符串进行编码

如何解决Java:Base64使用键对字符串进行编码

| 嗨,我有数据和键(两个字符串)。需要使用Base64使用密钥对数据进行编码。可以给我一个示例代码吗?     

解决方法

        Base64不适用于“用密钥编码”。它只是一种编码方案:您可以使用Base64加密和解密字符串,而无需任何额外的操作。它仅用于(非常)基本的安全用法。     ,        您可以使用密钥对数据进行异或,然后对它进行base64编码。
var key = \"mykey\";
var mydata = \"some long text here\";
var output = \'\';

for (var i = 0,len = mydata.length; i < len; i++) {
   output += String.fromCharCode(mydata.charCodeAt(i) ^ key.charCodeAt(i % key.length));
}
然后使用某处的某些功能将\'output \'编码为base64     ,        如果您需要使用密钥使用Base64进行编码,那么即使在标准中未定义,实际上也并不难。 Base64使用64个符号的字母。前62个符号是英文字母的小写和大写字母,再加上0到9之间的数字。最后2个字符最常见的是+和/,但在实现方式上可能会有所不同。 所以现在了解,当您将字符串分解为位,并使用6位而不是每个符号8位将它们重新组合时,您将始终能够在字母表中查找符号,因为6位数字恰好具有64个不同的可能值。 Base64只是枚举从%000000(0)到111111(63)的符号。但是您可以在此符号查找期间使用键。假设您的6位数字是%000011(3),则它将索引您字母表中的第4个符号。但是,现在您可以使用密钥来修改该索引,将其向左或向右移动(例如)等于该密钥字符的ASCII码(8位数字)的位数。每当索引超出范围(低于0或高于63)时,您就将其传送到范围的另一侧。并且,如果在编码过程中将索引右移,请使用左方向进行解码(反之亦然)。基本上,您是在使用符号字符定义的模式来加扰符号查找。 在这里,您只需将Base64编码和一个键一起使用(而不是先键入输入然后编码)。别客气。 而且由于您要提供代码示例,因此下面是我编写的对象Pascal中的快速示例。如果您先为最终的字符串分配内存,然后将其写入,而不是在每次循环时都重新分配内存的循环中串联该字符串,则此代码可能会更快-但如果需要,您可以自己弄清楚以获得更好的性能:
const 
      C_ALPHABIG      = \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\';
      C_ALPHASMALL    = \'abcdefghijklmnopqrstuvwxyz\';
      C_ALPHA         = C_ALPHABIG+C_ALPHASMALL;
      C_DIGITS        = \'0123456789\';
      C_SYMBOLS       = \'+/\';
      C_ALPHABET      = C_ALPHA+C_DIGITS+C_SYMBOLS;

    type 
      TIndexShiftDirection = (isdLeft,isdRight);

      Function ShiftSymbolIndex(const AIndex: integer; const AKey: string; var ACurrentKeyPos: integer; const ADirection: TIndexShiftDirection): integer;
       begin
         Result := AIndex; if(AKey=\'\')then exit;
         if(ACurrentKeyPosLength(AKey))then ACurrentKeyPos := 1;
         if(ADirection=isdRight)
           then begin
                  Result := Result+Ord(AKey[ACurrentKeyPos]);
                  if(Result>64)then Result := Result mod 64;
                  if(Result=0)then Result :=64;
                end
           else begin
                  Result := Result-Ord(AKey[ACurrentKeyPos]);
                  if(Result=Length(AKey))
           then ACurrentKeyPos := 1
           else inc(ACurrentKeyPos);
       end;

      Function  Encode64(const s: string; const Key: string): string;
       var
         i,n,p,k : integer;
         a,b,c,d : byte;
       begin
         Result   := \'\'; k := 1; if(s=\'\')then exit;
         n := Length(s)div 3;
         if(n>0)then for i:=0 to n-1 do
           begin
             p := (i*3)+1;
             a := (ord(s[p])shr 2); inc(a);
             b := ((ord(s[p])and %00000011)shl 4)+(ord(s[p+1])shr 4); inc(b);
             c := ((ord(s[p+1])and %00001111)shl 2)+(ord(s[p+2])shr 6); inc(c);
             d := ord(s[p+2])and %00111111; inc(d);
           //
             a := ShiftSymbolIndex(a,key,k,isdRight);
             b := ShiftSymbolIndex(b,isdRight);
             c := ShiftSymbolIndex(c,isdRight);
             d := ShiftSymbolIndex(d,isdRight);
           //
             Result := Result
                     + C_ALPHABET[a]
                     + C_ALPHABET[b]
                     + C_ALPHABET[c]
                     + C_ALPHABET[d];
           end;
         n := Length(s)-(n*3);
         if(n=0)then begin {Result := Result+\'0\';} exit; end;
         case n of
           1: begin
                p := Length(s);
                a := (ord(s[p])shr 2);         inc(a); a := ShiftSymbolIndex(a,isdRight);
                b := (ord(s[p])and %00000011); inc(b); b := ShiftSymbolIndex(b,isdRight);
                Result := Result
                        + C_ALPHABET[a]
                        + C_ALPHABET[b]
                        {+ \'2\'};//if Length(endoced_str)mod 4 = 2,then this case is true
              end;
           2: begin
                p := Length(s)-1;
                a := (ord(s[p])shr 2);
                b := ((ord(s[p])and %00000011)shl 4)+(ord(s[p+1])shr 4);
                c := (ord(s[p+1])and %00001111);
                inc(a); a := ShiftSymbolIndex(a,isdRight);
                inc(b); b := ShiftSymbolIndex(b,isdRight);
                inc(c); c := ShiftSymbolIndex(c,isdRight);
                Result := Result
                        + C_ALPHABET[a]
                        + C_ALPHABET[b]
                        + C_ALPHABET[c]
                        {+ \'4\'};//if Length(endoced_str)mod 4 = 3,then this case is true
              end;
         end;
       end;

      Function  Decode64(const s: string; const Key: string): string;
       var
         n,i,d : byte;
       begin
         Result := \'\'; k:=1; if(s=\'\')then exit;
         n := Length(s)div 4;
         if(n>0)then for i:=0 to n-1 do
           begin
             p := (i*4)+1;
             a := Pos(s[p],C_ALPHABET);   a := ShiftSymbolIndex(a,isdLeft);
             b := Pos(s[p+1],C_ALPHABET); b := ShiftSymbolIndex(b,isdLeft);
             c := Pos(s[p+2],C_ALPHABET); c := ShiftSymbolIndex(c,isdLeft);
             d := Pos(s[p+3],C_ALPHABET); d := ShiftSymbolIndex(d,isdLeft);
             if(a*b*c*d=0)then begin Result := \'\'; exit; end; //cannot be,if symbols are valid
             Result := Result
                     + chr(((a-1)shl 2) + ((b-1)shr 4))
                     + chr((((b-1)and %001111)shl 4) + ((c-1)shr 2))
                     + chr((((c-1)and %000011)shl 6) + (d-1));
           end;
         n := Length(s)mod 4;
         if(n=0)then exit;
         case n of
           2: begin
                p := Length(s)-1;
                a := Pos(s[p],isdLeft);
                b := Pos(s[p+1],isdLeft);
                if(a*b=0)then begin Result := \'\'; exit; end; //cannot be,if symbols are valid
                Result := Result
                        + chr(((a-1)shl 2) + (b-1));
              end;
           3: begin
                p := Length(s)-2;
                a := Pos(s[p],C_ALPHABET);
                b := Pos(s[p+1],C_ALPHABET);
                c := Pos(s[p+2],C_ALPHABET);
                if(a*b*c=0)
                  then begin Result := \'\'; exit; end; //cannot be,if symbols are valid
                a := ShiftSymbolIndex(a,isdLeft);
                b := ShiftSymbolIndex(b,isdLeft);
                c := ShiftSymbolIndex(c,isdLeft);
                Result := Result
                        + chr(((a-1)shl 2) + ((b-1)shr 4))
                        + chr((((b-1)and %001111)shl 4) + (c-1));
              end;
           else Result := \'\';
         end;
       end;  

注意函数ShiftSymbolIndex-这是符号查找扰码器,它可以向右或向左移动符号索引。我在编码器中使用权,在编码器中使用权,但这完全取决于您。 如果您跳过Encode64或Decode64函数中的key参数(或者如果您传递了空字符串键),那么最终将使用默认的Base64编码/解码。 另外,此编码器不会将填充(\“ = \”字符)追加到base64编码的字符串上。填充不需要解码,除非您的解码器在严格模式下工作(该编码器不是)-但这可以使您自己弄清楚。     ,        您可以使用apache commons编解码器库的
Base64
类。这是它的主页和下载页面。     ,        您可以使用对称二进制加密算法(例如Twofish或RC4)来利用这种密钥,然后将结果编码为base-64。     ,        Base64不包含使用密钥加密的功能。您可以先使用AES,DES等进行加密,然后再使用base64进行编码。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-