AES Encryption Question

Status
Not open for further replies.

MonaLisaOverdrive

Ars Legatus Legionis
10,332
Subscriptor++
Not sure if this is the right forum in which to post this question so feel free to suggest others.

Is it normal for the ciphertext from a string encrypted with ConvertFrom-SecureString with keys of different lengths used in the -Key param to:
  • have the same beginning 36 characters?
  • display repeated patterns in the text?

Example:
The following is the output from the same string encrypted with keys of varying lengths, with the first 36 characters separated from the rest of the string. For the ciphertext from the 256bit key, I've separated out the first two patterns.

Code:
265bit
76492d1116743f0423413b16050a5345MgB8 AFUA cwBk ACsA WgBRAHoAZwBFAEoAcABMAFEAbgBnAHcAbQAyAHMAQQByAHcAPQA9AHwAZgA2ADkANwBlADEAOABjADQANAA2ADUANQA4ADQANABlAGEAZQA5ADkANAA4ADQANQA1AGYAZQBmADcAZgAzADIAMAAwADIANQBiAGIAZQBjADAAOQA1AGEAMABiAGUANwBjAGMAMABiADAANgA2AGMANQAyADgAZQBlADMANgBjADYANABjADUAYQA2ADgAZQBlADcAZQA2AGQAZgAyADIANQAxADYANgA0ADUAZgA1ADMAZAAyADcAYgAwAGIA
192bit
76492d1116743f0423413b16050a5345MgB8 AE4AdwB0AFUAYwBmAFIAMwBSAHgANAA4ADEAZwBQAFQAaABXAGUARwBUAHcAPQA9AHwAZgAxADMAOQAwAGEAYwBmADMANAA1ADIANQA1ADcANgBjADcAYwA3ADEAMQBhADAAMgBkADcANABkADUAMABjADgANwBkADYAOQA2ADAAYgA1ADEAMQAyADAAMQA5ADYAOAAzADEAZgAyADkANQA1ADIAOAAxAGIANQA5ADAANAAwADEAMQBhADIAMQBmADgANgA3ADcAMgBlAGQANQBiAGEAYgA1ADQAYQAxAGEAMQA0AGUAZABiADIAMABiAGUA
128bit
76492d1116743f0423413b16050a5345MgB8 AC8AbQB2ADQAQwB4AHcAOQBrAEUAaQBlAGcAVAB4AHYASQBtAGkAcQBnAHcAPQA9AHwAZQA5ADIAMAAwAGEAZgAwAGYANgA4AGMAMQBlAGIAOQBlADMANgAyAGQAYgAxAGYANwAxADIAMQA5ADYANwBiAGQAZAA5ADgAMwA1ADkANAAxAGMANwBhADgAMQA0ADkANQA1ADQAMABlAGYAZgAwADUAYgAyAGMAMgBkAGUAZABlADYAYQBiADAAYQAxADYAMQA3AGIAOABjAGIAMQAxAGMAOAAwADQAYQA1ADkANgAxADgANAA1ADQAOQBmADEA

Keep in mind that I've a beginner's level of encryption knowledge when you reply.
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
I'm not sure what language / library ConvertFrom-SecureString is from. But in general when you get one of these opaque strings that are only supposed to be compared with output from the same library they have an internal structure with things like version numbers and the like that could well mean shared segments.

For example the de facto password hash format is: an identifier for the algorithm, a cost parameter, the salt used, and only then the hash value. Many bcrypt hashes you see end up starting out with $2a$10$
 

MonaLisaOverdrive

Ars Legatus Legionis
10,332
Subscriptor++
I'm not sure what language / liberary ConvertFrom-SecureString is from. But in general when you get one of these opaque strings that are only supposed to be compared with output from the same library they have an internal structure with things like version numbers and the like that could well mean shared segments.

It's PowerShell, which is based off of .NET. ConvertFrom-SecureString manipulates a System.Security.SecureString object.

For example the de facto password hash format is: an identifier for the algorithm, a cost parameter, the salt used, and only then the hash value. Many bcrypt hashes you see end up starting out with $2a$10$

This is probably a stupid question, but does hash = ciphertext?
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
Not a stupid question. They are somewhat different. A hash function takes arbitrary length input and produces a fixed length output. It isn't reversible. A symmetric cipher takes a key and fixed length input and produces a fixed length output. It is reversible.

But the hash value is the analogous output to the ciphertext for a symmetric cipher.
 

MonaLisaOverdrive

Ars Legatus Legionis
10,332
Subscriptor++
Not a stupid question. They are somewhat different. A hash function takes arbitrary length input and produces a fixed length output. It isn't reversible. A symmetric cipher takes a key and fixed length input and produces a fixed length output. It is reversible.

But the hash value is the analogous output to the ciphertext for a symmetric cipher.

EDIT: Answered own question

For example the de facto password hash format is: an identifier for the algorithm, a cost parameter, the salt used, and only then the hash value. Many bcrypt hashes you see end up starting out with $2a$10$

So, other than the starting characters, does the ciphertext from a bcrypt hash contain recognizable patterns?
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
Not a stupid question. They are somewhat different. A hash function takes arbitrary length input and produces a fixed length output. It isn't reversible. A symmetric cipher takes a key and fixed length input and produces a fixed length output. It is reversible.

