Why is Ruby base64 encoded string different from all other base64 encoded strings?Calling shell commands from RubyHow to generate a random string in RubyHow can you encode a string to Base64 in JavaScript?How to convert a string to lower or upper case in RubyBinary Data in JSON String. Something better than Base64How to do Base64 encoding in node.js?Why is it bad style to `rescue Exception => e` in Ruby?How do I encode and decode a base64 string?rubyzip Zip::ZipFile.open_buffer expects an argument of class String or IOBase64 to image conversion in python
Paths Short or Long
Manager manipulates my leaves, what's in it for him?
Hobby function generators
MySQL - How to check for a value in all columns
Why are two-stroke engines nearly unheard of in aviation?
Minimize taxes now that I earn a living wage
Cemented carbide swords - worth it?
What's the word for a student who doesn't register but goes to a class anyway?
Which block cipher parameters should be kept secret?
Minimum number of lines to draw 111 squares
Very lazy puppy
As an employer, can I compel my employees to vote?
What are the end bytes of *.docx file format
Can one guy with a duplicator trigger a nuclear apocalypse?
Tips for remembering the order of parameters for ln?
(How long) Should I indulge my new co-workers?
Why is belonging not transitive?
Debussy as term for bathroom?
Secondary characters in character-study fiction
Does Mage Hand give away the caster's position?
Persuading players to be less attached to a pre-session 0 character concept
Should the pagination be reset when changing the order?
Is it worth the risk to apply to REUs? How likely am I to be accepted?
merging certain list elements
Why is Ruby base64 encoded string different from all other base64 encoded strings?
Calling shell commands from RubyHow to generate a random string in RubyHow can you encode a string to Base64 in JavaScript?How to convert a string to lower or upper case in RubyBinary Data in JSON String. Something better than Base64How to do Base64 encoding in node.js?Why is it bad style to `rescue Exception => e` in Ruby?How do I encode and decode a base64 string?rubyzip Zip::ZipFile.open_buffer expects an argument of class String or IOBase64 to image conversion in python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
For end-to-end encrypted communication between a client and a server, I am implementing an encryption/decryption algorithm.
However, they (encryption/decryption and base64 encoding/decoding) work fine only when it's in Ruby.
But the actual problem I see is with the Base64 encoding of Ruby.
For example, let's say I have this (32 bytes) AES key:
""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B"
that I am using to encrypt data in AES algorithm.
I want to send this key to a client in Base64 encoded format. For that, I am doing (two ways, each produces different encoded output):
Key in double quotes
Base64.urlsafe_encode64(""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B")
# => "IjGvx8CmybrWn7rSyb4PjiqIhyiby3AVIS8Tj877FZs="
Key in single quotes
Base64.urlsafe_encode64('"1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B')
# => "XCIxXHhBRlx4QzdceEMwXHhBNlx4QzlceEJBXHhENlx4OUZceEJBXHhEMlx4QzlceEJFXHgwRlx4OEUqXHg4OFx4ODcoXHg5Qlx4Q0JwXHgxNSEvXHgxM1x4OEZceENFXHhGQlx4MTVceDlC"
Output 1 is different from all other libraries: Java, Swift, and an online site, another site, which all produce the same output.
Output 2 is the same with other libraries with respect to the output encoding. But I have issues converting AES key and AES encrypted data to be used in single quotes, which is not possible, as I have encrypted data that already have those single quotes and other illegal characters, for which Ruby's Base64 encoding does not work correctly.
Any help would be appreciated.
ruby base64 aes
|
show 1 more comment
For end-to-end encrypted communication between a client and a server, I am implementing an encryption/decryption algorithm.
However, they (encryption/decryption and base64 encoding/decoding) work fine only when it's in Ruby.
But the actual problem I see is with the Base64 encoding of Ruby.
For example, let's say I have this (32 bytes) AES key:
""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B"
that I am using to encrypt data in AES algorithm.
I want to send this key to a client in Base64 encoded format. For that, I am doing (two ways, each produces different encoded output):
Key in double quotes
Base64.urlsafe_encode64(""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B")
# => "IjGvx8CmybrWn7rSyb4PjiqIhyiby3AVIS8Tj877FZs="
Key in single quotes
Base64.urlsafe_encode64('"1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B')
# => "XCIxXHhBRlx4QzdceEMwXHhBNlx4QzlceEJBXHhENlx4OUZceEJBXHhEMlx4QzlceEJFXHgwRlx4OEUqXHg4OFx4ODcoXHg5Qlx4Q0JwXHgxNSEvXHgxM1x4OEZceENFXHhGQlx4MTVceDlC"
Output 1 is different from all other libraries: Java, Swift, and an online site, another site, which all produce the same output.
Output 2 is the same with other libraries with respect to the output encoding. But I have issues converting AES key and AES encrypted data to be used in single quotes, which is not possible, as I have encrypted data that already have those single quotes and other illegal characters, for which Ruby's Base64 encoding does not work correctly.
Any help would be appreciated.
ruby base64 aes
Do you really mean to include the double quote in the beginning also? That might be the reason it’s different. Have you decoded both versions and checked what comes back and that it’s identical?
– Sami Kuhmonen
Mar 28 at 14:04
3
"For end to end encrypted communication between client and server, I have been implementing the encryption/decryption algorithm." Don't. Strong, secure, and free encryption software already exists and it's easy to use. Writing your own encryption code is not a good idea and the result will not be secure. Instead, enable SSL for your application server and use all the time you saved to add useful features to your app.
– Jordan Running
Mar 28 at 14:14
... Observe what happens if youputs
the strings with the different quotes. The type of quote matters in Ruby.
– Dave Newton
Mar 28 at 14:28
And if you're implementing your own encryption algorithm, I echo the others in saying for your own benefit, don't. It's not clear if you're using a separate library or if you're actually attempting your own crypto.
– Dave Newton
Mar 28 at 14:29
@jordan running yes, I am not writing my own encryption algo, instead I am using Ruby's default OpenSSL library and its AES encryption technics.
– Bhavesh Kshatriya
Mar 28 at 20:28
|
show 1 more comment
For end-to-end encrypted communication between a client and a server, I am implementing an encryption/decryption algorithm.
However, they (encryption/decryption and base64 encoding/decoding) work fine only when it's in Ruby.
But the actual problem I see is with the Base64 encoding of Ruby.
For example, let's say I have this (32 bytes) AES key:
""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B"
that I am using to encrypt data in AES algorithm.
I want to send this key to a client in Base64 encoded format. For that, I am doing (two ways, each produces different encoded output):
Key in double quotes
Base64.urlsafe_encode64(""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B")
# => "IjGvx8CmybrWn7rSyb4PjiqIhyiby3AVIS8Tj877FZs="
Key in single quotes
Base64.urlsafe_encode64('"1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B')
# => "XCIxXHhBRlx4QzdceEMwXHhBNlx4QzlceEJBXHhENlx4OUZceEJBXHhEMlx4QzlceEJFXHgwRlx4OEUqXHg4OFx4ODcoXHg5Qlx4Q0JwXHgxNSEvXHgxM1x4OEZceENFXHhGQlx4MTVceDlC"
Output 1 is different from all other libraries: Java, Swift, and an online site, another site, which all produce the same output.
Output 2 is the same with other libraries with respect to the output encoding. But I have issues converting AES key and AES encrypted data to be used in single quotes, which is not possible, as I have encrypted data that already have those single quotes and other illegal characters, for which Ruby's Base64 encoding does not work correctly.
Any help would be appreciated.
ruby base64 aes
For end-to-end encrypted communication between a client and a server, I am implementing an encryption/decryption algorithm.
However, they (encryption/decryption and base64 encoding/decoding) work fine only when it's in Ruby.
But the actual problem I see is with the Base64 encoding of Ruby.
For example, let's say I have this (32 bytes) AES key:
""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B"
that I am using to encrypt data in AES algorithm.
I want to send this key to a client in Base64 encoded format. For that, I am doing (two ways, each produces different encoded output):
Key in double quotes
Base64.urlsafe_encode64(""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B")
# => "IjGvx8CmybrWn7rSyb4PjiqIhyiby3AVIS8Tj877FZs="
Key in single quotes
Base64.urlsafe_encode64('"1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B')
# => "XCIxXHhBRlx4QzdceEMwXHhBNlx4QzlceEJBXHhENlx4OUZceEJBXHhEMlx4QzlceEJFXHgwRlx4OEUqXHg4OFx4ODcoXHg5Qlx4Q0JwXHgxNSEvXHgxM1x4OEZceENFXHhGQlx4MTVceDlC"
Output 1 is different from all other libraries: Java, Swift, and an online site, another site, which all produce the same output.
Output 2 is the same with other libraries with respect to the output encoding. But I have issues converting AES key and AES encrypted data to be used in single quotes, which is not possible, as I have encrypted data that already have those single quotes and other illegal characters, for which Ruby's Base64 encoding does not work correctly.
Any help would be appreciated.
ruby base64 aes
ruby base64 aes
edited Mar 29 at 8:52
sawa
136k31 gold badges222 silver badges321 bronze badges
136k31 gold badges222 silver badges321 bronze badges
asked Mar 28 at 13:53
Bhavesh KshatriyaBhavesh Kshatriya
92 bronze badges
92 bronze badges
Do you really mean to include the double quote in the beginning also? That might be the reason it’s different. Have you decoded both versions and checked what comes back and that it’s identical?
– Sami Kuhmonen
Mar 28 at 14:04
3
"For end to end encrypted communication between client and server, I have been implementing the encryption/decryption algorithm." Don't. Strong, secure, and free encryption software already exists and it's easy to use. Writing your own encryption code is not a good idea and the result will not be secure. Instead, enable SSL for your application server and use all the time you saved to add useful features to your app.
– Jordan Running
Mar 28 at 14:14
... Observe what happens if youputs
the strings with the different quotes. The type of quote matters in Ruby.
– Dave Newton
Mar 28 at 14:28
And if you're implementing your own encryption algorithm, I echo the others in saying for your own benefit, don't. It's not clear if you're using a separate library or if you're actually attempting your own crypto.
– Dave Newton
Mar 28 at 14:29
@jordan running yes, I am not writing my own encryption algo, instead I am using Ruby's default OpenSSL library and its AES encryption technics.
– Bhavesh Kshatriya
Mar 28 at 20:28
|
show 1 more comment
Do you really mean to include the double quote in the beginning also? That might be the reason it’s different. Have you decoded both versions and checked what comes back and that it’s identical?
– Sami Kuhmonen
Mar 28 at 14:04
3
"For end to end encrypted communication between client and server, I have been implementing the encryption/decryption algorithm." Don't. Strong, secure, and free encryption software already exists and it's easy to use. Writing your own encryption code is not a good idea and the result will not be secure. Instead, enable SSL for your application server and use all the time you saved to add useful features to your app.
– Jordan Running
Mar 28 at 14:14
... Observe what happens if youputs
the strings with the different quotes. The type of quote matters in Ruby.
– Dave Newton
Mar 28 at 14:28
And if you're implementing your own encryption algorithm, I echo the others in saying for your own benefit, don't. It's not clear if you're using a separate library or if you're actually attempting your own crypto.
– Dave Newton
Mar 28 at 14:29
@jordan running yes, I am not writing my own encryption algo, instead I am using Ruby's default OpenSSL library and its AES encryption technics.
– Bhavesh Kshatriya
Mar 28 at 20:28
Do you really mean to include the double quote in the beginning also? That might be the reason it’s different. Have you decoded both versions and checked what comes back and that it’s identical?
– Sami Kuhmonen
Mar 28 at 14:04
Do you really mean to include the double quote in the beginning also? That might be the reason it’s different. Have you decoded both versions and checked what comes back and that it’s identical?
– Sami Kuhmonen
Mar 28 at 14:04
3
3
"For end to end encrypted communication between client and server, I have been implementing the encryption/decryption algorithm." Don't. Strong, secure, and free encryption software already exists and it's easy to use. Writing your own encryption code is not a good idea and the result will not be secure. Instead, enable SSL for your application server and use all the time you saved to add useful features to your app.
– Jordan Running
Mar 28 at 14:14
"For end to end encrypted communication between client and server, I have been implementing the encryption/decryption algorithm." Don't. Strong, secure, and free encryption software already exists and it's easy to use. Writing your own encryption code is not a good idea and the result will not be secure. Instead, enable SSL for your application server and use all the time you saved to add useful features to your app.
– Jordan Running
Mar 28 at 14:14
... Observe what happens if you
puts
the strings with the different quotes. The type of quote matters in Ruby.– Dave Newton
Mar 28 at 14:28
... Observe what happens if you
puts
the strings with the different quotes. The type of quote matters in Ruby.– Dave Newton
Mar 28 at 14:28
And if you're implementing your own encryption algorithm, I echo the others in saying for your own benefit, don't. It's not clear if you're using a separate library or if you're actually attempting your own crypto.
– Dave Newton
Mar 28 at 14:29
And if you're implementing your own encryption algorithm, I echo the others in saying for your own benefit, don't. It's not clear if you're using a separate library or if you're actually attempting your own crypto.
– Dave Newton
Mar 28 at 14:29
@jordan running yes, I am not writing my own encryption algo, instead I am using Ruby's default OpenSSL library and its AES encryption technics.
– Bhavesh Kshatriya
Mar 28 at 20:28
@jordan running yes, I am not writing my own encryption algo, instead I am using Ruby's default OpenSSL library and its AES encryption technics.
– Bhavesh Kshatriya
Mar 28 at 20:28
|
show 1 more comment
2 Answers
2
active
oldest
votes
The problem is that when you use single quote you will get a different result:
a = ""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B"
#=> ""1xAFxC7xC0xA6ɺ֟xBAxD2ɾu000Fx8E*x88x87(x9BxCBpu0015!/u0013x8FxCExFBu0015x9B"
b = '"1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B'
#=> "\"1\xAF\xC7\xC0\xA6\xC9\xBA\xD6\x9F\xBA\xD2\xC9\xBE\x0F\x8E*\x88\x87(\x9B\xCBp\x15!/\x13\x8F\xCE\xFB\x15\x9B"
a == b
=> false
a.bytes.count
=> 32
b.bytes.count
=> 108
a.length
=> 29
b.length
=> 108
So you see single quotes will double escape the escapes.
However, as Jordan Running mentions in comments you should not roll your own encryption, it is not secure please read this answer to understand why.
It is a much better idea to use ruby's openssl gem. For instructions on how to use it, please refer to documentation here.
Looking at theString#bytes
output might make the difference clearer in this case. Probably best to make sure they're working with a binary string as well (just in case).
– mu is too short
Mar 28 at 17:04
1
@muistooshort yeah good idea, I updated, but didn't show the full bytes array but I think this is clear enough.
– lacostenycoder
Mar 28 at 18:48
add a comment
|
If you need single quote behavior but cannot use single quotes then Ruby has the %q
option, that is %q followed by brackets or parens etc surrounding your string, or any char following %q will act as single quote: %q|it's your string|
or %qit's your string
Thanks for answer. This could probably do the trick. But the problem is I already have data in some variable, so now how to use that variable to enclose it within single quote by this method? %q#aes_key => #aes_key It doesn't evaluates that variable.
– Bhavesh Kshatriya
Mar 29 at 7:49
Yes, that is single quote behavior. If you want to evaluate variables you need double quote behaviour - use %Q.
– steenslag
Mar 29 at 9:23
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399329%2fwhy-is-ruby-base64-encoded-string-different-from-all-other-base64-encoded-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that when you use single quote you will get a different result:
a = ""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B"
#=> ""1xAFxC7xC0xA6ɺ֟xBAxD2ɾu000Fx8E*x88x87(x9BxCBpu0015!/u0013x8FxCExFBu0015x9B"
b = '"1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B'
#=> "\"1\xAF\xC7\xC0\xA6\xC9\xBA\xD6\x9F\xBA\xD2\xC9\xBE\x0F\x8E*\x88\x87(\x9B\xCBp\x15!/\x13\x8F\xCE\xFB\x15\x9B"
a == b
=> false
a.bytes.count
=> 32
b.bytes.count
=> 108
a.length
=> 29
b.length
=> 108
So you see single quotes will double escape the escapes.
However, as Jordan Running mentions in comments you should not roll your own encryption, it is not secure please read this answer to understand why.
It is a much better idea to use ruby's openssl gem. For instructions on how to use it, please refer to documentation here.
Looking at theString#bytes
output might make the difference clearer in this case. Probably best to make sure they're working with a binary string as well (just in case).
– mu is too short
Mar 28 at 17:04
1
@muistooshort yeah good idea, I updated, but didn't show the full bytes array but I think this is clear enough.
– lacostenycoder
Mar 28 at 18:48
add a comment
|
The problem is that when you use single quote you will get a different result:
a = ""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B"
#=> ""1xAFxC7xC0xA6ɺ֟xBAxD2ɾu000Fx8E*x88x87(x9BxCBpu0015!/u0013x8FxCExFBu0015x9B"
b = '"1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B'
#=> "\"1\xAF\xC7\xC0\xA6\xC9\xBA\xD6\x9F\xBA\xD2\xC9\xBE\x0F\x8E*\x88\x87(\x9B\xCBp\x15!/\x13\x8F\xCE\xFB\x15\x9B"
a == b
=> false
a.bytes.count
=> 32
b.bytes.count
=> 108
a.length
=> 29
b.length
=> 108
So you see single quotes will double escape the escapes.
However, as Jordan Running mentions in comments you should not roll your own encryption, it is not secure please read this answer to understand why.
It is a much better idea to use ruby's openssl gem. For instructions on how to use it, please refer to documentation here.
Looking at theString#bytes
output might make the difference clearer in this case. Probably best to make sure they're working with a binary string as well (just in case).
– mu is too short
Mar 28 at 17:04
1
@muistooshort yeah good idea, I updated, but didn't show the full bytes array but I think this is clear enough.
– lacostenycoder
Mar 28 at 18:48
add a comment
|
The problem is that when you use single quote you will get a different result:
a = ""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B"
#=> ""1xAFxC7xC0xA6ɺ֟xBAxD2ɾu000Fx8E*x88x87(x9BxCBpu0015!/u0013x8FxCExFBu0015x9B"
b = '"1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B'
#=> "\"1\xAF\xC7\xC0\xA6\xC9\xBA\xD6\x9F\xBA\xD2\xC9\xBE\x0F\x8E*\x88\x87(\x9B\xCBp\x15!/\x13\x8F\xCE\xFB\x15\x9B"
a == b
=> false
a.bytes.count
=> 32
b.bytes.count
=> 108
a.length
=> 29
b.length
=> 108
So you see single quotes will double escape the escapes.
However, as Jordan Running mentions in comments you should not roll your own encryption, it is not secure please read this answer to understand why.
It is a much better idea to use ruby's openssl gem. For instructions on how to use it, please refer to documentation here.
The problem is that when you use single quote you will get a different result:
a = ""1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B"
#=> ""1xAFxC7xC0xA6ɺ֟xBAxD2ɾu000Fx8E*x88x87(x9BxCBpu0015!/u0013x8FxCExFBu0015x9B"
b = '"1xAFxC7xC0xA6xC9xBAxD6x9FxBAxD2xC9xBEx0Fx8E*x88x87(x9BxCBpx15!/x13x8FxCExFBx15x9B'
#=> "\"1\xAF\xC7\xC0\xA6\xC9\xBA\xD6\x9F\xBA\xD2\xC9\xBE\x0F\x8E*\x88\x87(\x9B\xCBp\x15!/\x13\x8F\xCE\xFB\x15\x9B"
a == b
=> false
a.bytes.count
=> 32
b.bytes.count
=> 108
a.length
=> 29
b.length
=> 108
So you see single quotes will double escape the escapes.
However, as Jordan Running mentions in comments you should not roll your own encryption, it is not secure please read this answer to understand why.
It is a much better idea to use ruby's openssl gem. For instructions on how to use it, please refer to documentation here.
edited Mar 28 at 18:48
answered Mar 28 at 14:22
lacostenycoderlacostenycoder
5,9923 gold badges15 silver badges32 bronze badges
5,9923 gold badges15 silver badges32 bronze badges
Looking at theString#bytes
output might make the difference clearer in this case. Probably best to make sure they're working with a binary string as well (just in case).
– mu is too short
Mar 28 at 17:04
1
@muistooshort yeah good idea, I updated, but didn't show the full bytes array but I think this is clear enough.
– lacostenycoder
Mar 28 at 18:48
add a comment
|
Looking at theString#bytes
output might make the difference clearer in this case. Probably best to make sure they're working with a binary string as well (just in case).
– mu is too short
Mar 28 at 17:04
1
@muistooshort yeah good idea, I updated, but didn't show the full bytes array but I think this is clear enough.
– lacostenycoder
Mar 28 at 18:48
Looking at the
String#bytes
output might make the difference clearer in this case. Probably best to make sure they're working with a binary string as well (just in case).– mu is too short
Mar 28 at 17:04
Looking at the
String#bytes
output might make the difference clearer in this case. Probably best to make sure they're working with a binary string as well (just in case).– mu is too short
Mar 28 at 17:04
1
1
@muistooshort yeah good idea, I updated, but didn't show the full bytes array but I think this is clear enough.
– lacostenycoder
Mar 28 at 18:48
@muistooshort yeah good idea, I updated, but didn't show the full bytes array but I think this is clear enough.
– lacostenycoder
Mar 28 at 18:48
add a comment
|
If you need single quote behavior but cannot use single quotes then Ruby has the %q
option, that is %q followed by brackets or parens etc surrounding your string, or any char following %q will act as single quote: %q|it's your string|
or %qit's your string
Thanks for answer. This could probably do the trick. But the problem is I already have data in some variable, so now how to use that variable to enclose it within single quote by this method? %q#aes_key => #aes_key It doesn't evaluates that variable.
– Bhavesh Kshatriya
Mar 29 at 7:49
Yes, that is single quote behavior. If you want to evaluate variables you need double quote behaviour - use %Q.
– steenslag
Mar 29 at 9:23
add a comment
|
If you need single quote behavior but cannot use single quotes then Ruby has the %q
option, that is %q followed by brackets or parens etc surrounding your string, or any char following %q will act as single quote: %q|it's your string|
or %qit's your string
Thanks for answer. This could probably do the trick. But the problem is I already have data in some variable, so now how to use that variable to enclose it within single quote by this method? %q#aes_key => #aes_key It doesn't evaluates that variable.
– Bhavesh Kshatriya
Mar 29 at 7:49
Yes, that is single quote behavior. If you want to evaluate variables you need double quote behaviour - use %Q.
– steenslag
Mar 29 at 9:23
add a comment
|
If you need single quote behavior but cannot use single quotes then Ruby has the %q
option, that is %q followed by brackets or parens etc surrounding your string, or any char following %q will act as single quote: %q|it's your string|
or %qit's your string
If you need single quote behavior but cannot use single quotes then Ruby has the %q
option, that is %q followed by brackets or parens etc surrounding your string, or any char following %q will act as single quote: %q|it's your string|
or %qit's your string
answered Mar 28 at 22:50
steenslagsteenslag
65.7k12 gold badges108 silver badges146 bronze badges
65.7k12 gold badges108 silver badges146 bronze badges
Thanks for answer. This could probably do the trick. But the problem is I already have data in some variable, so now how to use that variable to enclose it within single quote by this method? %q#aes_key => #aes_key It doesn't evaluates that variable.
– Bhavesh Kshatriya
Mar 29 at 7:49
Yes, that is single quote behavior. If you want to evaluate variables you need double quote behaviour - use %Q.
– steenslag
Mar 29 at 9:23
add a comment
|
Thanks for answer. This could probably do the trick. But the problem is I already have data in some variable, so now how to use that variable to enclose it within single quote by this method? %q#aes_key => #aes_key It doesn't evaluates that variable.
– Bhavesh Kshatriya
Mar 29 at 7:49
Yes, that is single quote behavior. If you want to evaluate variables you need double quote behaviour - use %Q.
– steenslag
Mar 29 at 9:23
Thanks for answer. This could probably do the trick. But the problem is I already have data in some variable, so now how to use that variable to enclose it within single quote by this method? %q#aes_key => #aes_key It doesn't evaluates that variable.
– Bhavesh Kshatriya
Mar 29 at 7:49
Thanks for answer. This could probably do the trick. But the problem is I already have data in some variable, so now how to use that variable to enclose it within single quote by this method? %q#aes_key => #aes_key It doesn't evaluates that variable.
– Bhavesh Kshatriya
Mar 29 at 7:49
Yes, that is single quote behavior. If you want to evaluate variables you need double quote behaviour - use %Q.
– steenslag
Mar 29 at 9:23
Yes, that is single quote behavior. If you want to evaluate variables you need double quote behaviour - use %Q.
– steenslag
Mar 29 at 9:23
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399329%2fwhy-is-ruby-base64-encoded-string-different-from-all-other-base64-encoded-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you really mean to include the double quote in the beginning also? That might be the reason it’s different. Have you decoded both versions and checked what comes back and that it’s identical?
– Sami Kuhmonen
Mar 28 at 14:04
3
"For end to end encrypted communication between client and server, I have been implementing the encryption/decryption algorithm." Don't. Strong, secure, and free encryption software already exists and it's easy to use. Writing your own encryption code is not a good idea and the result will not be secure. Instead, enable SSL for your application server and use all the time you saved to add useful features to your app.
– Jordan Running
Mar 28 at 14:14
... Observe what happens if you
puts
the strings with the different quotes. The type of quote matters in Ruby.– Dave Newton
Mar 28 at 14:28
And if you're implementing your own encryption algorithm, I echo the others in saying for your own benefit, don't. It's not clear if you're using a separate library or if you're actually attempting your own crypto.
– Dave Newton
Mar 28 at 14:29
@jordan running yes, I am not writing my own encryption algo, instead I am using Ruby's default OpenSSL library and its AES encryption technics.
– Bhavesh Kshatriya
Mar 28 at 20:28