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,key,k, isdRight);
             c := ShiftSymbolIndex(c,key,k, isdRight);
             d := ShiftSymbolIndex(d,key,k, 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,key,k, isdRight);
                b := (ord(s[p])and %00000011); inc(b); b := ShiftSymbolIndex(b,key,k, 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,key,k, isdRight);
                inc(b); b := ShiftSymbolIndex(b,key,k, isdRight);
                inc(c); c := ShiftSymbolIndex(c,key,k, 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,p,k : integer;
         a,b,c,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,key,k, isdLeft);
             b := Pos(s[p+1],C_ALPHABET); b := ShiftSymbolIndex(b,key,k, isdLeft);
             c := Pos(s[p+2],C_ALPHABET); c := ShiftSymbolIndex(c,key,k, isdLeft);
             d := Pos(s[p+3],C_ALPHABET); d := ShiftSymbolIndex(d,key,k, 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],C_ALPHABET);   a := ShiftSymbolIndex(a,key,k, isdLeft);
                b := Pos(s[p+1],C_ALPHABET); b := ShiftSymbolIndex(b,key,k, 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,key,k, isdLeft);
                b := ShiftSymbolIndex(b,key,k, isdLeft);
                c := ShiftSymbolIndex(c,key,k, 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进行编码。     

要回复问题请先登录注册