IV undefined Error while decrypting AES encrypted video - encryption

I am trying to decrypt AES 128 encrypted video with AES key and IV
For decrypting I am using openssl
key —î¡ŒØËÉްʾC{]5q)pZE4d4:-MBB¬vóRJ”7–Æ­‹5•Õ
IV 0x496daa1c6914000e408c65cead91fc29
following is command line to decrypt video
openssl enc -nosalt -aes-128-cbc -d -in Question1.ts -base64 -K ^R-î^O¡ŒËÉްʾC^A{]5q)pZE4d4:-MBB^B¬vóR^TJ"7-Æ­‹5•Õ -iv 0x496daa1c6914000e408c65cead91fc29
I got this error
iv undefined
First I used AES key in command line in base64 format
and got following error
hex string is too long, ignoring excess
non-hex digit
invalid hex iv value
then I used AES key in normal format
and got error about IV
iv undefined
Which step should be followed to decrypt video with openssl?

Related

Openssl - AES-256-ECB decrypt with key, bad magic number

Im trying to decrypt a text, which was encrypted with AES-256-ECB with the given key. To decrypt, Im using the same version of the openssl which was used for encryption (OpenSSL 1.1.1d 10 Sep 2019).
String to decrypt: VAWawVAWawxiyH20dI+t5NPAY9w== (inside file.txt)
Key: 461a966faef244e4808d6b2b8e928d01 (inside key.txt)
I tried those commands:
cat file.txt | base64 -d > file2.txt
openssl enc -AES-256-ECB -d -in file2.txt -out answer.txt --kfile key.txt
And im getting: bad magic number. Whats the problem?
openssl enc will normally use a password to derive a key. So it is the derived key that is used to decrypt the file. The derivation process requires a "salt", and openssl enc during encryption stores that salt at the beginning of the file along with a "magic number" to identify it. If the magic number is missing (usually because the file wasn't encrypted by openssl enc or because the password based key derivation derivation method wasn't used) then you get this error.
The -kfile option tells OpenSSL to read the password from a file and then derive the key from it. Probably want you intended was to not use password derivation at all, but to use the explicit key. In which case you need to use the -K option and supply the key on the command line using hex.

Decrypting AES encrypted base64 string

so I have a AES-256-ecb base64 string which I decoded using an online tool. However, I'm more of a command line guy so I'm trying to use command line to decode it.
echo "nzE+iKr82Kh8BOQg0k/LViTZJup+9DReAsXd/PCtFZP5FHM7WtJ9Nz1NmqMi9G0i7rGIvhK2jRcGnFyWDT9MLoJvY1gZKI2xsUuS3nJ/n3T1Pe//4kKId+B3wfDW/TgqX6Hg/kUj8JO08wGe9JxtOEJ6XJA3cO/cSna9v3YVf/ssHTbXkb+bFgY7WLdHJyvF6lD/wfpY2ZnA1787ajtm+/aWWVMxDOwKuqIT1ZZ0Nw4=" | openssl enc -aes-256-ecb -d -a -K 366a74cb3c959de17d61db30591c39d1
this is what i've tried but i get bad decrypt error
nzE+iKr82Kh8BOQg0k/LViTZJup+9DReAsXd/PCtFZP5FHM7WtJ9Nz1NmqMi9G0i7rGIvhK2jRcGnFyWDT9MLoJvY1gZKI2xsUuS3nJ/n3T1Pe//4kKId+B3wfDW/TgqX6Hg/kUj8JO08wGe9JxtOEJ6XJA3cO/cSna9v3YVf/ssHTbXkb+bFgY7WLdHJyvF6lD/wfpY2ZnA1787ajtm+/aWWVMxDOwKuqIT1ZZ0Nw4=
is the encrypted base64 text
-aes-256-ecb is the encryption cipher
and 366a74cb3c959de17d61db30591c39d1 is the key
The result is supposed to be another base64 string:
RG9udCB3b3JyeSBzYWtldCBvbmUgZGF5IHdlIHdpbGwgcmVhY2ggdG8Kb3VyIGRlc3RpbmF0aW9uIHZlcnkgc29vbi4gQW5kIGlmIHlvdSBmb3JnZXQgCnlvdXIgdXNlcm5hbWUgdGhlbiB1c2UgeW91ciBvbGQgcGFzc3dvcmQKPT0+ICJ0cmlidXRlX3RvX2lwcHNlYyIKClZpY3Rvciw=
What you thought was the hex key was actually the binary key. We know this because if it were the hex representation, then the key would only be 128 bits, but AES-256 needs a key that's 256 bits. The real hex key is effectively a double layer of hex then, so passing 3336366137346362336339353964653137643631646233303539316333396431 as the key instead of 366a74cb3c959de17d61db30591c39d1 makes it work:
$ echo "nzE+iKr82Kh8BOQg0k/LViTZJup+9DReAsXd/PCtFZP5FHM7WtJ9Nz1NmqMi9G0i7rGIvhK2jRcGnFyWDT9MLoJvY1gZKI2xsUuS3nJ/n3T1Pe//4kKId+B3wfDW/TgqX6Hg/kUj8JO08wGe9JxtOEJ6XJA3cO/cSna9v3YVf/ssHTbXkb+bFgY7WLdHJyvF6lD/wfpY2ZnA1787ajtm+/aWWVMxDOwKuqIT1ZZ0Nw4=" | openssl enc -aes-256-ecb -d -a -K 3336366137346362336339353964653137643631646233303539316333396431 | base64
RG9udCB3b3JyeSBzYWtldCBvbmUgZGF5IHdlIHdpbGwgcmVhY2ggdG8Kb3VyIGRlc3RpbmF0aW9u
IHZlcnkgc29vbi4gQW5kIGlmIHlvdSBmb3JnZXQgCnlvdXIgdXNlcm5hbWUgdGhlbiB1c2UgeW91
ciBvbGQgcGFzc3dvcmQKPT0+ICJ0cmlidXRlX3RvX2lwcHNlYyIKClZpY3RvciwPDw8PDw8PDw8P
Dw8PDw8=
$

