嗨,我有数据和密钥(都是字符串)。数据需要使用Base64的密钥进行编码。有人可以给我一个示例代码。Java:Base64使用密钥对字符串进行编码
回答
Base64不适用于'使用密钥编码'。它只是一种编码方案:您可以使用Base64来加密和解密字符串,而不需要额外的。这只是非常(非常)基本的安全用法。
+1,但它可能是OP希望使用某个“密钥”(无论他的意思是:来自PKCS的公钥还是对称密钥等)进行编码,然后使用Base64对结果进行编码。 – SyntaxT3rr0r 2011-06-14 20:20:05
您可以使用apache commons Codec库的Base64
类。这是homepage它和download page。
你可以用你的密钥对数据进行异或运算,然后用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使用一些功能从某处
你可以使用一个对称二元加密算法,如Twofish的或RC4,它利用这样的键,然后编码结果的基础-64。
Base64不包含使用密钥进行加密的功能。您可以使用AES,DES等先加密,然后使用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 = '';
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函数(或者如果您传递空字符串键)中的键参数,那么您将最终得到默认的Base64编码/解码。
此外,此编码器不会将填充(“=”字符)附加到base64编码的字符串。除非您的解码器以严格模式工作(此编码器不是),否则不需要填充进行解码 - 但是,您可以自己弄清楚。
- 1. 生成RSA密钥对并将私钥编码为字符串
- 2. 仅使用java中的密码加密对字符串字符进行加密
- 3. C#使用换行符对文本字符串进行编码
- 4. 如何在Python中使用PKCS12对RSA密钥进行编码?
- 5. 对字符进行编码
- 6. PyCrypto:使用RSA非对称密钥编码中文字符
- 7. 对使用HTTP请求发送的字符串进行编码?
- 8. 如何在Android中使用Base64对字符串进行编码?
- 9. 使用正则表达式对URL字符串进行编码
- 10. 使用密钥文件进行加密,使用密码进行解密
- 11. 如何使用对称密钥加密连接字符串
- 12. 使用gpg对加密密钥进行数字签名
- 13. 使用RSA公钥加密字符串
- 14. 可以使用编码的字符串和字符串发现加密密钥吗?
- 15. 编码字符串为34字节十六进制SQLite密码
- 16. 如何对空字符串进行编码/解码
- 17. 在dotnet核心中使用密码和salt对字符串进行编码和解码
- 18. 如何Base64使用shell命令对MD5二进制字符串进行编码?
- 19. 使用字符串对电子邮件地址进行编码和解码
- 20. 通过任意Monoid的密钥对组值进行编码
- 21. JavaScript - 使用特殊字符对对象进行编码
- 22. AWS使用什么密码/密钥进行AES-256加密?
- 23. Swift:如何使用枚举作为密钥对哈希映射进行编码?
- 24. 使用ansible对加密AWS .pem密钥文件进行加密
- 25. 使用与密钥相同的字符串加密字符串是否安全?
- 26. 使用主密钥进行Python加密
- 27. 如何使用私钥加密字符串并使用公钥解密?
- 28. 使用Lodash获取密钥只使用部分密钥字符串
- 29. C#使用字符串进行编码以查找全名
- 30. 如何使用硬编码字符串进行python gql查询?
一个不base64编码与“钥匙”。它(base64)是*编码*方案,而不是*加密*算法。这就是说,我很困惑,真正的目标是 - 也许标题是非常糟糕的选择。 – 2011-06-14 18:00:12