How to fill byte[] with specific character or string in android - android

I am reading a PDF file (as bytes) using this code
byte[] bytes=FileUtils.readFileToByteArray(pdf);
i want to convert this byte[] to base64 string and send it to a client.
I am getting String value as [B#42109d30.
And I want to fill byte[] with the same value [B#42109d30.
I tried to fetch each character of string into a Byte and then assign this Byte value to byte[] , but even then the final value of byte[] changes.
String getbase64 = "[B#42109d30";
String a = String.valueOf((getbase64.charAt(i)));
if (a.equals("#") || a.equals("d") ) {
bytes[i]=0;
} else {
b = Byte.valueOf(""+a);
bt = b;
bytes[i] = bt;
Log.d("bytes", bytes.toString());
}
Please help,thank you

You can convert string to byte[] and vice versa this way :
byte[] bytes = text.getBytes("UTF-8");
String text = new String(bytes, "UTF-8");

Related

I cant able to convert the base64 encoded string to hex string in android?

I am using AES (Rijndael) encryption method to encrypt a string. The transformation used is ECB_PKCS5PADDING. It will return a base64 encoded string. I had decoded the string using base64 and converted to hex using a function. The code snippets are attached below.
private void encrptString()
{
sample = "<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String encrypt = Encoder.BuilderAES()
.message(sample)
.method(AES.Method.AES_ECB_PKCS5PADDING)
.key("xxxxxxxxxxxxxx")
.keySize(AES.Key.SIZE_128)
.encrypt();
System.out.println("encrypt"+encrypt);
String stringFromBase = new String(Base64.decode(encrypt,Base64.DEFAULT));
System.out.println("base64"+stringFromBase.trim());
String hex= null;
try {
hex = toHex(stringFromBase);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Toast.makeText(this, hex, Toast.LENGTH_SHORT).show();
System.out.println("hex"+hex.trim());
}
public String toHex(String arg) throws UnsupportedEncodingException {
return String.format("%040x", new BigInteger(1, arg.getBytes("UTF-8")));
}
base64 Input String:
O3Kmyftikp0lY/8SVWMiLFCn9sdQX7HauqdLZXcE+jkVcZH7kfPD/
jSLIC2wlP1iQTaiynoaW8A7L1INEYunkmCWT2eVwumwouHVT7d5Kq
/sqEcB9/9pyyAuSjrQxtCgJlHm7dlvt0lVEHD2pHnEAhZTRtuuy6b
8MNI7W4jLn+/ZNEqZxdJll26a52D7ceFv
Expected output:
3b72a6c9fb62929d2563ff125563222c50a7f6c7505fb1dabaa74b657704fa391571
91fb91f3c3fe348b202db094fd624136a2ca7a1a5bc03b2f520d118ba79260964f67
95c2e9b0a2e1d54fb7792aafeca84701f7ff69cb202e4a3ad0c6d0a02651e6edd96fb
749551070f6a479c402165346dbaecba6fc30d23b5b88cb9fe
fd9344a99c5d265976e9ae760fb71e16f
I am getting this output:
3b72efbfbdefbfbdefbfbd62efbfbdefbfbd2563efbfbd125563222c50efbfbdefbfbd
efbfbd505fefbfbddabaefbfbd4b657704efbfbd391571efbfbdefbfbdefbfbdefbfbd
efbfbd34efbfbd202defbfbdefbfbdefbfbd624136efbfbdefbfbd7a1a5befbfbd3b2f
520d11efbfbdefbfbdefbfbd60efbfbd4f67efbfbdefbfbde9b0a2efbfbdefbfbd4fef
bfbd792aefbfbdefbfbd4701efbfbdefbfbd69efbfbd202e4a3aefbfbdefbfbdd0a026
51efbfbdefbfbdefbfbd6fefbfbd49551070efbfbd79efbfbd02165346dbaecba6efbf
bd30efbfbd3b5befbfbdcb9fefbfbdefbfbd344aefbfbdefbfbdefbfbd65efbfbd6eef
bfbdefbfbd60efbfbd
I'd leave the decoded Base64 as a byte[] and encode that:
String b64 = "O3Kmyftikp0lY/8SVWMiLFCn9sdQX7HauqdLZXcE+jkVcZH7kfPD/ jSLIC2wlP1iQTaiynoaW8A7L1INEYunkmCWT2eVwumwouHVT7d5Kq /sqEcB9/9pyyAuSjrQxtCgJlHm7dlvt0lVEHD2pHnEAhZTRtuuy6b 8MNI7W4jLn+/ZNEqZxdJll26a52D7ceFv";
StringBuilder hexStringBuilder = new StringBuilder();
for (byte b : Base64.decode(b64, Base64.DEFAULT)) {
hexStringBuilder.append(String.format("%02x", b));
}
String hex = hexStringBuilder.toString()
You should base64-decode to a byte array (byte[]) and then hex encode the byte array to a string.

How to decrypt data sent from Android (Java) using Node.Js

I have run into a problem - an existing program uses the code below to encrypt data
public static String encryptData(String data, final String key) {
try {
byte[] kb=key.getBytes("utf-8");
byte[] dig= MessageDigest.getInstance("SHA-1").digest(kb);
byte[] kdig= Arrays.copyOf(dig, 24);
final SecretKeySpec secretKeySpec = new SecretKeySpec(kdig, "DESede");
final Cipher instance = Cipher.getInstance("DESede");
instance.init(ENCRYPT_MODE, secretKeySpec);
byte[] ecb=instance.doFinal(data.getBytes("utf-8"));
byte[] enb64=Base64.encode(ecb, Base64.DEFAULT);
return new String(enb64);
} catch (Exception ex) {
ErmsLogger.e(TAG, ex.getMessage());
return null;
}
}
I need to write code on Node.Js that decrypts this encrypted data. So far - I have come up with
function decryptData(encrpted_data,fn){
var hashedKey = crypto.createHash('sha1').update(config.dataPassword).digest('hex');
if(hashedKey.length < 48){
var num=48 - hashedKey.length;
for(var i=0;i < num; i++){
hashedKey +='0';
}
}
var key=Buffer.from(hashedKey, 'hex');
var decipher = crypto.createDecipher('des-ede', key);
decoded = decipher.update(encrpted_data, 'base64', 'utf8');
decoded += decipher.final('utf8');
log.debug(JSON.stringify(decoded));
return fn(decoded);
}
I keep on running into
Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
at Decipher.final (internal/crypto/cipher.js:104:26)
whenever I try to decrypt data sent from the android app.
the working decryption code on Android (Java) is
public static String decryptData(final String data, final String password) throws Exception {
MessageDigest instance=MessageDigest.getInstance("SHA-1");
byte[] passworddigest=instance.digest(password.getBytes("utf-8"));
byte[] key=Arrays.copyOf(passworddigest, 24);
final SecretKeySpec secretKeySpec = new SecretKeySpec(key, "DESede");
final byte[] decodeddata = Base64.decode(data.getBytes("utf-8"), Base64.DEFAULT);
final Cipher ciperInstance = Cipher.getInstance("DESede");
ciperInstance.init(DECRYPT_MODE, secretKeySpec);
byte[] res= ciperInstance.doFinal(decodeddata);
return new String(res, "UTF-8");
}
Could you assist me translate this in Node?
After a bit of research and hitting my head against it - I finally came up with something that works
function decryptData(encrpted_data,fn){
var encodeKey = crypto.createHash('sha1').update(config.dataPassword).digest('hex');
var cryptkey=Buffer.alloc(24);
encodeKey.copy(cryptkey);
var decipher = crypto.createDecipheriv('des-ede3', cryptkey,'');
decipher.setAutoPadding(true);
decoded = decipher.update(encrpted_data, 'base64', 'utf8');
decoded += decipher.final('utf8');
log.debug(JSON.stringify(decoded));
return fn(decoded);
}
the explanation -
crypto.createDecipher() - internally uses the MD5 hash for the key passed to it and so it really doesn't matter what key value you send in - it will be changed. So the best option is to use createDecipheriv which accepts a raw key. This allows you to hash your key outside before passing it in. Since I was not using any IV - I passed the value of ''.
In Java (Android) DESede is really TripleDES and the equivalent on on crypto is 'des-ede3' and not 'des-ede'.
I hope this saves someone a couple of hours.

IV for encryption conversion. Expected IV length of 16 but was 24

Can someone please tell me why I am getting this error message? Obviously it is some kind of conversion that I am missing
expected IV length of 16 but was 24
To call it I use
String encrypted = "E5ADDEB05D9D7B3925B7DE16B560D87C";
String sKey = "3985661DD71D591665BD39476636486B";
String sIv = "75E5FBB56AA78D05D246078A782553E1";
String decrypted = decrypt2(encrypted, sKey, sIv);
Log.i("--------------------------------> ", decrypted);
this is the procedure
public static String decrypt2(final String EncryptedMessageBase64,
final String symKeyHex,
final String sIvHex) {
final byte[] symKeyData = Base64.decode((symKeyHex),Base64.DEFAULT);
final byte[] byIvData = Base64.decode((sIvHex), Base64.DEFAULT);
final byte[] EncryptedMessage = Base64.decode(EncryptedMessageBase64, Base64.DEFAULT);
try
{
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
final int blockSize = cipher.getBlockSize();
final SecretKeySpec symKey = new SecretKeySpec(symKeyData, "AES");
Log.i("### iv size -------->", String.valueOf(blockSize));
Log.i("### symKeyHex -------->", symKeyHex);
Log.i("### sIvHex -------->", sIvHex);
Log.i("### blockSize -------->", String.valueOf(blockSize));
final IvParameterSpec iv = new IvParameterSpec(byIvData);
final byte[] encryptedMessage = new byte[EncryptedMessage.length];
cipher.init(Cipher.DECRYPT_MODE, symKey, iv);
This is the output
### iv size -------->: 16
### symKeyHex -------->: 3985661DD71D591665BD39476636486B
### sIvHex -------->: 75E5FBB56AA78D05D246078A782553E1
### blockSize -------->: 16
error: expected IV length of 16 but was 24
You are doing Base64 decoding on the key and IV but they are hex encoded, you need to do hex decoding to binary.
symKeyHex and sIvHex are very clearly hex encoded values and EncryptedMessageBase64 is clearly Base64 encoded.
Depending on the libraries you have included in your project one possibility is:
final byte[] symKeyData = (byte[]) new Hex().decode(symKeyHex);
final byte[] byIvData = (byte[]) new Hex().decode(sIvHex);
More: Base64 encoding represents 3 binary bytes as 4 ASCII characters. Hexadecimal encoding represents 1 binary byte as 2 ASCII characters.

NSNonLossyASCIIStringEncoding equivalent for Android

I got to port some chat code from iOS to Android. Before sending the chat message to the socket, the iOS code uses the NSNonLossyASCIIStringEncoding class as parameter of the NSString::dataUsingEncoding.
How would you do it in Android? Same question about the opposite decoding.
Without doing that, for instance, the line breaks disappear in the message received on the other mobile.
Code on iOS:
NSData *data1 = [myStringTosend dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding] autorelease];
And decoding:
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
So far (and not correct), encoding on the Android side:
OutputStream os = socket.getOutputStream();
os.write(request.getBytes("UTF-8"));
os.flush();
And decoding:
while ((bytesRead = is.read(buffer, 0, BUFFER_SIZE)) >= 0) {
if (bytesRead > 0) response.append(new String(buffer, 0, bytesRead, "UTF-8"));
if (bytesRead < BUFFER_SIZE) break;
}
#portforwardpodcast is absolutely correct that you should, if possible, avoid ASCII encoding your utf8 and instead set up your stack to handle/store utf8 directly. That said, if you don't have the ability to change the behavior, the following code may be helpful.
While there's no published explanation of how NSNonLossyASCIIStringEncoding works, based on its output it looks like:
Bytes in the extended ASCII range (decimal values 128 - 255) are escaped using an octal encoding (e.g. ñ with decimal value 241 -> \361)
Non-ASCII code points are escaped in two byte chunks using a hex encoding (e.g. 😥 which takes up 32 bits with decimal value 128549 -> \ud83d\ude25)
So to encode:
public static String encodeToNonLossyAscii(String original) {
Charset asciiCharset = Charset.forName("US-ASCII");
if (asciiCharset.newEncoder().canEncode(original)) {
return original;
}
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < original.length(); i++) {
char c = original.charAt(i);
if (c < 128) {
stringBuffer.append(c);
} else if (c < 256) {
String octal = Integer.toOctalString(c);
stringBuffer.append("\\");
stringBuffer.append(octal);
} else {
String hex = Integer.toHexString(c);
stringBuffer.append("\\u");
stringBuffer.append(hex);
}
}
return stringBuffer.toString();
}
And to decode (this can be made more efficient by parsing the two types of encodings in lock step, rather as two separate passes):
private static final Pattern UNICODE_HEX_PATTERN = Pattern.compile("\\\\u([0-9A-Fa-f]{4})");
private static final Pattern UNICODE_OCT_PATTERN = Pattern.compile("\\\\([0-7]{3})");
public static String decodeFromNonLossyAscii(String original) {
Matcher matcher = UNICODE_HEX_PATTERN.matcher(original);
StringBuffer charBuffer = new StringBuffer(original.length());
while (matcher.find()) {
String match = matcher.group(1);
char unicodeChar = (char) Integer.parseInt(match, 16);
matcher.appendReplacement(charBuffer, Character.toString(unicodeChar));
}
matcher.appendTail(charBuffer);
String parsedUnicode = charBuffer.toString();
matcher = UNICODE_OCT_PATTERN.matcher(parsedUnicode);
charBuffer = new StringBuffer(parsedUnicode.length());
while (matcher.find()) {
String match = matcher.group(1);
char unicodeChar = (char) Integer.parseInt(match, 8);
matcher.appendReplacement(charBuffer, Character.toString(unicodeChar));
}
matcher.appendTail(charBuffer);
return charBuffer.toString();
}
Don't use NSNonLossyASCIIStringEncoding, use utf-8 encoding. I just solved this problem myself on ios+android+java spring backend, and it took me around 4 full days to figure everything out. Android can't display emojis, but this gives me full character support in almost all (or all not sure) languages. Here are the articles that helped me:
Must Read: http://blog.manbolo.com/2012/10/29/supporting-new-emojis-on-ios-6 http://blog.manbolo.com/2011/12/12/supporting-ios-5-new-emoji-encoding
See the hex bytes of a string inside the DB: How can I see raw bytes stored in a MySQL column?
Details about how to setup MySQL: http://technovergence-en.blogspot.com/2012/03/mysql-from-utf8-to-utf8mb4.html
In depth FAQ of utf8- http://www.unicode.org/faq/utf_bom.html#utf8-4
Details about the difference from notation: \ud83d\udc7d and hex value in memory: 0xF09F91BD http://en.wikipedia.org/wiki/UTF-8#Description
Use this to copy and paste characters in to see real hex byte values (works for emojis): http://perishablepress.com/tools/utf8-hex/index.php
Get Spring to support utf8 in urls (for GET params) http://forum.springsource.org/showthread.php?93728-RequestParam-doesn-t-seem-to-be-decoded Get Parameter Encoding http://forum.springsource.org/showthread.php?112181-Unable-to-Override-the-Spring-MVC-URL-decoding-which-uses-default-quot-ISO-8859-1-quot
My answer code is equivalent to IOS NSNonLossyASCIIStringEncoding for Android.
In your gradle put below depandancy.
compile 'org.apache.commons:commons-lang3:3.4'
then Put method to your Utils Class Like this
public static String encode(String s)
{
return StringEscapeUtils.escapeJava(s);
}
public static String decode(String s)
{
return StringEscapeUtils.unescapeJava(s);
}
then Simply call this method where you want to encode string or decode String like this
//for encode
String stencode = Utils.encode("mystring");
//for decode
String stdecode = Utils.decode("mystring")

How can I Zip and Unzip a string using GZIPOutputStream that is compatible with .Net?

I need an example for compressing a string using GZip in android. I want to send a string like "hello" to the method and get the following zipped string:
BQAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyLmeVlW/w+GphA2BQAAAA==
Then I need to decompress it. Can anybody give me an example and complete the following methods?
private String compressString(String input) {
//...
}
private String decompressString(String input) {
//...
}
Thanks,
update
According to scessor's answer, Now I have the following 4 methods. Android and .net compress and decompress methods. These methods are compatible with each other except in one case. I mean they are compatible in the first 3 states but incompatible in the 4th state:
state 1) Android.compress <-> Android.decompress: (OK)
state 2) Net.compress <-> Net.decompress: (OK)
state 3) Net.compress -> Android.decompress: (OK)
state 4) Android.compress -> .Net.decompress: (NOT OK)
can anybody solve it?
Android methods:
public static String compress(String str) throws IOException {
byte[] blockcopy = ByteBuffer
.allocate(4)
.order(java.nio.ByteOrder.LITTLE_ENDIAN)
.putInt(str.length())
.array();
ByteArrayOutputStream os = new ByteArrayOutputStream(str.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(str.getBytes());
gos.close();
os.close();
byte[] compressed = new byte[4 + os.toByteArray().length];
System.arraycopy(blockcopy, 0, compressed, 0, 4);
System.arraycopy(os.toByteArray(), 0, compressed, 4,
os.toByteArray().length);
return Base64.encode(compressed);
}
public static String decompress(String zipText) throws IOException {
byte[] compressed = Base64.decode(zipText);
if (compressed.length > 4)
{
GZIPInputStream gzipInputStream = new GZIPInputStream(
new ByteArrayInputStream(compressed, 4,
compressed.length - 4));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int value = 0; value != -1;) {
value = gzipInputStream.read();
if (value != -1) {
baos.write(value);
}
}
gzipInputStream.close();
baos.close();
String sReturn = new String(baos.toByteArray(), "UTF-8");
return sReturn;
}
else
{
return "";
}
}
.Net methods:
public static string compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String(gzBuffer);
}
public static string decompress(string compressedText)
{
byte[] gzBuffer = Convert.FromBase64String(compressedText);
using (MemoryStream ms = new MemoryStream())
{
int msgLength = BitConverter.ToInt32(gzBuffer, 0);
ms.Write(gzBuffer, 4, gzBuffer.Length - 4);
byte[] buffer = new byte[msgLength];
ms.Position = 0;
using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
{
zip.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
}
The GZIP methods:
public static byte[] compress(String string) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(string.getBytes());
gos.close();
byte[] compressed = os.toByteArray();
os.close();
return compressed;
}
public static String decompress(byte[] compressed) throws IOException {
final int BUFFER_SIZE = 32;
ByteArrayInputStream is = new ByteArrayInputStream(compressed);
GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
StringBuilder string = new StringBuilder();
byte[] data = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = gis.read(data)) != -1) {
string.append(new String(data, 0, bytesRead));
}
gis.close();
is.close();
return string.toString();
}
And a test:
final String text = "hello";
try {
byte[] compressed = compress(text);
for (byte character : compressed) {
Log.d("test", String.valueOf(character));
}
String decompressed = decompress(compressed);
Log.d("test", decompressed);
} catch (IOException e) {
e.printStackTrace();
}
=== Update ===
If you need .Net compability my code has to be changed a little:
public static byte[] compress(String string) throws IOException {
byte[] blockcopy = ByteBuffer
.allocate(4)
.order(java.nio.ByteOrder.LITTLE_ENDIAN)
.putInt(string.length())
.array();
ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(string.getBytes());
gos.close();
os.close();
byte[] compressed = new byte[4 + os.toByteArray().length];
System.arraycopy(blockcopy, 0, compressed, 0, 4);
System.arraycopy(os.toByteArray(), 0, compressed, 4, os.toByteArray().length);
return compressed;
}
public static String decompress(byte[] compressed) throws IOException {
final int BUFFER_SIZE = 32;
ByteArrayInputStream is = new ByteArrayInputStream(compressed, 4, compressed.length - 4);
GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
StringBuilder string = new StringBuilder();
byte[] data = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = gis.read(data)) != -1) {
string.append(new String(data, 0, bytesRead));
}
gis.close();
is.close();
return string.toString();
}
You can use the same test script.
Whatever it was that compressed "Hello" to BQAAAB+LC... is a particularly poor implementation of a gzipper. It expanded "Hello" far, far more than necessary, using a dynamic block instead of a static block in the deflate format. After removing the four-byte prefix to the gzip stream (which always starts with hex 1f 8b), "Hello" was expanded to 123 bytes. In the world of compression, that is considered a crime.
The Compress method that you are complaining about is working correctly and properly. It is generating a static block and a total output of 25 bytes. The gzip format has a ten-byte header and eight-byte trailer overhead, leaving the five-byte input having been coded in seven bytes. That's more like it.
Streams that are not compressible will be expanded, but it shouldn't be by much. The deflate format used by gzip will add five bytes to every 16K to 64K for incompressible data.
To get actual compression, in general you need to give the compressor much more to work with that five bytes, so that it can find repeated strings and biased statistics in compressible data. I understand that you were just doing tests with a short string. But in an actual application, you would never use a general-purpose compressor with such short strings, since it would always be better to just send the string.
In your Decompress() method, the first 4 bytes of the Base64 decoded input are skipped before passing to GZipInputStream. These bytes are found to be 05 00 00 00 in this particular case. So in the Compress() method, these bytes have to be put back in just before the Base64 encode.
If I do this, Compress() returns the following:
BQAAAB+LCAAAAAAAAADLSM3JyQcAhqYQNgUAAAA=
I know that this is not exactly the same as your expectation, which is:
BQAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyLmeVlW/w+GphA2BQAAAA==
But, if my result is plugged back into Decompress(), I think you'll still get "Hello". Try it. The difference may be due to the different compression level with which you got the original string.
So what are the mysterious prefixed bytes 05 00 00 00? According to this answer it may be the length of the compressed string so that the program knows how long the decompressed byte buffer should be. Still that does not tally in this case.
This is the modified code for compress():
public static String Compress(String text) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// TODO: Should be computed instead of being hard-coded
baos.write(new byte[]{0x05, 0, 0, 0}, 0, 4);
GZIPOutputStream gzos = new GZIPOutputStream(baos);
gzos.write(text.getBytes());
gzos.close();
return Base64.encode(baos.toByteArray());
}
Update:
The reason why the output strings in Android and your .NET code don't match is that the .NET GZip implementation does a faster compression (and thus larger output). This can be verified for sure by looking at the raw Base64 decoded byte values:
.NET:
1F8B 0800 0000 0000 0400 EDBD 0760 1C49
9625 262F 6DCA 7B7F 4AF5 4AD7 E074 A108
8060 1324 D890 4010 ECC1 88CD E692 EC1D
6947 2329 AB2A 81CA 6556 655D 6616 40CC
ED9D BCF7 DE7B EFBD F7DE 7BEF BDF7 BA3B
9D4E 27F7 DFFF 3F5C 6664 016C F6CE 4ADA
C99E 2180 AAC8 1F3F 7E7C 1F3F 22E6 7959
56FF 0F86 A610 3605 0000 00
My Android version:
1F8B 0800 0000 0000 0000 CB48 CDC9 C907
0086 A610 3605 0000 00
Now if we check the GZip File Format, we see that both the .NET and Android versions are mostly identical in the initial header and trailing CRC32 & Size fields. The only differences are in the below fields:
XFL = 04 (compressor used fastest algorithm) in the case of .NET, whereas it's 00 in Android
The actual compressed blocks
So it's clear from the XFL field that the .NET compression algorithm produces longer output.
Infact, when I creates a binary file with these raw data values and then uncompressed them using gunzip, both the .NET and Android versions gave exactly the same output as "hello".
So you don't have to bother about the differing results.
I tried your code in my project, and found a encoding bug in compress method on Android:
byte[] blockcopy = ByteBuffer
.allocate(4)
.order(java.nio.ByteOrder.LITTLE_ENDIAN)
.putInt(str.length())
.array();
ByteArrayOutputStream os = new ByteArrayOutputStream(str.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(str.getBytes());
on above code, u should use the corrected encoding, and fill the bytes length, not the string length:
byte[] data = str.getBytes("UTF-8");
byte[] blockcopy = ByteBuffer
.allocate(4)
.order(java.nio.ByteOrder.LITTLE_ENDIAN)
.putInt(data.length)
.array();
ByteArrayOutputStream os = new ByteArrayOutputStream( data.length );
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write( data );
I got crazy with this issue. At the end, in my case (.Net 4) it was not necessary to add this extra 4 bytes at the begining for the .Net compatibility.
It works simply like this:
Android Compress:
public static byte[] compress(String string) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(string.getBytes());
gos.close();
byte[] compressed = os.toByteArray();
os.close();
return compressed;
}
.Net Decompress
public static byte[] DecompressViD(byte[] gzip)
{
// Create a GZIP stream with decompression mode.
// ... Then create a buffer and write into while reading from the GZIP stream.
using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
OK, I hate chiming in when there are tons of existing answers, but unfortunately most of them are simply wrong for variety of reasons:
There are differences between GZIP algorithms within .NET Framework. If you are using .NET 4.5 most of the complaints you see in different answers simply don't apply to you (rather to those who use 2.0 or 3.5). If you go with "fixed" versions of code you'll actually mess-up compression/decompression.
Java uses unsigned byte[], .NET uses signed byte[]. This may cause problems during transport depending on how exactly you are transporting that byte[].
I've used Base64 to transport byte[] which can introduce even more problems. There are variety of other reasons, but let's skip further whining and get to the code...
If you are using .NET Framework 4.5 here is C# class you need (Base64 as a bonus):
public class CompressString
{
private static void CopyTo(Stream src, Stream dest)
{
byte[] bytes = new byte[4096];
int cnt;
while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0)
{
dest.Write(bytes, 0, cnt);
}
}
public static byte[] Zip(string str)
{
var bytes = Encoding.UTF8.GetBytes(str);
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(mso, CompressionMode.Compress))
{
//msi.CopyTo(gs);
CopyTo(msi, gs);
}
return mso.ToArray();
}
}
public static string Unzip(byte[] bytes)
{
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
//gs.CopyTo(mso);
CopyTo(gs, mso);
}
return Encoding.UTF8.GetString(mso.ToArray());
}
}
// Base64
public static string ZipBase64(string compress)
{
var bytes = Zip(compress);
var encoded = Convert.ToBase64String(bytes, Base64FormattingOptions.None);
return encoded;
}
public static string UnzipBase64(string compressRequest)
{
var bytes = Convert.FromBase64String(compressRequest);
var unziped = Unzip(bytes);
return unziped;
}
// Testing
public static bool TestZip(String stringToTest)
{
byte[] compressed = Zip(stringToTest);
Debug.WriteLine("Compressed to " + compressed.Length + " bytes");
String decompressed = Unzip(compressed);
Debug.WriteLine("Decompressed to: " + decompressed);
return stringToTest == decompressed;
}
}
And here is Android/Java class you need:
public class CompressString {
public static byte[] compress(String string) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(string.getBytes());
gos.close();
byte[] compressed = os.toByteArray();
os.close();
return compressed;
} catch (IOException ex) {
return null;
}
}
public static String decompress(byte[] compressed) {
try {
final int BUFFER_SIZE = 32;
ByteArrayInputStream is = new ByteArrayInputStream(compressed);
GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
byte[] data = new byte[BUFFER_SIZE];
int bytesRead;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((bytesRead = gis.read(data)) != -1) {
baos.write(data, 0, bytesRead);
}
gis.close();
is.close();
return baos.toString("UTF-8");
} catch (IOException ex) {
return null;
}
}
// Base64
public static String compressBase64(String strToCompress) {
byte[] compressed = compress(strToCompress);
String encoded = android.util.Base64.encodeToString(compressed, android.util.Base64.NO_WRAP);
return encoded;
}
public static String decompressBase64(String strEncoded) {
byte[] decoded = android.util.Base64.decode(strEncoded, android.util.Base64.NO_WRAP);
String decompressed = decompress(decoded);
return decompressed;
}
// test
public static boolean testCompression(String stringToTest) {
byte[] compressed = compress(stringToTest);
Log.d("compress-test", "Compressed to " + compressed.length + " bytes");
String decompressed = decompress(compressed);
Log.d("compress-test", "Decompressed to " + decompressed);
return stringToTest.equals(decompressed);
}
}
So, there you go - dependency free, 100% working compression Android/Java/C#/.NET classes. If you find string that's not working with .NET 4.5 (I've tried everything from "Hello world" to 1000 word short story) - let me know.
Android method decompress not ok
Android Compress -> OK:
public static byte[] compress(String string) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
GZIPOutputStream gos = new GZIPOutputStream(os);
gos.write(string.getBytes());
gos.close();
byte[] compressed = os.toByteArray();
os.close();
return compressed;
}
.Net Decompress -> OK:
public static byte[] DecompressViD(byte[] gzip)
{
// Create a GZIP stream with decompression mode.
// ... Then create a buffer and write into while reading from the GZIP stream.
using (GZipStream stream = new GZipStream(new MemoryStream(gzip), CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
.Net Compress -> OK:
public static string compress(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
return Convert.ToBase64String(compressed);
}
Android Decompress -> Not OK:
public static String decompress(String zipText) throws IOException {
byte[] compressed = Base64.decode(zipText);
GZIPInputStream os = new GZIPInputStream(new ByteArrayInputStream(compressed));
GZIPInputStream gzipInputStream = new GZIPInputStream(os);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int value = 0; value != -1;) {
value = gzipInputStream.read();
if (value != -1) {
baos.write(value);
}
}
gzipInputStream.close();
baos.close();
return new String(baos.toByteArray(), "UTF-8");
}
Here's a simple example to get you started.
public static void main(String[] args) throws IOException
{
byte[] buffer = new byte[4096];
StringBuilder sb = new StringBuilder();
//read file to compress
String read = readFile( "spanish.xml", Charset.defaultCharset());
if( read != null )
{
//compress file to output
FileOutputStream fos = new FileOutputStream("spanish-new.xml");
GZIPOutputStream gzos = new GZIPOutputStream(fos);
gzos.write( read.getBytes());
gzos.close();
//uncompress and read back
FileInputStream fis = new FileInputStream("spanish-new.xml");
GZIPInputStream gzis = new GZIPInputStream(fis);
int bytes = 0;
while ((bytes = gzis.read(buffer)) != -1) {
sb.append( new String( buffer ) );
}
}
}
static String readFile(String path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
This might be late but could be useful for someone,
I recently had a requirement to compress a string in C# Xamarin and decompress it in Android. Basically a Xamarin android app sends another Native Android app an intent with a compressed extra string. And the Android app has to decompress it before using it.
These are the methods that worked for me.
ANDROID DECOMPRESS
public static String decompress(String zipText) throws IOException {
int size = 0;
byte[] gzipBuff = Base64.decode(zipText,Base64.DEFAULT);
ByteArrayInputStream memstream = new ByteArrayInputStream(gzipBuff, 4,gzipBuff.length - 4);
GZIPInputStream gzin = new GZIPInputStream(memstream);
final int buffSize = 8192;
byte[] tempBuffer = new byte[buffSize];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((size = gzin.read(tempBuffer, 0, buffSize)) != -1) {
baos.write(tempBuffer, 0, size);
} byte[] buffer = baos.toByteArray();
baos.close(); return new String(buffer, StandardCharsets.UTF_8);
}
XAMARIN COMPRESS
public static string CompressString(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);
var memoryStream = new MemoryStream();
using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gZipStream.Write(buffer, 0, buffer.Length);
}
memoryStream.Position = 0;
var compressedData = new byte[memoryStream.Length];
memoryStream.Read(compressedData, 0, compressedData.Length);
var gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
return Convert.ToBase64String(gZipBuffer);
}
I do it so in Vb.net:
Public Function zipString(ByVal Text As String) As String
Dim res As String = ""
Try
Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(Text)
Dim ms As New MemoryStream()
Using zipStream As New System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, True)
zipStream.Write(buffer, 0, buffer.Length)
End Using
ms.Position = 0
Dim outStream As New MemoryStream()
Dim compressed As Byte() = New Byte(ms.Length - 1) {}
ms.Read(compressed, 0, compressed.Length)
Dim gzBuffer As Byte() = New Byte(compressed.Length + 3) {}
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length)
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4)
res = Convert.ToBase64String(gzBuffer)
Catch ex As Exception
Log("mdl.zipString: " & ex.Message)
End Try
Return res
End Function
Public Function unzipString(ByVal compressedText As String) As String
Dim res As String = ""
Try
Dim gzBuffer As Byte() = Convert.FromBase64String(compressedText)
Using ms As New MemoryStream()
Dim msgLength As Integer = BitConverter.ToInt32(gzBuffer, 0)
ms.Write(gzBuffer, 4, gzBuffer.Length - 4)
Dim buffer As Byte() = New Byte(msgLength - 1) {}
ms.Position = 0
Using zipStream As New System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress)
zipStream.Read(buffer, 0, buffer.Length)
End Using
res = System.Text.Encoding.UTF8.GetString(buffer, 0, buffer.Length)
End Using
Catch ex As Exception
Log("mdl.unzipString: " & ex.Message)
End Try
Return res
End Function

Categories

Resources