J
James
Hello,
I'm having an issue that a previously posted on but after re-reading that
post I realize its a mess and it would be a real pain for someone to lend a
hand based on that.. so here is a much more direct question:
could anyone tell just how these .net base64 functions differ from the
vbscript base64 functions? When using them if I encode the text 'test' I get
the same result from them all, however, if I first RC4 encrypt the text
'test' and then encode that, the results differ between the vbscript and
..net functions?
both the .net functions and vbscript code is here:
START .net functions used------------------------
public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes =
System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
public string DecodeFrom64(string encodedData)
{
byte[] encodedDataAsBytes =
System.Convert.FromBase64String(encodedData);
string returnValue =
System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
}
OR
private string Base64Encode(string sString)
{
byte[] sString_bytes = UnicodeEncoding.UTF8.GetBytes(sString);
return Convert.ToBase64String(sString_bytes);
}
private string Base64Decode(string sBase64String)
{
byte[] sBase64String_bytes = Convert.FromBase64String(sBase64String);
return UnicodeEncoding.UTF8.GetString(sBase64String_bytes);
}
END: .net functions used -------------------------------
'---------------------------------------------------------------------------- 'START: MS's base64 functions (MDT 2008 update 1, fromztiutility.vbs)------------ '----------------------------------------------------------------------------Const BASE64_TABLE ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"Function SafeAsc ( sPlainText, pos ) if VarType(sPlainText) = (vbArray or vbByte) then SafeAsc = cint(midb(sPlainText, pos + 1 , 1)) elseif (pos + 1) <= len(sPlainText) then SafeAsc = asc(mid(sPlainText, pos + 1, 1)) else SafeAsc = 0 end ifEnd FunctionFunction SafeEnc( n, x ) Const BASE64_TABLE ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" SafeEnc = mid(BASE64_TABLE, ((n\(2^x)) and 63) + 1,1)End FunctionFunction base64Encode( sPlainText ) Dim i, n if 0 < len(sPlainText) then for i = 0 to len(sPlainText) - 1 step 3 ' Add a new line ... if i > 0 and i mod 57 = 0 then base64Encode = base64Encode & vbNewLine end if ' three 8-bit characters become one 24-bit number n = (SafeAsc(sPlainText,i)*&h10000 +SafeAsc(sPlainText,i+1)*&h100 + SafeAsc(sPlainText,i+2)) ' the 24-bit number becomes four 6-bit numbers base64Encode = base64Encode & SafeEnc( n, 18 ) & SafeEnc( n,12 ) & SafeEnc( n, 6 ) & SafeEnc( n, 0 ) next end if ' Pad Text at End of String n = (3-(len(sPlainText)mod 3)) mod 3 base64Encode = left ( base64Encode, len(base64Encode) - n ) + string( n,"=" )End FunctionFunction SafeDecode( s, i, x ) Const BASE64_TABLE ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" SafeDecode = ( InStr(1, BASE64_TABLE, mid(s,i,1), vbBinaryCompare) - 1) *(2 ^ x)End FunctionFunction base64Decode( sEncodedText ) Dim sEncText Dim regex Dim p, i, n ' Remove all non base64 text set regex = new RegExp regex.pattern = "[^=" & BASE64_TABLE & "]" regex.global = true sEncText = regex.Replace(sEncodedText,"") sEncText = replace( sEncText, vbLF, "") sEncText = replace( sEncText, vbCR, "") ' Verify String is in Base64 format (multiple of 4 chars) if len(sEncText) mod 4 <> 0 then oLogging.CreateEntry "OSDBitLockerStartupKey is not a valid string(not Base64 Format)", LogTypeError exit function end if if right(sEncText,2) = "==" then p = 2 elseif right(sEncText,1) = "=" then p = 1 end if sEncText = left(sEncText,len(sEncText)-p) & string(p,"A") for i = 1 to len(sEncText) step 4 ' Convert four 6-bit numbers into one 24 bit value n = SafeDecode(sEncText,i+3,0) + SafeDecode(sEncText,i+2,6) +SafeDecode(sEncText,i+1,12) + SafeDecode(sEncText,i+0,18) ' Convert the 24-bit value back into three 8-bit values. base64Decode = base64Decode & chr( (n \ (2^16)) and 255 ) & chr((n \ (2^8)) and 255 ) & chr( n and 255 ) next ' Trim off any excess space. base64Decode = left(base64Decode,len(base64Decode)-p)End Function '------------------------------------------------------------------------------ 'END: MS's base64functions --------------------------------------------------- '------------------------------------------------------------------------------
I'm having an issue that a previously posted on but after re-reading that
post I realize its a mess and it would be a real pain for someone to lend a
hand based on that.. so here is a much more direct question:
could anyone tell just how these .net base64 functions differ from the
vbscript base64 functions? When using them if I encode the text 'test' I get
the same result from them all, however, if I first RC4 encrypt the text
'test' and then encode that, the results differ between the vbscript and
..net functions?
both the .net functions and vbscript code is here:
START .net functions used------------------------
public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes =
System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
public string DecodeFrom64(string encodedData)
{
byte[] encodedDataAsBytes =
System.Convert.FromBase64String(encodedData);
string returnValue =
System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
}
OR
private string Base64Encode(string sString)
{
byte[] sString_bytes = UnicodeEncoding.UTF8.GetBytes(sString);
return Convert.ToBase64String(sString_bytes);
}
private string Base64Decode(string sBase64String)
{
byte[] sBase64String_bytes = Convert.FromBase64String(sBase64String);
return UnicodeEncoding.UTF8.GetString(sBase64String_bytes);
}
END: .net functions used -------------------------------
'---------------------------------------------------------------------------- 'START: MS's base64 functions (MDT 2008 update 1, fromztiutility.vbs)------------ '----------------------------------------------------------------------------Const BASE64_TABLE ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"Function SafeAsc ( sPlainText, pos ) if VarType(sPlainText) = (vbArray or vbByte) then SafeAsc = cint(midb(sPlainText, pos + 1 , 1)) elseif (pos + 1) <= len(sPlainText) then SafeAsc = asc(mid(sPlainText, pos + 1, 1)) else SafeAsc = 0 end ifEnd FunctionFunction SafeEnc( n, x ) Const BASE64_TABLE ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" SafeEnc = mid(BASE64_TABLE, ((n\(2^x)) and 63) + 1,1)End FunctionFunction base64Encode( sPlainText ) Dim i, n if 0 < len(sPlainText) then for i = 0 to len(sPlainText) - 1 step 3 ' Add a new line ... if i > 0 and i mod 57 = 0 then base64Encode = base64Encode & vbNewLine end if ' three 8-bit characters become one 24-bit number n = (SafeAsc(sPlainText,i)*&h10000 +SafeAsc(sPlainText,i+1)*&h100 + SafeAsc(sPlainText,i+2)) ' the 24-bit number becomes four 6-bit numbers base64Encode = base64Encode & SafeEnc( n, 18 ) & SafeEnc( n,12 ) & SafeEnc( n, 6 ) & SafeEnc( n, 0 ) next end if ' Pad Text at End of String n = (3-(len(sPlainText)mod 3)) mod 3 base64Encode = left ( base64Encode, len(base64Encode) - n ) + string( n,"=" )End FunctionFunction SafeDecode( s, i, x ) Const BASE64_TABLE ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" SafeDecode = ( InStr(1, BASE64_TABLE, mid(s,i,1), vbBinaryCompare) - 1) *(2 ^ x)End FunctionFunction base64Decode( sEncodedText ) Dim sEncText Dim regex Dim p, i, n ' Remove all non base64 text set regex = new RegExp regex.pattern = "[^=" & BASE64_TABLE & "]" regex.global = true sEncText = regex.Replace(sEncodedText,"") sEncText = replace( sEncText, vbLF, "") sEncText = replace( sEncText, vbCR, "") ' Verify String is in Base64 format (multiple of 4 chars) if len(sEncText) mod 4 <> 0 then oLogging.CreateEntry "OSDBitLockerStartupKey is not a valid string(not Base64 Format)", LogTypeError exit function end if if right(sEncText,2) = "==" then p = 2 elseif right(sEncText,1) = "=" then p = 1 end if sEncText = left(sEncText,len(sEncText)-p) & string(p,"A") for i = 1 to len(sEncText) step 4 ' Convert four 6-bit numbers into one 24 bit value n = SafeDecode(sEncText,i+3,0) + SafeDecode(sEncText,i+2,6) +SafeDecode(sEncText,i+1,12) + SafeDecode(sEncText,i+0,18) ' Convert the 24-bit value back into three 8-bit values. base64Decode = base64Decode & chr( (n \ (2^16)) and 255 ) & chr((n \ (2^8)) and 255 ) & chr( n and 255 ) next ' Trim off any excess space. base64Decode = left(base64Decode,len(base64Decode)-p)End Function '------------------------------------------------------------------------------ 'END: MS's base64functions --------------------------------------------------- '------------------------------------------------------------------------------