Why won't ruby-mcrypt accept an array as a key?
Hello I am having trouble encrypting using an array as the key and the value with the ruby-mcrypt gem. The gem lets me use an array for the key fine, cipher = Mcrypt.new("rijndael-256", :ecb, secret)
works. But it will give me an error when I try to encrypt. I've tried many things but no luck. Does anyone know if Mcrypt just doesn't like encrypting with an array?
require 'mcrypt'
def encrypt(plain, secret)
cipher = Mcrypt.new("rijndael-256", :ecb, secret)
cipher.padding = :zeros
encrypted = cipher.encrypt(plain)
p encrypted
encrypted.unpack("H*").first.to_s.upcase
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(array_to_encrypt, key_array)
p "RESULT IS #{result}"
The output is as follows:
Mcrypt::RuntimeError: Could not initialize mcrypt: Key length is not legal.
I traced this error to here in the ruby-mcrypt
gem but don't understand it enough to figure out why I am getting the error message. Any help or insights would be amazing. Thanks!
ruby encryption
|
show 1 more comment
Hello I am having trouble encrypting using an array as the key and the value with the ruby-mcrypt gem. The gem lets me use an array for the key fine, cipher = Mcrypt.new("rijndael-256", :ecb, secret)
works. But it will give me an error when I try to encrypt. I've tried many things but no luck. Does anyone know if Mcrypt just doesn't like encrypting with an array?
require 'mcrypt'
def encrypt(plain, secret)
cipher = Mcrypt.new("rijndael-256", :ecb, secret)
cipher.padding = :zeros
encrypted = cipher.encrypt(plain)
p encrypted
encrypted.unpack("H*").first.to_s.upcase
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(array_to_encrypt, key_array)
p "RESULT IS #{result}"
The output is as follows:
Mcrypt::RuntimeError: Could not initialize mcrypt: Key length is not legal.
I traced this error to here in the ruby-mcrypt
gem but don't understand it enough to figure out why I am getting the error message. Any help or insights would be amazing. Thanks!
ruby encryption
Trykey_array.map(&:to_s)
?key_array.map(&:size).inject(:+) => 128
whereaskey_array.map(&:to_s).map(&:size).inject(:+) => 32
. If Mcrypt is not changing the ints to strings it may be giving it a 128byte / 1024bit key instead of 32byte / 256bit
– Tom
Jan 2 at 18:39
Thanks for the suggestion. Unfortunately I am still getting the same error. I was hoping that would work cause that makes sense.
– Gabriel
Jan 2 at 18:45
Try doubling the length of the array [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56, 65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]`. That would match up with this suggestion where their hex array is twice as long for a 256 rijndael.
– Tom
Jan 2 at 19:41
Judging by the fact that thekey
accessor returns aString
and reading between the lines of the documentation forkey=
, I would guess that the key needs to be aString
inEncoding::BINARY
encoding.
– Jörg W Mittag
Jan 2 at 19:46
@Tom I did try that and still got the same error
– Gabriel
Jan 2 at 19:53
|
show 1 more comment
Hello I am having trouble encrypting using an array as the key and the value with the ruby-mcrypt gem. The gem lets me use an array for the key fine, cipher = Mcrypt.new("rijndael-256", :ecb, secret)
works. But it will give me an error when I try to encrypt. I've tried many things but no luck. Does anyone know if Mcrypt just doesn't like encrypting with an array?
require 'mcrypt'
def encrypt(plain, secret)
cipher = Mcrypt.new("rijndael-256", :ecb, secret)
cipher.padding = :zeros
encrypted = cipher.encrypt(plain)
p encrypted
encrypted.unpack("H*").first.to_s.upcase
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(array_to_encrypt, key_array)
p "RESULT IS #{result}"
The output is as follows:
Mcrypt::RuntimeError: Could not initialize mcrypt: Key length is not legal.
I traced this error to here in the ruby-mcrypt
gem but don't understand it enough to figure out why I am getting the error message. Any help or insights would be amazing. Thanks!
ruby encryption
Hello I am having trouble encrypting using an array as the key and the value with the ruby-mcrypt gem. The gem lets me use an array for the key fine, cipher = Mcrypt.new("rijndael-256", :ecb, secret)
works. But it will give me an error when I try to encrypt. I've tried many things but no luck. Does anyone know if Mcrypt just doesn't like encrypting with an array?
require 'mcrypt'
def encrypt(plain, secret)
cipher = Mcrypt.new("rijndael-256", :ecb, secret)
cipher.padding = :zeros
encrypted = cipher.encrypt(plain)
p encrypted
encrypted.unpack("H*").first.to_s.upcase
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(array_to_encrypt, key_array)
p "RESULT IS #{result}"
The output is as follows:
Mcrypt::RuntimeError: Could not initialize mcrypt: Key length is not legal.
I traced this error to here in the ruby-mcrypt
gem but don't understand it enough to figure out why I am getting the error message. Any help or insights would be amazing. Thanks!
ruby encryption
ruby encryption
edited Jan 8 at 22:40
Gabriel
asked Jan 2 at 18:09


GabrielGabriel
310314
310314
Trykey_array.map(&:to_s)
?key_array.map(&:size).inject(:+) => 128
whereaskey_array.map(&:to_s).map(&:size).inject(:+) => 32
. If Mcrypt is not changing the ints to strings it may be giving it a 128byte / 1024bit key instead of 32byte / 256bit
– Tom
Jan 2 at 18:39
Thanks for the suggestion. Unfortunately I am still getting the same error. I was hoping that would work cause that makes sense.
– Gabriel
Jan 2 at 18:45
Try doubling the length of the array [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56, 65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]`. That would match up with this suggestion where their hex array is twice as long for a 256 rijndael.
– Tom
Jan 2 at 19:41
Judging by the fact that thekey
accessor returns aString
and reading between the lines of the documentation forkey=
, I would guess that the key needs to be aString
inEncoding::BINARY
encoding.
– Jörg W Mittag
Jan 2 at 19:46
@Tom I did try that and still got the same error
– Gabriel
Jan 2 at 19:53
|
show 1 more comment
Trykey_array.map(&:to_s)
?key_array.map(&:size).inject(:+) => 128
whereaskey_array.map(&:to_s).map(&:size).inject(:+) => 32
. If Mcrypt is not changing the ints to strings it may be giving it a 128byte / 1024bit key instead of 32byte / 256bit
– Tom
Jan 2 at 18:39
Thanks for the suggestion. Unfortunately I am still getting the same error. I was hoping that would work cause that makes sense.
– Gabriel
Jan 2 at 18:45
Try doubling the length of the array [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56, 65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]`. That would match up with this suggestion where their hex array is twice as long for a 256 rijndael.
– Tom
Jan 2 at 19:41
Judging by the fact that thekey
accessor returns aString
and reading between the lines of the documentation forkey=
, I would guess that the key needs to be aString
inEncoding::BINARY
encoding.
– Jörg W Mittag
Jan 2 at 19:46
@Tom I did try that and still got the same error
– Gabriel
Jan 2 at 19:53
Try
key_array.map(&:to_s)
? key_array.map(&:size).inject(:+) => 128
whereas key_array.map(&:to_s).map(&:size).inject(:+) => 32
. If Mcrypt is not changing the ints to strings it may be giving it a 128byte / 1024bit key instead of 32byte / 256bit– Tom
Jan 2 at 18:39
Try
key_array.map(&:to_s)
? key_array.map(&:size).inject(:+) => 128
whereas key_array.map(&:to_s).map(&:size).inject(:+) => 32
. If Mcrypt is not changing the ints to strings it may be giving it a 128byte / 1024bit key instead of 32byte / 256bit– Tom
Jan 2 at 18:39
Thanks for the suggestion. Unfortunately I am still getting the same error. I was hoping that would work cause that makes sense.
– Gabriel
Jan 2 at 18:45
Thanks for the suggestion. Unfortunately I am still getting the same error. I was hoping that would work cause that makes sense.
– Gabriel
Jan 2 at 18:45
Try doubling the length of the array [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56, 65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]`. That would match up with this suggestion where their hex array is twice as long for a 256 rijndael.
– Tom
Jan 2 at 19:41
Try doubling the length of the array [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56, 65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]`. That would match up with this suggestion where their hex array is twice as long for a 256 rijndael.
– Tom
Jan 2 at 19:41
Judging by the fact that the
key
accessor returns a String
and reading between the lines of the documentation for key=
, I would guess that the key needs to be a String
in Encoding::BINARY
encoding.– Jörg W Mittag
Jan 2 at 19:46
Judging by the fact that the
key
accessor returns a String
and reading between the lines of the documentation for key=
, I would guess that the key needs to be a String
in Encoding::BINARY
encoding.– Jörg W Mittag
Jan 2 at 19:46
@Tom I did try that and still got the same error
– Gabriel
Jan 2 at 19:53
@Tom I did try that and still got the same error
– Gabriel
Jan 2 at 19:53
|
show 1 more comment
1 Answer
1
active
oldest
votes
The library doesn't support arrays. You'll need to use Strings instead:
def binary(byte_array)
byte_array.pack('C*')
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(binary(array_to_encrypt), binary(key_array))
p "RESULT IS #{result}"
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/3.0/"u003ecc by-sa 3.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%2f54011153%2fwhy-wont-ruby-mcrypt-accept-an-array-as-a-key%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The library doesn't support arrays. You'll need to use Strings instead:
def binary(byte_array)
byte_array.pack('C*')
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(binary(array_to_encrypt), binary(key_array))
p "RESULT IS #{result}"
add a comment |
The library doesn't support arrays. You'll need to use Strings instead:
def binary(byte_array)
byte_array.pack('C*')
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(binary(array_to_encrypt), binary(key_array))
p "RESULT IS #{result}"
add a comment |
The library doesn't support arrays. You'll need to use Strings instead:
def binary(byte_array)
byte_array.pack('C*')
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(binary(array_to_encrypt), binary(key_array))
p "RESULT IS #{result}"
The library doesn't support arrays. You'll need to use Strings instead:
def binary(byte_array)
byte_array.pack('C*')
end
array_to_encrypt = [16, 0, 0, 0, 50, 48, 49, 55, 47, 48, 50, 47, 48, 55, 32, 50, 50, 58, 52, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
key_array = [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]
result = encrypt(binary(array_to_encrypt), binary(key_array))
p "RESULT IS #{result}"
answered Jan 7 at 19:44
KingPongKingPong
9621118
9621118
add a comment |
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%2f54011153%2fwhy-wont-ruby-mcrypt-accept-an-array-as-a-key%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
Try
key_array.map(&:to_s)
?key_array.map(&:size).inject(:+) => 128
whereaskey_array.map(&:to_s).map(&:size).inject(:+) => 32
. If Mcrypt is not changing the ints to strings it may be giving it a 128byte / 1024bit key instead of 32byte / 256bit– Tom
Jan 2 at 18:39
Thanks for the suggestion. Unfortunately I am still getting the same error. I was hoping that would work cause that makes sense.
– Gabriel
Jan 2 at 18:45
Try doubling the length of the array [65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56, 65, 66, 67, 68, 49, 50, 51, 52, 70, 71, 72, 73, 53, 54, 55, 56]`. That would match up with this suggestion where their hex array is twice as long for a 256 rijndael.
– Tom
Jan 2 at 19:41
Judging by the fact that the
key
accessor returns aString
and reading between the lines of the documentation forkey=
, I would guess that the key needs to be aString
inEncoding::BINARY
encoding.– Jörg W Mittag
Jan 2 at 19:46
@Tom I did try that and still got the same error
– Gabriel
Jan 2 at 19:53