Error: Invalid character in a Base-64 string

S

shamsh

I'm getting a error as "Invalid character in a Base-64 string", whenever I
try to execute the statement:
byte[] encryptedData = Convert.FromBase64String(bstrDecryptText);

where bstrDecryptText =
"URgMuT2/EaPh0C8Tcf8A6g3Gj/$uzA7uxu4Og7EAoChCZSH0Eir8IdnS8Pir089P"

I read somewhere FromBase64String() has some issue related to size etc.
Can anyone please let me know the reason for the error.
 
A

Anthony Jones

shamsh said:
I'm getting a error as "Invalid character in a Base-64 string", whenever I
try to execute the statement:
byte[] encryptedData = Convert.FromBase64String(bstrDecryptText);

where bstrDecryptText =
"URgMuT2/EaPh0C8Tcf8A6g3Gj/$uzA7uxu4Og7EAoChCZSH0Eir8IdnS8Pir089P"

I read somewhere FromBase64String() has some issue related to size etc.
Can anyone please let me know the reason for the error.

$ is not a valid character in Base64. Its probably some kind of delimiter
between two different field values.
 
A

Alberto Poblacion

shamsh said:
I'm getting a error as "Invalid character in a Base-64 string", whenever I
try to execute the statement:
byte[] encryptedData = Convert.FromBase64String(bstrDecryptText);

where bstrDecryptText =
"URgMuT2/EaPh0C8Tcf8A6g3Gj/$uzA7uxu4Og7EAoChCZSH0Eir8IdnS8Pir089P"

Well, there *is* an invalid character in your string: the '$' symbol . A
base-64 string should only contain letters, numbers, and the symbols '+' and
'/'.
 
S

shamsh

Actually the string is generated and returned from a COM++(cpp code).In the
cpp to64frombits((unsigned char*)pszBase64, pbBuffer, dwBufferLen); is being
used to convert a binary data to unsigned char.
At .net end I have to convert that string back to binary before decrypting.

Is there a way to either modify something at cpp so that the encrypted
string doesn't have '$' within it,or any other way at .net to convert the
string to binary.
--
regards,
Shamsheer


Alberto Poblacion said:
shamsh said:
I'm getting a error as "Invalid character in a Base-64 string", whenever I
try to execute the statement:
byte[] encryptedData = Convert.FromBase64String(bstrDecryptText);

where bstrDecryptText =
"URgMuT2/EaPh0C8Tcf8A6g3Gj/$uzA7uxu4Og7EAoChCZSH0Eir8IdnS8Pir089P"

Well, there *is* an invalid character in your string: the '$' symbol . A
base-64 string should only contain letters, numbers, and the symbols '+' and
'/'.
 
A

Anthony Jones

shamsh said:
Actually the string is generated and returned from a COM++(cpp code).In
the
cpp to64frombits((unsigned char*)pszBase64, pbBuffer, dwBufferLen); is
being
used to convert a binary data to unsigned char.
At .net end I have to convert that string back to binary before
decrypting.

Is there a way to either modify something at cpp so that the encrypted
string doesn't have '$' within it,or any other way at .net to convert the
string to binary.

Assuming you are using a standard implementation of the to64frombits
function then its not that function which is introducing the $ character.

After to64frombits the pszBase64 buffer will contain a null terminated
base64 string. There is something else happening to that buffer afterwards
that is corrupting it.
 
S

shamsh

When I went through to64frombits,$ was being used.Hence I replaced the $ with
'+' and the resulting string was without '$'.Though the FromBase64String()
works fine,but still there seems to be an issue with the text while doing the
reverse i.e again converting back to bits.
Do you have any idea where can I get the dotnet equivalents of to64frombits
and from64tobits logic .If required I can provide the code for those
functions,but since those are part of base64.h ,they should be standard ones.
 
A

Anthony Jones

shamsh said:
When I went through to64frombits,$ was being used.Hence I replaced the $
with
'+' and the resulting string was without '$'.Though the FromBase64String()
works fine,but still there seems to be an issue with the text while doing
the
reverse i.e again converting back to bits.

What issue is that?
Do you have any idea where can I get the dotnet equivalents of
to64frombits
and from64tobits logic .If required I can provide the code for those
functions,but since those are part of base64.h ,they should be standard
ones.

The implementations of to64frombits that I have seen on the web all appear
to create the base64 stirng correctly, none needed adjusting to use +
instead of $ since $ would clearly be wrong. If you've had to fix the
implementation you had then what else might be wrong with it?

to64frombits creates a base64 string. The definition of such a string is
simple and deterministic. Any correct implementation of a base64 decode is
compatible with any correct implementation of base64 encode.

I know that .NETs decode works, I know that the standard to64frombits works.
Hence your problem lies either with 1) your implementation is even more
broken OR 2) something else is happening to the string so that it doesn't
get to .NETs decode with fidelity.
 
S

shamsh

Is it possible for you to provide me with a standard .cpp
"to64frombits(unsigned char *out, const unsigned char *in, int inlen)" and a
corresponding .net decode.
Since nothing looks wrong except '$' to '+' within current to64frombits.
Within calling function there is a statement after to64frombits() called :
*pbstrResultText = CComBSTR(pszBase64).Detach(); where pszBase64 is the
output from to64frombits.
I'm not sure, but I don't think it will disturb the output from to64frombits.
 
