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;








1















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.










share|improve this question


























  • 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

















1















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.










share|improve this question


























  • 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













1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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
















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












2 Answers
2






active

oldest

votes


















3
















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.






share|improve this answer



























  • 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





    @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


















1
















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






share|improve this answer

























  • 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













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
);



);














draft saved

draft discarded
















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









3
















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.






share|improve this answer



























  • 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





    @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















3
















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.






share|improve this answer



























  • 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





    @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













3














3










3









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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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





    @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






  • 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













1
















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






share|improve this answer

























  • 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















1
















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






share|improve this answer

























  • 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













1














1










1









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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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


















draft saved

draft discarded















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현