嗨,我有数据和一个键(两个字符串)。数据需要使用使用 Base64 的密钥进行编码。有人可以给我一个示例代码。
7 回答
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));
}
然后您使用某处的某些功能将“输出”编码为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 编码(而不是先键入输入然后编码)。别客气。
由于您要求提供代码示例,以下是我编写的 Object 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 编码字符串。解码不需要填充,除非您的解码器在严格模式下工作(此编码器不是) - 但是,您可以自己弄清楚。
您可以使用对称二进制加密算法,例如使用此类密钥的 Twofish 或 RC4,然后将结果编码为 base-64。
Base64 不包含使用密钥加密的功能。您可以先使用 AES、DES 等进行加密,然后使用 base64 进行编码。
Base64 是纯编码;您不能使用 base64 进行加密或解密,因为根据定义加密是使用密钥进行编码,并且由于您不能使用带有 base64 的密钥,因此它不能是任何类型的加密。
Base64 是编码,用于将数据转换为任何人都可以反转的新类型。你会听到人们告诉你这是为了混淆等,不是的;这不是编码的目的。
因此,直接针对您的问题,base64 不能接受密钥;它只能编码,并且编码不使用密钥。有一些加密机制,比如 XOR 是最简单的,可以接受密钥并进行加密。
编码+密钥=加密编码=将数据转换为任何人都可以反转的另一种类型,不使用密钥,永远散列=数据的单向数学函数来“指纹”您的数据。它不能被逆转,它用于验证数据没有改变。
这种混淆的根源是我们用“编码”这个词来表示一个特定的东西,而不是加密,我们也用“编码”作为加密必须做的一个过程。使用同一个词来解释两个相关但不同的事情会造成很多混乱。