i want use GZip compression algorithme in PHP & C#.
Code in C#
byte[] bs1 = File.ReadAllBytes(inName);
memoryStream1 = new MemoryStream();
gZipStream = new GZipStream(memoryStream1, CompressionMode.Compress);
gZipStream.Write(bs1, 0, (int)bs1.Length);
gZipStream.Close();
Code in PHP
$data = file_get_contents(inName);
$gzip = gzencode($data);
I have try all gzip php functions and all compression level.
I dont have the same result, same result is needed for crc check.
Thanks for your help.
Source string : This is a test !
gzencode + base64 inphp : H4sIAAAAAAAACwvJyCxWAKJEhZLU4hIFRQDdPPQVEAAAAA==
gZipStream + base64 in C# : H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/It7Miyal/2Vpmzdt+rv+P9089BUQAAAA
Related
I am trying to convert the following code from ruby to C#:
open(file_path, ’rb’) do |doc|
response = RestClient::Request.execute :method => :post,
:url => "http://something.com",
:payload => {
:document => doc
}
my understanding is this will read a file in binary in ASCII-8BIT as per the following documentation:
"b" Binary file mode
Suppresses EOL <-> CRLF conversion on Windows. And
sets external encoding to ASCII-8BIT unless explicitly
specified.
So trying to replicate this example code in C# I have written:
string myString;
Encoding enc = Encoding.GetEncoding(????);
using (FileStream fs = new FileStream("D:\\sample1page.pdf", FileMode.Open))
using (BinaryReader br = new BinaryReader(fs, enc))
{
byte[] bin = br.ReadBytes(System.Convert.ToInt32(fs.Length));
myString = enc.GetString(bin);
}
So the question is what encoding number do I need to do markerd by ???? is to do the equivalent ASCII-8BIT that works in ruby
i have a C# application where i am using SharpZipLib to deflate a very long string and then send the data over to a PHP Service in a Base64 string of the deflated byte[].
For some reason, when trying to Inflate it on the PHP side, it is returning an error: 'gzinflate: data error'.
How to inflate a gzipped string in PHP?
Here is the C# code:
byte[] sIn = System.Text.UTF8Encoding.UTF8.GetBytes(data);
MemoryStream rawDataStream = new MemoryStream();
GZipOutputStream gzipOut = new GZipOutputStream(rawDataStream);
gzipOut.IsStreamOwner = false;
gzipOut.Write(sIn, 0, sIn.Length);
gzipOut.Close();
byte[] compressed = rawDataStream.ToArray();
// data sent to the php service
string b64 = Convert.ToBase64String(compressed);
PHP code:
$inflated = base64_decode($_POST['data']);
// crash here
$inflated = gzinflate($inflated);
Thanks in advance!
Can't really say why it fails for you with GZipOutStream though I'm guessing it is doing something else then just a pure deflate-compression. I changed your code to use DeflateStream from System.IO.Compression instead and then it worked like a charm.
byte[] sIn = UTF8Encoding.UTF8.GetBytes("testing some shit");
MemoryStream rawDataStream = new MemoryStream();
DeflateStream gzipOut = new DeflateStream(rawDataStream, CompressionMode.Compress);
gzipOut.Write(sIn, 0, sIn.Length);
gzipOut.Close();
byte[] compressed = rawDataStream.ToArray();
// data sent to the php service
string b64 = Convert.ToBase64String(compressed);
Edit
Since the question was about using compression for a Windows Phone project I tried using the DeflateStream from SharpCompress as well and it works just fine, you just have to change which namespace you are using, the classes are the same.
I have an xml file that I need to send to a REST server as a post. When I read the exact same file from c# and java the bytes do not match when they arrive at the server. The java ones fail with a 500 Internal Server Error while the c# one works perfectly. The server is c#.
The file in c# is read as follows:
using (ms = new MemoryStream())
{
string fullPath = #"c:\pathtofile\datalast.xml";
using (FileStream outStream = File.OpenRead(fullPath))
{
outStream.CopyTo(ms);
outStream.Flush();
}
ms.Position = 0;
var xmlDoc = new XmlDocument();
xmlDoc.Load(ms);
content = xmlDoc.OuterXml;
}
content is then sent to a call that uses an HttpWebResponse
The java (Android) code reads the file like this:
FileInputStream fis = app.openFileInput(DATA_LAST_FILE_NAME);
byte[] buffer = new byte[1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len;
while ((len = fis.read(buffer)) != -1)
{
outputStream.write(buffer, 0, len);
}
outputStream.close();
fis.close();
ByteArrayEntity data = new ByteArrayEntity(buffer);
data.setContentType("application/xml");
post.setEntity(data);
HttpResponse response = request.execute(post);
For the most part the arrays generated are identical. The only difference seems to be in the first 3 bytes. The c# byte array's first 3 values are:
239,187,191
The java ones are:
-17,-69,-65
What is happening here? What should I do?
Thanks,
\ ^ / i l l
Look at what you're doing here:
FileInputStream fis = app.openFileInput(DATA_LAST_FILE_NAME);
byte[] buffer = new byte[1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len;
while ((len = fis.read(buffer)) != -1)
{
outputStream.write(buffer, 0, len);
}
outputStream.close();
fis.close();
ByteArrayEntity data = new ByteArrayEntity(buffer);
You're creating the ByteArrayEntity from the buffer that you've used when reading the data. It's almost certainly not the right length (it will always be length 1024), and it may well not have all the data either.
You should be using the ByteArrayOutputStream you've been writing into, e.g.
ByteArrayEntity data = new ByteArrayEntity(outputStream.toByteArray());
(You should be closing fis in a finally block, by the way.)
EDIT: The values you've printed to the console are indeed just showing the differences between signed and unsigned representations. They have nothing to do with the reason the Java code is failing, which is due to the above problem, I believe. You should look at what's being sent over the wire in Wireshark - that'll show you what's really going on.
Take a look at this: http://en.wikipedia.org/wiki/Byte_order_mark
EDIT: The reason why java and C# are different is that when reading the bytes, C# is unsigned, and java is signed. Same binary values, however.
I am trying to use BouncyCastle to encrypt a file using the PKCS 7 file standard. Here is the code I have which outputs a p7m file. When I go to decrypt the file (using Entrust) I am prompted for my key store password, so it knows the file was encrypted for me using AES 128, but it cannot decrypt the body of the file. Something has to be going wrong on the encrypt.
byte[] fileContent = readFile(filename);
FileStream outStream = null;
Stream cryptoStream = null;
BinaryWriter binWriter = null;
try
{
CmsEnvelopedDataStreamGenerator dataGenerator = new CmsEnvelopedDataStreamGenerator();
dataGenerator.AddKeyTransRecipient(cert); //cert is the user's x509cert that i am encrypting for
outStream = new FileStream(filename + ".p7m", FileMode.Create);
cryptoStream = dataGenerator.Open(outStream, CmsEnvelopedGenerator.Aes128Cbc);
binWriter = new BinaryWriter(cryptoStream);
binWriter.Write(fileContent);
}
And when i try and decrypt the file using BouncyCastle I get this error when i pass the file contents to a CMSEnveloped Object:
IOException converting stream to byte array: Attempted to read past the end of the stream.
Any ideas whats going on here?
I used the EnvelopedCMS class to accomplish this.
http://msdn.microsoft.com/en-us/library/bb924575(VS.90).aspx
I have raw data of base64Binary.
string base64BinaryStr = "J9JbWFnZ......"
How can I make pdf file? I know it need some conversion. Please help me.
Step 1 is converting from your base64 string to a byte array:
byte[] bytes = Convert.FromBase64String(base64BinaryStr);
Step 2 is saving the byte array to disk:
System.IO.FileStream stream =
new FileStream(#"C:\file.pdf", FileMode.CreateNew);
System.IO.BinaryWriter writer =
new BinaryWriter(stream);
writer.Write(bytes, 0, bytes.Length);
writer.Close();
using (System.IO.FileStream stream = System.IO.File.Create("c:\\temp\\file.pdf"))
{
System.Byte[] byteArray = System.Convert.FromBase64String(base64BinaryStr);
stream.Write(byteArray, 0, byteArray.Length);
}
First convert the Bas64 string to byte[] and write it into a file.
byte[] bytes = Convert.FromBase64String(base64BinaryStr);
File.WriteAllBytes(#"FolderPath\pdfFileName.pdf", bytes );
This code does not write any file on the hard drive.
Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Length", base64Result.Length.ToString());
Response.AddHeader("Content-Disposition", "inline;");
Response.AddHeader("Cache-Control", "private, max-age=0, must-revalidate");
Response.AddHeader("Pragma", "public");
Response.BinaryWrite(Convert.FromBase64String(base64Result));
Note: the variable base64Result contains the Base64-String: "JVBERi0xLjMgCiXi48/TIAoxI..."
All you need to do is run it through any Base64 decoder which will take your data as a string and pass back an array of bytes. Then, simply write that file out with pdf in the file name.
Or, if you are streaming this back to a browser, simple write the bytes to the output stream, marking the appropriate mime-type in the headers.
Most languages either have built in methods for converted to/from Base64. Or a simple Google with your specific language will return numerous implementations you can use. The process of going back and forth to Base64 is pretty straightforward and can be implemented by even novice developers.
base64BinaryStr - from webservice SOAP message
byte[] bytes = Convert.FromBase64String(base64BinaryStr);