But the hash value is the analogous output to the ciphertext for a symmetric cipher.

So a hash function is, or is part of, an asymmetric cipher?
No. A hash function is a primitive that acts like a fingerprint.

Suppose you want to make sure that two big files on two different systems are identical. Hash the file on each system and you'll end up with a short output (e.g. 128 bit) fingerprint. Then you can just compare the fingerprints -- if they are the same than there's a very high probability that the files are identical.

A secure hash is one where it is very difficult to construct an input string to produce a given output string or to take an output string and produce an input string that could have produced it.

An asymmetric cipher is as you say public / private key encryption.

For example the de facto password hash format is: an identifier for the algorithm, a cost parameter, the salt used, and only then the hash value. Many bcrypt hashes you see end up starting out with $2a$10$

So, other than the starting characters, does the ciphertext from a bcrypt hash contain recognizable patterns?
Not a ciphertext, let's say the output instead. And it shouldn't, no. The salt part should be random and the hash part should be indistinguishable from random.
 

MonaLisaOverdrive

Ars Legatus Legionis
10,332
Subscriptor++
I guess I'm back to my first question: is it unusual for ciphertexts generated with different key lengths to have the same starting characters and to display discernible patterns.
If I'm understanding the documentation for the cmdlet correctly, it uses AES encryption with the possible key lengths of 128, 192 or 256 with the -Key parameter.
 

MonaLisaOverdrive

Ars Legatus Legionis
10,332
Subscriptor++
Are you trying to decrypt or encrypt? You are using "ConvertFrom" so you should be starting with cipher text. Depending on which AES algorithm is used by the underlying algorithm and how the key is generated, those leading 36 characters could be a nonce or counter or both.

ConvertFrom takes a System.Security.SecureString and converts it into an encrypted standard string. Whether it means encrypt or decrypt I don't know, but I'm guessing encrypt plus something else. Feel free, anyone else, to correct me if I'm wrong.

The key is generated using the following PowerShell/.NET code

Code:
$rng = [System.Security.Cryptography.RNGCryptoServiceProvider]::Create()
$key = New-Object byte[](32)

$rng.GetBytes($key)

Depending on which AES algorithm is used by the underlying algorithm and how the key is generated, those leading 36 characters could be a nonce or counter or both.

Thanks, this helps. Off to research nonces and counters.
What about the (semi) repeating pattern? Is there anything that would explain it?
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
I think I tracked down the code that's writing this out:
Code:
if (encryptionResult != null)
{
    // The formatted string is Algorithm Version,
    // Initialization Vector, Encrypted Data
    string dataPackage = String.Format(
        System.Globalization.CultureInfo.InvariantCulture,
        "{0}|{1}|{2}",
        2,
        encryptionResult.IV,
        encryptionResult.EncryptedData);

    // encode the package, and output it.
    // We also include a recognizable prefix so that
    // we can use the old decryption mechanism if we
    // don't see it. While the old decryption
    // generated invalid data for the first bit of the
    // SecureString, it at least didn't generate an
    // exception.
    byte[] outputBytes = System.Text.Encoding.Unicode.GetBytes(dataPackage);
    string encodedString = Convert.ToBase64String(outputBytes);
    WriteObject(SecureStringHelper.SecureStringExportHeader + encodedString);
}
From here: https://github.com/PowerShell/PowerShel ... ommands.cs

SecureStringHelper is here: https://github.com/PowerShell/PowerShel ... gHelper.cs

and SecureStringHelper.SecureStringExportHeader is defined as:
Code:
internal static string SecureStringExportHeader = "76492d1116743f0423413b16050a5345";
on line 25.

So that explains the first part. Then is supposed to come
Code:
2|IV|encryptedData
base64 encoded. The IV should be random (see line 284 of SecureStringHelper.cs) and the encryptedData should be indistinguishable from random.

When I take the second string you posted and I base64 decode the part after that initial header I get NUL characters interspersed at every other spot with a regular character. My guess is that c# is using UTF-16. Interpreted that way the decoded string is:

2|NwtUcfR3Rx481gPThWeGTw==|f1390acf34525576c7c711a02d74d50c87d6960b51120196831f2955281b5904011a21f86772ed5bab54a1a14edb20be

It looks like the two parts -- the IV and the encrypted data -- use different encoding. The IV is base 64 encoded, the == padding is there because the number of bytes in the IV is 128, which modulo 3 is 2. That means two padding characters were needed, which in base 64 are equals signs. Meanwhile the encrypted data is just written out in lowercase hex. Because the 2, the pipes and the == are always going to be the same that explains the commonality across the different strings.

For completeness sake here are the other two decodes
2|/mv4Cxw9kEiegTxvImiqgw==|e9200af0f68c1eb9e362db1f7121967bdd9835941c7a81495540eff05b2c2dede6ab0a1617b8cb11c804a596184549f1

and

2|Usd+ZQzgEJpLQngwm2sArw==|f697e18c44655844eae9948455fef7f320025bbec095a0be7cc0b066c528ee36c64c5a68ee7e6df22516645f53d27b0b

---

This doesn't seem like an especially logical or consistent way to do this, but I imagine there were a lot of backward compatibility concerns.
 
Status
Not open for further replies.