Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blowfish representation:hex to base64 #2

Closed
brenthuisman opened this issue May 14, 2013 · 13 comments
Closed

Blowfish representation:hex to base64 #2

brenthuisman opened this issue May 14, 2013 · 13 comments
Assignees

Comments

@brenthuisman
Copy link

Currently an encrypted message takes up twice the numbre of chars as an unencrypted message, due to the hex representation of the former versus base64 for the latter. In order to save some chars, it'd be nice to convert the hex to base64 and back.

@ghost ghost assigned alfg May 14, 2013
@alfg
Copy link
Owner

alfg commented May 14, 2013

Hi @huisman,

Do you have any examples you can show me? I tried converting a few samples of the generated hex code to base64 and the string output is longer, it seems.

A simple "Hello, World!" with a key of foobar outputs the following hex:
5689482A359F2AA08AEC0A4C0B19647EFED77C945C3B7EA3

Then converting to base64:

$ echo "5689482A359F2AA08AEC0A4C0B19647EFED77C945C3B7EA3" | base64
NTY4OTQ4MkEzNTlGMkFBMDhBRUMwQTRDMEIxOTY0N0VGRUQ3N0M5NDVDM0I3RUEzCg==

I also tried a paragraph of Lorem Ipsum with foobar as the key. The hex output was 1826 characters compared to 2469 when the hex is converted to base64.

Maybe it depends on the key size and/or text, or perhaps I'm misunderstanding your question. Can you provide the sample you tried?

Thanks,
Alf

@brenthuisman
Copy link
Author

Hey @alfg,

Simply converting a hex-string to base64 doesnt increase the number of bits per char, as the conversion treats the input sequence naively as a string. But since a hex char encodes 4 bits, and a base64 char 6 bits, we need to convert the hex string to a bit representation and then express that number in base64 in order to get to the shorter string. base64.js only features UTF* <-> base64 conversion, so I suppose we need to find/write a routine that converts hex-chars and put that in front of the blowfish.js in-/output.

Is that more clear? I don't really know javascript, but I found one that appears to do what is required: http://en.ahotoke.com/tools/hexbase64.html

Cheers!

@alfg
Copy link
Owner

alfg commented May 15, 2013

Alrighty. So I've added an implementation of converting Hex<->Base64 based off of the link you referred to. Thanks!

Here is a sample:
http://alfg.co/jot/#brLFB58KKrzmc8U7Gpry9k3LsiGDKPM50H+0e/PikHTlx7VniPUVijMmrzRyZeiR9/td8lFw7fqM=
(key=foobar)

Compared to just the hex value:
ACB141E7C28AAF399CF14EC6A6BCBD9372EC8860CA3CCE741FED1EFCF8A41D3971ED59E23D45628CC9ABCD1C997A247DFED77C945C3B7EA3

Seems much shorter now after a few tests. Thanks for your help on this. Let me know if if you see any problems with the implementation.

Alf

@brenthuisman
Copy link
Author

A that's great! The URLs are shorter here too. Nice work!

Small detail: perhaps it'd be nice that after you press Decrypt the regular Save/Clear/Add key buttons return.

@alfg
Copy link
Owner

alfg commented May 16, 2013

Thanks for testing it out!

The Clear button will make the regular buttons return. I leave the Decrypt button there for when you try decrypting with the wrong key, you won't have to refresh to try again. I haven't thought of a way to validate if the decryption was successful or not.

@brenthuisman
Copy link
Author

The downside is that currently you need to copy, clear and paste to edit a protected text. You could add a second control char (after e) but ecrypted with the key. If after decryption this char is correct, you'll be good to go.

@alfg
Copy link
Owner

alfg commented May 20, 2013

Thanks for the suggestion. I will look into this.

@brenthuisman brenthuisman reopened this May 28, 2013
@brenthuisman
Copy link
Author

I discovered a bug that shows on messages of (at least) 1420 chars (I havent pinned down the exact number) that are encrypted. After decryption the last character of the original message is missing.

@alfg
Copy link
Owner

alfg commented May 29, 2013

Thanks, looking into this. I noticed it happens on some shorter messages after playing around with some variations. Not sure what is causing it yet.

http://alfg.co/jot/#bYwoTigrPd6TpdG4Z2+3p9hMv3b9w+Zrt (key=test)

It cuts off the trailing e in message.

@ghost
Copy link

ghost commented May 29, 2013

Hi @alfg,

About the bug, I tried this code.

for (var i = 1, bf = new Blowfish("test"), a = []; i < 50; ++i)
  if (bf.decrypt(bf.encrypt(Array(i + 1).join("a"))).length !== i)
    a.push(i);
alert(a); //1,9,17,25,33,41,49

I guess that blowfish.js cause this bug when a number of characters is 8 * n + 1.
I found the library.
https://github.com/takezoh/blowfish.js
(en|de)crypt64 method is useful.
Maybe you dont need hexBase64.js.
Thank you.

@alfg
Copy link
Owner

alfg commented May 29, 2013

Thank you, @imu0x10! I will test out this library later tonight.

@alfg
Copy link
Owner

alfg commented May 30, 2013

Hi @imu0x10 and @huisman,

http://alfg.co/jot/ is now up to date with the updated blowfish.js library mentioned above and is also using the encrypt64/decrypt64 methods as well. The issue seems to be fixed for some samples I've tried. Go ahead and give it a shot and close ticket or respond if you still have any issues.

Thanks again!

@brenthuisman
Copy link
Author

Excellent, the bug seems to be gone now! Thanks @imu0x10, @alfg!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants