FromBase64Transform truncating string

C

Cole Shelton

Hi all,

I am simply trying to do a transform to a base 64 string and back, but for
some reason, the FromBase64Transform is return less bytes than it should. I
have verified that the base64 string is the same length as the one that was
output from ToBase64Transform. I also turned on DoNotIgnoreWhitespaces.
Any ideas?? Here is the code:

// ToBase64
ASCIIEncoding enc = new ASCIIEncoding();
byte[] valBytes = enc.GetBytes(val);
MemoryStream mstr = new MemoryStream();

CryptoStream str = new CryptoStream(mstr,new
ToBase64Transform(),CryptoStreamMode.Write);
str.Write(valBytes,0,valBytes.Length);

byte[] buf = mstr.ToArray();
return Convert.ToBase64String(buf);

// From Base64
byte[] valBytes = Convert.FromBase64String(val);
MemoryStream mstr = new MemoryStream();

CryptoStream str = new CryptoStream(mstr,new
FromBase64Transform(FromBase64TransformMode.DoNotIgnoreWhiteSpaces),CryptoSt
reamMode.Write);
str.Write(valBytes,0,valBytes.Length);

byte[] buf = mstr.ToArray();
ASCIIEncoding enc = new ASCIIEncoding();
return enc.GetString(buf);
 
T

Tim Smelser

I am simply trying to do a transform to a base 64 string and back, but for
some reason, the FromBase64Transform is return less bytes than it should. I
have verified that the base64 string is the same length as the one that was
output from ToBase64Transform. I also turned on DoNotIgnoreWhitespaces.
Any ideas?? Here is the code:
// ToBase64
ASCIIEncoding enc = new ASCIIEncoding();
byte[] valBytes = enc.GetBytes(val);
MemoryStream mstr = new MemoryStream();
CryptoStream str = new CryptoStream(mstr,new
ToBase64Transform(),CryptoStreamMode.Write);
str.Write(valBytes,0,valBytes.Length);
byte[] buf = mstr.ToArray();
return Convert.ToBase64String(buf);
// From Base64
byte[] valBytes = Convert.FromBase64String(val);
MemoryStream mstr = new MemoryStream();
CryptoStream str = new CryptoStream(mstr,new
FromBase64Transform(FromBase64TransformMode.DoNotIgnoreWhiteSpaces),CryptoSt
reamMode.Write);
str.Write(valBytes,0,valBytes.Length);
byte[] buf = mstr.ToArray();
ASCIIEncoding enc = new ASCIIEncoding();
return enc.GetString(buf);

If all you want to do is switch back and forth between strings, might
something like this work for you?

public static string ToBase64(string asciiText) {
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(asciiText);
return Convert.ToBase64String(buffer);
}

public static string FromBase64(string base64Text) {
byte[] buffer = Convert.FromBase64String(base64Text);
return ASCIIEncoding.ASCII.GetString(buffer);
}

HTH,
Tim
 
C

Cole Shelton

Thanks Tim, that worked. I had tried something similar, but got an error.
Must've been doing something wrong.

Cole

Tim Smelser said:
I am simply trying to do a transform to a base 64 string and back, but for
some reason, the FromBase64Transform is return less bytes than it should. I
have verified that the base64 string is the same length as the one that was
output from ToBase64Transform. I also turned on DoNotIgnoreWhitespaces.
Any ideas?? Here is the code:
// ToBase64
ASCIIEncoding enc = new ASCIIEncoding();
byte[] valBytes = enc.GetBytes(val);
MemoryStream mstr = new MemoryStream();
CryptoStream str = new CryptoStream(mstr,new
ToBase64Transform(),CryptoStreamMode.Write);
str.Write(valBytes,0,valBytes.Length);
byte[] buf = mstr.ToArray();
return Convert.ToBase64String(buf);
// From Base64
byte[] valBytes = Convert.FromBase64String(val);
MemoryStream mstr = new MemoryStream();
CryptoStream str = new CryptoStream(mstr,new
FromBase64Transform(FromBase64TransformMode.DoNotIgnoreWhiteSpaces),CryptoSt
reamMode.Write);
str.Write(valBytes,0,valBytes.Length);
byte[] buf = mstr.ToArray();
ASCIIEncoding enc = new ASCIIEncoding();
return enc.GetString(buf);

If all you want to do is switch back and forth between strings, might
something like this work for you?

public static string ToBase64(string asciiText) {
byte[] buffer = ASCIIEncoding.ASCII.GetBytes(asciiText);
return Convert.ToBase64String(buffer);
}

public static string FromBase64(string base64Text) {
byte[] buffer = Convert.FromBase64String(base64Text);
return ASCIIEncoding.ASCII.GetString(buffer);
}

HTH,
Tim
 

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