2011-06-14 150 views
0

嗨,我有数据和密钥(都是字符串)。数据需要使用Base64的密钥进行编码。有人可以给我一个示例代码。Java:Base64使用密钥对字符串进行编码

+0

一个不base64编码与“钥匙”。它(base64)是*编码*方案,而不是*加密*算法。这就是说,我很困惑,真正的目标是 - 也许标题是非常糟糕的选择。 – 2011-06-14 18:00:12

回答

4

Base64不适用于'使用密钥编码'。它只是一种编码方案:您可以使用Base64来加密和解密字符串,而不需要额外的。这只是非常(非常)基本的安全用法。

+0

+1,但它可能是OP希望使用某个“密钥”(无论他的意思是:来自PKCS的公钥还是对称密钥等)进行编码,然后使用Base64对结果进行编码。 – SyntaxT3rr0r 2011-06-14 20:20:05

1

你可以用你的密钥对数据进行异或运算,然后用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)); 
} 

,然后你编码“输出”为base64使用一些功能从某处

0

你可以使用一个对称二元加密算法,如Twofish的或RC4,它利用这样的键,然后编码结果的基础-64。

0

Base64不包含使用密钥进行加密的功能。您可以使用AES,DES等先加密,然后使用base64进行编码。

0

如果您需要使用密钥对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编码和一个键(而不是先键入输入然后编码)。别客气。

而且由于您问了一个代码示例,下面是Object Pascal中的一个快速示例,我写道。如果首先为最终字符串分配内存然后再写入内存,而不是在每次都重新分配内存的循环中连接字符串,则此代码可能会更快 - 但如果您需要,您可以自己弄清楚为更好的性能:


const 
     C_ALPHABIG  = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
     C_ALPHASMALL = 'abcdefghijklmnopqrstuvwxyz'; 
     C_ALPHA   = C_ALPHABIG+C_ALPHASMALL; 
     C_DIGITS  = ''; 
     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 - 这是符号查找加扰器,它可以移动符号索引向右或向左移动。我在编码器中正确使用,并保留在编码器中,但完全取决于您。 如果您跳过Encode64Decode64函数(或者如果您传递空字符串键)中的键参数,那么您将最终得到默认的Base64编码/解码。

此外,此编码器不会将填充(“=”字符)附加到base64编码的字符串。除非您的解码器以严格模式工作(此编码器不是),否则不需要填充进行解码 - 但是,您可以自己弄清楚。

相关问题