Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Base 64 and GZIP question

Status
Not open for further replies.

FreshmenProgrammer

Programmer
Jan 16, 2006
42
CA
Hello, I would first like to say Thanks in advance for reading this and trying to help me. Ive been stuck for a good 4 hours. I have an app that has to GZIP a token then encrypt it and then Base 64 it and another app to do the opposite.

here is the code that GZIP's it then encrypts it and then base 64.
Code:
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream g = new GZIPOutputStream(out);
StringBuffer buf = new StringBuffer();
     
g.write(buf.toString().getBytes());
g.finish();

Base64 b = new Base64();
String eDataEncrypted = tripleDes.encrypt3(out.toString(), KEY);
    
String eData = new String(b.encode(eDataEncrypted.getBytes()));
Here is the app that take it apart but thats not working either

Code:
Base64 bb = new Base64();
String  deCode = new String(bb.decode(eData.getBytes()));
    
String decrypted = (tripleDes.decrypt3(deCode, KEY));

ByteArrayInputStream in = new ByteArrayInputStream(decrypted.getBytes());
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPInputStream g = new GZIPInputStream(in);
int i;

while ((i = g.read()) != -1   ) {
    out.write(i);
}
g.close();
System.out.println("Decompressed: " + out.toString());

Something is majorly wrong and I don't see it.. Thank you again. This is my error.
Code:
java.util.zip.ZipException: oversubscribed literal/length tree
java.util.zip.InflaterInputStream.read(Unknown Source)
java.util.zip.GZIPInputStream.read(Unknown Source)
java.util.zip.InflaterInputStream.read(Unknown Source)
 
This is something I wrote a while ago, modified a bit for you.
I'm not sure what your problem is exactly, as I cannot run a test, because you do not post what your "Base64" class is, or how you are generating your key etc ...

Anyhow - this works :

Code:
		// Make an encrypt key
		Security.addProvider( new com.sun.crypto.provider.SunJCE() );
		KeyGenerator generator = KeyGenerator.getInstance("DES", "SunJCE" );

		//generate a new random key
		generator.init(56, new SecureRandom());
		Key key = generator.generateKey();


		// Test data
		byte[] data = "Hello World!".getBytes();

		///////////////////////////
		//// WRITE
		///////////////////////////

		// GZIP it
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream gzOut = new GZIPOutputStream(out);
		gzOut.write(data);
		gzOut.finish();
		gzOut.close();

		byte[] gzData = out.toByteArray();

		// Encrypt it
		Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, key);

		byte[] encryptedGZData = cipher.doFinal(gzData);

		// Base64 encode it
		sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
		String b64Data = encoder.encode(encryptedGZData);


		///////////////////////////
		//// READ
		///////////////////////////

		// Un base64 it
		sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
		encryptedGZData = decoder.decodeBuffer(b64Data);

		// Decrypt it
		cipher.init(Cipher.DECRYPT_MODE, key);
		byte[] decryptGZData = cipher.doFinal(encryptedGZData);


		// Un-GZIP it
		ByteArrayInputStream in = new ByteArrayInputStream(decryptGZData);
		GZIPInputStream gzIn = new GZIPInputStream(in);
		byte[] buffer = new byte[4096];

		out = new ByteArrayOutputStream();

		while (gzIn.read(buffer, 0, buffer.length) != -1) {
			out.write(buffer);
		}
		gzIn.close();

		// Print out the result
		System.out.println(out.toString().trim());

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
See the ByteArrayOutputStream out... Can I say out.toString(). Because with my encryption i need to pass a string and a key. eg. String encryptedData = tDES(out.toString(), KEY); then can I go encryptedData.getBytes(); Because im still having problems after its been zipped. Getting oversubscribed literal/length tree. Sorry this question is off topic Is there any sites to learn java really well im just new out of school for about 4 months now so im just a rookie. Thanks
 
Did you even try running my code ?
It encrypts data using a key ... and shows you how to base64 encode it and zip/unzip it !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I tried using most of your code. I can't use the encrypt part though thats it because in my company i'm working for there is about 20 apps that use 3des ECB but the java one wouldnt work so. I'm using an encrypt class written by someone here. I'm writing a authorization/authentication program that has to encrypt with the class i have here in order for all the other apps to use it. So basically I can use everything you have except the encrypt part. My encrypt class i have to send it a string and key and get back an encrypted string vise versa to decrypt. But I cant get your code working if I use my encrypt class in there for some reason.

I did have the GZIP working before but i did it wrong because I GZIPed it and then base64 then encrypted which i should ahve did the base64 last but when I did it that way I couldn't get it to work. how would I motify ur code to work with my encryption class as a string. Thanks

 
Post him the API for your encryption class, maybe?

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top