2011-06-14 38 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編碼的字符串。除非您的解碼器以嚴格模式工作(此編碼器不是),否則不需要填充進行解碼 - 但是,您可以自己弄清楚。

相關問題