How to decrypt rijndael with command line tool

I have a third-party source of encrypted measurement data that are updated often and need decryption. I know how to decrypt the data in perl or ruby with the mcrypt library.
For documentation purposes and easy access, I would like to document how one would decrypt the ciphertext with command line tools. I have tried the mcrypt and openssl command line tools and cannot seem to decrypt the ciphertext correctly with the command line tools.
The data is encrypted with rijndael-128 in mode ecb. This is outside of my control.
Given the following minimal example:
The encrypted data is stored in file "./ciphertext" in binary.
The ciphertext is the sequence of these bytes: 0xfb 0x0d 0xfb 0xa2 0xfc 0x43 0x0a 0xe5 0xe8 0x8b 0x25 0xac 0x06 0x9c 0xdd 0x77
The file can be created e.g. in bash with printf '\xfb\x0d\xfb\xa2\xfc\x43\x0a\xe5\xe8\x8b\x25\xac\x06\x9c\xdd\x77' >/tmp/ciphertext
The encryption key is 32 repeating bytes of value 121 (that would be 32 lowercase "y"s in ASCII)
I can decrypt the cyphertext in ruby with mcrypt like this:
require "rubygems"
require "mcrypt"
key = "y"*32
ciphertext = IO.read("ciphertext", :encoding => "BINARY")
puts(Mcrypt.new("rijndael-128", :ecb, "y"*32).decrypt(ciphertext))
and in Perl like this:
#!/usr/bin/perl
use Crypt::Rijndael;
my $key = ("y" x 32);
my $ciphertext;
open(my $fh, '<', "ciphertext") or die "cannot open ciphertext";
{
local $/;
$ciphertext = <$fh>;
};
my $cipher = Crypt::Rijndael->new($key, Crypt::Rijndael::MODE_ECB());
print($cipher->decrypt($ciphertext) . "\n");
I would like to know how to decrypt ciphertext encrypted like this with command line tools, preferably openssl, or mcrypt. I have tried these invocations, but I cannot get them right apparently:
$ mcrypt -k yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy -a rijndael-128 -m ECB -d ciphertext
Warning: It is insecure to specify keywords in the command line
An OpenPGP encrypted file has been detected.
Unknown suffix. Will append '.dc'.
File ciphertext was NOT decrypted successfully.
$ openssl enc -aes-256-ecb -d -a -K 7979797979797979797979797979797979797979797979797979797979797979 -in ciphertext -out file.txt
bad decrypt
140057024816256:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:../crypto/evp/evp_enc.c:559:
The -a option tells openssl the cipher is base64 encoded. But that's wrong, it is not, in fact, base64 encoded. Also, because no padding was used on encryption you also need to specify the -nopad option.
openssl aes-256-ecb -d -nopad -K 7979797979797979797979797979797979797979797979797979797979797979 -in ciphertext

Using Openssl to decrypt AES 128 bits CBC based64 text

I am trying to use Openssl to decrypt the following AES 128 bits CBC based64 text (not padded) contained in the input.txt
bzxCHMWF+KVMumKb6rXTJQ0803fpYyxgdtZ8/nvc0Fc=
the first 16bytes are the IV. the encryption key is h4ckth1sk3yp4d16
I tried using the command:
openssl enc -aes-128-cbc -d -base64 -K 6834636b746831736b33797034643136 -iv 627a7843484d57462b4b564d756d4b62 -in input.txt -out result.txt
in the input.txt, I remove the first 16 bytes of the encrypted text when running the above command.
but I kept getting the error:
bad decrypt 3073874120:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:evp_enc.c:532:
can anyone tell what's wrong here? thanks!

Openssl "data greater than mod len"

What does the "data is greater than mod len" error message mean? I have encountered this while trying to decrypt data using php's openssl_private_decrypt. How does one go about solving this issue? Been searching hours online, not getting anywhere.
Asymmetric RSA keys can encrypt/decrypt only data of limited length i.e. RSAES-PKCS1-v1_5 encryption scheme defined in RFC3447 can operate on messages of length up to k - 11 octets (k is the octet length of the RSA modulus) so if you are using 2048-bit RSA key then maximum length of the plain data to be encrypted is 245 bytes.
If you are having this decryption error: RSA_EAY_PRIVATE_DECRYPT:data greater than mod len try this command before decrypt your file:
cat yourEncryptedFile| base64 -D > yourEncryptedRawFile
You can also try openssl enc -in cipherTextFile.base64 -out binaryTextFile.bin -d -a. This was what worked for me when I got this error while trying to decrypt. I was then able to decrypt using openssl rsautl -decrypt -in binaryTextFile.bin -out plainTextFile.txt -inkey my-private-key.pem without failure.

Resources