A

Arne Vajhøj

shamsh said:
Is it possible for you to provide me with a standard .cpp
"to64frombits(unsigned char *out, const unsigned char *in, int inlen)" and a
corresponding .net decode.
Since nothing looks wrong except '$' to '+' within current to64frombits.
Within calling function there is a statement after to64frombits() called :
*pbstrResultText = CComBSTR(pszBase64).Detach(); where pszBase64 is the
output from to64frombits.
I'm not sure, but I don't think it will disturb the output from to64frombits.

I wrote some C code back in the 90's.

They should be compatible with the standard .NET methods.

You may want to skip the linlen functionality though.

See code below.

Arne

===================================

struct sixbits { unsigned sb1:6;
unsigned sb2:6;
unsigned sb3:6;
unsigned sb4:6; };

union overlay { struct sixbits sixs;
char eigths[3]; };

char enc_b64vals[64] = { 'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/' };

int enc_deb64(char c)
{
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return c-'0'+52;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
return c-'A';
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
return c-'a'+26;
case '+':
return 62;
case '/':
return 63;
case '=':
return 0;
default:
return -1;
}
return -1;
}

void NormalToB64(char *s1,int l1,char *s2,int *l2)
{
union overlay Cvt;
char *p = s1;
int ltmp = (l1*8+5)/6;
int extra = 3-(ltmp+3)%4;
int linlen = 0;
(*l2) = 0;
// loop through string
for(int i=0;i<ltmp;i=i+4) {
// convert 8->6 bit
Cvt.eigths[2]=(((p-s1)<l1)?(*p):0);
p++;
Cvt.eigths[1]=(((p-s1)<l1)?(*p):0);
p++;
Cvt.eigths[0]=(((p-s1)<l1)?(*p):0);
p++;
s2[*l2]=enc_b64vals[Cvt.sixs.sb4];
(*l2)++;
s2[*l2]=enc_b64vals[Cvt.sixs.sb3];
(*l2)++;
s2[*l2]=enc_b64vals[Cvt.sixs.sb2];
(*l2)++;
s2[*l2]=enc_b64vals[Cvt.sixs.sb1];
(*l2)++;
// append newline if line too long
linlen=linlen+4;
if(linlen>75) {
s2[*l2]='\n';
(*l2)++;
linlen=0;
}
}
// append 0,1 or 2 "="
(*l2)=(*l2)-extra;
for(int j=0;j<extra;j++) {
s2[*l2]='=';
(*l2)++;
}
// append newline
s2[*l2]='\n';
(*l2)++;
return;
}

void B64ToNormal(char *s1,int l1,char *s2,int *l2)
{
union overlay Cvt;
char *p = s1;
int skip = 0;
(*l2) = 0;
// loop through string
while(p<(s1+l1)) {
// convert 6->8 bit
Cvt.sixs.sb4=enc_deb64(*p);
p++;
if((*p)=='=') skip++;
Cvt.sixs.sb3=enc_deb64(*p);
p++;
if((*p)=='=') skip++;
Cvt.sixs.sb2=enc_deb64(*p);
p++;
if((*p)=='=') skip++;
Cvt.sixs.sb1=enc_deb64(*p);
p++;
s2[*l2]=Cvt.eigths[2];
(*l2)++;
s2[*l2]=Cvt.eigths[1];
(*l2)++;
s2[*l2]=Cvt.eigths[0];
(*l2)++;
(*l2)=(*l2)-skip;
// skip newline
if((*p)=='\n') p++;
}
return;
}
 
A

Anthony Jones

shamsh said:
Is it possible for you to provide me with a standard .cpp
"to64frombits(unsigned char *out, const unsigned char *in, int inlen)" and
a
corresponding .net decode.
Since nothing looks wrong except '$' to '+' within current to64frombits.
Within calling function there is a statement after to64frombits() called :
*pbstrResultText = CComBSTR(pszBase64).Detach(); where pszBase64 is the
output from to64frombits.
I'm not sure, but I don't think it will disturb the output from
to64frombits.
http://kdeedu.sourcearchive.com/documentation/4.1.3/base64_8c-source.html

There are plenty of examples knocking about on the web. The above is one
such example
 
S

shamsh

The code for from64tobits within the link is exactly the same to what we have
in our code except for the condition:
while
(*in && *in != '\r' && digit4 != '=');
but still the .net DECODE Convert.frombase64 doesn't work properly.
Do you have a .net decoder corresponding to the to64frombits.
 
A

Anthony Jones

shamsh said:
The code for from64tobits within the link is exactly the same to what we
have
in our code except for the condition:
while
(*in && *in != '\r' && digit4 != '=');
but still the .net DECODE Convert.frombase64 doesn't work properly.
Do you have a .net decoder corresponding to the to64frombits.

The System.Convert.FromBase64String method in .NET will decode the string
generated by the implementation of to64frombits found in the link I supplied
to you and corresponds with the from64tobits function in that link.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top