2

How would I be able to determine the encryption of a key (AES256 or 3DES 256)...Since both keys will be 32 characters (8 bits per char * 32 char)=256 bits and Mime encoded.

Example

MQAyAEgAOgA5ADUAMwA3AD8AQgBFAD4A --->AES256 key

g1EOWGFb+JjCZ7BbH2RergtKUtDfXrNb --->3DES key

The AES keys were made in Openssl while the 3DES ones were made using Java with the following Apis.

javax.crypto.Cipher;
 javax.crypto.KeyGenerator;
 javax.crypto.SecretKey;
 javax.crypto.SecretKeyFactory;
 javax.crypto.spec.DESedeKeySpec;
 javax.crypto.spec.IvParameterSpec;
user2402616
  • 1,434
  • 4
  • 22
  • 38

1 Answers1

2

First of all, there is no such thing as 3DES 256. 3DES has a key size of 128 or 192 bits, of which 112 and 168 bits are effectively used. Note that the security margin of 3DES is even lower.

AES on the other hand can be used with 128, 192 and 256 bits, all of which are used.

Now base 64 (not SMIME, that's a higher level protocol) has 6 bits per character (not excluding spurious bits at the end). If I check your keys both of them are 192 bit in size, so that won't help you distinguish the keys. You can use the Apache Codec library to decode base 64 strings.

However, your 3DES key - the second one - seems to use odd parity bytes for the 3 single DES keys. That can be used to distinguish the keys from each other. Note that this is not foolproof, a randomly generated AES key may have the parity bits set correctly by chance alone. However, the chance of that happening is somewhere around the order of 2^24.

It is possible to use the method DESedeKeySpec.isParityAdjusted(byte[] key, int offset) to check if the parity is correctly set. It is required to decode the base 64 string first of course.

Note that sometimes 3DES keys are distributed without having the parity set correctly. In your case, you need to use the KeyFactory to generate the keys otherwise the parity may not be set.

Another way of checking if the key is of the correct type is to decrypt some known plaintext/ciphertext/secretkey pair using both algorithms.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • @Duncan Jones My AES key was made by encoding a random string in Base64. My 3DES key was made using SecretKeyFactory – user2402616 May 30 '13 at 14:15
  • The following is the code used to generate my 3DES keys...perhaps it will be useful in detecting if already made keys came from it. – user2402616 May 30 '13 at 15:11
  • public static String generateKey() { try { KeyGenerator newKey = KeyGenerator.getInstance("DESede"); SecretKey key = newKey.generateKey(); return Base64.encodeBytes(key.getEncoded()); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; } – user2402616 May 30 '13 at 15:12
  • So if the first 7 bits in each byte are for ascii data...am I to assume the last bit is an odd parity? – user2402616 May 30 '13 at 17:37
  • No the first 7 bits should be random binary data, not ASCII data. I've updated my answer to include base 64 decoding and a method that indicates if the parity is correctly set. – Maarten Bodewes May 30 '13 at 17:41
  • The method you introduced will be handy only if AES keys do not have parity bits, because then there would be no way to distinguish them. – user2402616 May 30 '13 at 17:51
  • ^ I.E. it would return true for both keys – user2402616 May 30 '13 at 17:54
  • AES keys do not have parity bits, so that's ok then. Please try and study the subject at hand... As said, 24 bytes, if they all contain just random data then there are 24 parity bits, so the chance is 1 in 2^24 for an random generated AES key to have parity set correctly by chance. That's about 1 in 16 million, if I'm not mistaken. – Maarten Bodewes May 30 '13 at 18:05
  • I'm going to make all my AES-192 and AES-256 key bytes have odd parity just to frustrate you :) – President James K. Polk May 30 '13 at 23:57
  • @GregS that would be an odd way of getting even :P – Maarten Bodewes Jun 01 '13 at 22:27
  • Thanks! I am now able to distinguish between the two keys. Also, I am not concerned about the safety of the keys. – user2402616 Jun 04 '13 at 18:47