Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate encrypt/decrypt #5

Closed
ajitam opened this issue Mar 7, 2016 · 6 comments
Closed

Separate encrypt/decrypt #5

ajitam opened this issue Mar 7, 2016 · 6 comments

Comments

@ajitam
Copy link

ajitam commented Mar 7, 2016

Hi, so I continue my work using you script and I hit a problem. I'm not 100% this is an issue, because the logic changed and I'm still trying to wrap my head around the code. So here we go:

I prepared a simple script which:

  • encrypts 2bytes (0xAA 0xEE)

  • uses 10 iterations

    #include <Cape.h>
    Cape cape("oEpHgSPFvZ6TzrzkaKMX", 10);
    
    #define DO_ENCRYPT 1
    #define DO_DECRYPT 1
    
    void setup() {
      Serial.begin(115200);
    }
    
    void loop() {
    
    #if DO_ENCRYPT
      char in[] = {0xAA, 0xEE};
    
      cape.encrypt(in, 2);
      byte enPacket_1 = cape.result[0];
      byte enPacket_2 = cape.result[1];
      byte enPacket_3 = cape.result[2];
    
      Serial.print("0x");
      Serial.print(enPacket_1, HEX);
      Serial.print(" | ");
      Serial.print(enPacket_2, HEX);
      Serial.print(" | ");
      Serial.println(enPacket_3, HEX);
    
      delay(1000);
    #endif
    
    #if DO_DECRYPT
      char out[] = {enPacket_1, enPacket_2, enPacket_3};
      //char out[] = {0xC9, 0xE2, 0x3B};
    
      cape.decrypt(out, 2);
      byte dePacket_1 = cape.result[0];
      byte dePacket_2 = cape.result[1];
    
      Serial.print("0x");
      Serial.print(dePacket_1, HEX);
      Serial.print(" | ");
      Serial.println(dePacket_2, HEX);
    
      delay(1000);
    #endif
    
    }

Steps

  1. If I set DO_ENCRYPT 1 and DO_DECRYPT 1 I get this

    0xC1 | EA | 33
    0xAA | EE
    0x81 | AA | 73
    0xAA | EE
    0x5B | 70 | A9
    0xAA | EE
    0x17 | 3C | E5
    0xAA | EE
    0xDF | F4 | 2D
    0xAA | EE
    0xEF | C4 | 1D
    0xAA | EE
    0xAB | 80 | 59
    0xAA | EE
    

    which is correct.

  2. Then I set set DO_ENCRYPT 1 and DO_DECRYPT 0 and I get this

    0xC1 | EA | 33
    0x9 | 22 | FB
    0x15 | 3E | E7
    0xAB | 80 | 59
    0xEB | C0 | 19
    0x23 | 8 | D1
    0x3B | 10 | C9
    0x7B | 50 | 89
    
  3. I then:

    • select one of the (encrypted) values (lets say 0xAB 80 59) and I replace char out[] with this values like so char out[] = {0xAB, 0x80, 0x59};
    • set DO_ENCRYPT 0 and DO_DECRYPT 1
      and I get this
    0xF3 | B7
    0xB6 | F2
    0xB6 | F2
    0xB6 | F2
    0xB6 | F2
    

Problem

Any ideas why data is not decrypted to the same value. As far I understand 3th byte is initialization byte which just "mangles" data.

gioblu added a commit that referenced this issue Mar 8, 2016
@gioblu
Copy link
Owner

gioblu commented Mar 8, 2016

@ajitam Hi! Thank you again for your support and betatest.
You tested an unreleased not stable version of Cape.

I this week end and today worked to release a new stable version with a different and more secure encryption method. After various tests I decided to get rid of iterations, to save computation time (because of multiple function call). I ended up with a 3 step non-iteration-tunable encryption vastly based on private key and data hashing.

I also used your example to doublecheck its correct functionality:

#include <Cape.h>
Cape cape("oEpHgSPFvZ6TzrzkaKMX", 20);
#define DO_ENCRYPT 1
#define DO_DECRYPT 1

void setup() {
  Serial.begin(115200);
}

void loop() {
#if DO_ENCRYPT
  char in[] = {10, 10};
  cape.encrypt(in, 2);
  byte enPacket_1 = cape.result[0];
  byte enPacket_2 = cape.result[1];
  byte enPacket_3 = cape.result[2];

  Serial.print(enPacket_1);
  Serial.print(" | ");
  Serial.print(enPacket_2);
  Serial.print(" | ");
  Serial.println(enPacket_3);

  delay(1000);
#endif

#if DO_DECRYPT
  char out[] = {enPacket_1, enPacket_2, enPacket_3};

  // From static crypted string back to 10, 10
  //char out[] = {108, 65, 13};

  cape.decrypt(out, 3);
  byte dePacket_1 = cape.result[0];
  byte dePacket_2 = cape.result[1];

  Serial.print(dePacket_1);
  Serial.print(" | ");
  Serial.println(dePacket_2);
  delay(1000);
#endif
}

Changelog:

  • In Cape definition, you have to pass (as explained in the updated readme) the key and its length.
  • You have to pass to decrypt the real length of the string (counting also the initialization vector).

I am sorry for the temporary instability of the code.

@gioblu
Copy link
Owner

gioblu commented Mar 8, 2016

@ajitam
Copy link
Author

ajitam commented Mar 8, 2016

Is this new encryption based on some other method? (previous one was similar to RC4)

@gioblu
Copy link
Owner

gioblu commented Mar 8, 2016

Hi @ajitam. I would not indicate any method in particular as "inspiration" for this. Other users complained because RC4 is holed like Emmenthal, so I decided to work on a new, dedicated method based on standard and proved effective methods to encrypt a string.

@gioblu
Copy link
Owner

gioblu commented Mar 8, 2016

@ajitam I tried to be as clear as possible in the code's comments.
Let me know what do you think about it.
Distribution seems really better than before.

@ajitam
Copy link
Author

ajitam commented Mar 9, 2016

I'm not an expert in cryptology so frankly - I don't know which method is better and what are there flaws.
Thing is that currently I'm searching for the encryption to encrypt 2 or 3 bytes and I have 4 bytes of storage.
Buy my setup is a bit special:

  • encrypted data is always different
  • encryption key is always different

(closing this issue - sorry I didn't do it earlier)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants