![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Ritesh
main.cpp@0:20df98087dc9, 2017-05-05 (annotated)
- Committer:
- ritesh2094
- Date:
- Fri May 05 20:14:32 2017 +0000
- Revision:
- 0:20df98087dc9
Encrypt_Decrypt1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ritesh2094 | 0:20df98087dc9 | 1 | #include "mbed.h" |
ritesh2094 | 0:20df98087dc9 | 2 | |
ritesh2094 | 0:20df98087dc9 | 3 | //------------------------------------ |
ritesh2094 | 0:20df98087dc9 | 4 | // Hyperterminal configuration |
ritesh2094 | 0:20df98087dc9 | 5 | // 9600 bauds, 8-bit data, no parity |
ritesh2094 | 0:20df98087dc9 | 6 | //------------------------------------ |
ritesh2094 | 0:20df98087dc9 | 7 | |
ritesh2094 | 0:20df98087dc9 | 8 | #include "mbedtls/aes.h" |
ritesh2094 | 0:20df98087dc9 | 9 | |
ritesh2094 | 0:20df98087dc9 | 10 | #define IV_SIZE 16 |
ritesh2094 | 0:20df98087dc9 | 11 | #define INPUT_SIZE 256 |
ritesh2094 | 0:20df98087dc9 | 12 | |
ritesh2094 | 0:20df98087dc9 | 13 | Serial pc(USBTX, USBRX); |
ritesh2094 | 0:20df98087dc9 | 14 | |
ritesh2094 | 0:20df98087dc9 | 15 | // Indicator LED |
ritesh2094 | 0:20df98087dc9 | 16 | DigitalOut myled(LED1); |
ritesh2094 | 0:20df98087dc9 | 17 | |
ritesh2094 | 0:20df98087dc9 | 18 | // This function initializes the initilization vector to the original one. |
ritesh2094 | 0:20df98087dc9 | 19 | void init_ivs(unsigned char iv_orig[], unsigned char iv1[], unsigned char iv2[]); |
ritesh2094 | 0:20df98087dc9 | 20 | |
ritesh2094 | 0:20df98087dc9 | 21 | |
ritesh2094 | 0:20df98087dc9 | 22 | int main() |
ritesh2094 | 0:20df98087dc9 | 23 | { |
ritesh2094 | 0:20df98087dc9 | 24 | int i=0; |
ritesh2094 | 0:20df98087dc9 | 25 | |
ritesh2094 | 0:20df98087dc9 | 26 | // We create two AES contexts |
ritesh2094 | 0:20df98087dc9 | 27 | mbedtls_aes_context aes; |
ritesh2094 | 0:20df98087dc9 | 28 | mbedtls_aes_context aes2; |
ritesh2094 | 0:20df98087dc9 | 29 | |
ritesh2094 | 0:20df98087dc9 | 30 | // Our KEY , should be shared only with the receiver and has to be as random as possible. |
ritesh2094 | 0:20df98087dc9 | 31 | // Here we use this simple example for ease and simplicity |
ritesh2094 | 0:20df98087dc9 | 32 | unsigned char key[32] = "1234567812345678123456781234567" ; |
ritesh2094 | 0:20df98087dc9 | 33 | key[31] = '8'; // we replace the 32th (index 31) which contains '/0' with the '8' char. |
ritesh2094 | 0:20df98087dc9 | 34 | |
ritesh2094 | 0:20df98087dc9 | 35 | // This is the Init Vector generated with the random KEY from http://aes.online-domain-tools.com/ |
ritesh2094 | 0:20df98087dc9 | 36 | unsigned char orig_iv[] = {0x8d, 0xf8, 0x4d, 0xed, 0x5f, 0xbd, 0x51, 0x57, 0x5c, 0x55, 0x17, 0xb0, 0x66, 0xee, 0xb1, 0x2b}; // e053a4a22fd6bfaf43fd5e8b5"; {0xb2, 0x4b, 0xf2, 0xf7, 0x7a, 0xc5, 0xec, 0x0c, 0x5e, 0x1f, 0x4d, 0xc1, 0xae, 0x46, 0x5e, 0x75}; |
ritesh2094 | 0:20df98087dc9 | 37 | unsigned char iv1[IV_SIZE] = {0}; |
ritesh2094 | 0:20df98087dc9 | 38 | unsigned char iv2[IV_SIZE] = {0}; |
ritesh2094 | 0:20df98087dc9 | 39 | |
ritesh2094 | 0:20df98087dc9 | 40 | // This is the text that we want to encrypt and send |
ritesh2094 | 0:20df98087dc9 | 41 | unsigned char my_txt[] = "My name is Ritesh Reddy Sadula"; |
ritesh2094 | 0:20df98087dc9 | 42 | |
ritesh2094 | 0:20df98087dc9 | 43 | unsigned char input[INPUT_SIZE] = {0}; |
ritesh2094 | 0:20df98087dc9 | 44 | |
ritesh2094 | 0:20df98087dc9 | 45 | for(int m=0; m<strlen((const char*)my_txt); m++) |
ritesh2094 | 0:20df98087dc9 | 46 | { |
ritesh2094 | 0:20df98087dc9 | 47 | input[m] = *(my_txt+m); |
ritesh2094 | 0:20df98087dc9 | 48 | } |
ritesh2094 | 0:20df98087dc9 | 49 | |
ritesh2094 | 0:20df98087dc9 | 50 | |
ritesh2094 | 0:20df98087dc9 | 51 | // Output of encryption |
ritesh2094 | 0:20df98087dc9 | 52 | unsigned char output[INPUT_SIZE] = {0}; |
ritesh2094 | 0:20df98087dc9 | 53 | // Output of decryption |
ritesh2094 | 0:20df98087dc9 | 54 | unsigned char output2[INPUT_SIZE] = {0}; |
ritesh2094 | 0:20df98087dc9 | 55 | |
ritesh2094 | 0:20df98087dc9 | 56 | int ret =0; |
ritesh2094 | 0:20df98087dc9 | 57 | |
ritesh2094 | 0:20df98087dc9 | 58 | // To be a valid length, it has to be a multiple of 16, so we take the muliple of 16 that is nearly bigger than the actual size |
ritesh2094 | 0:20df98087dc9 | 59 | // Or the |
ritesh2094 | 0:20df98087dc9 | 60 | int TXT_VALID_LENGTH = ((strlen((const char*)my_txt)%16 == 0) ? strlen((const char*)my_txt) : (strlen((const char*)my_txt)/16 + 1)*16); |
ritesh2094 | 0:20df98087dc9 | 61 | while(1){ |
ritesh2094 | 0:20df98087dc9 | 62 | init_ivs(orig_iv , iv1, iv2); |
ritesh2094 | 0:20df98087dc9 | 63 | |
ritesh2094 | 0:20df98087dc9 | 64 | |
ritesh2094 | 0:20df98087dc9 | 65 | mbedtls_aes_setkey_enc( &aes, key, 256); |
ritesh2094 | 0:20df98087dc9 | 66 | ret = mbedtls_aes_crypt_cbc( &aes, MBEDTLS_AES_ENCRYPT, TXT_VALID_LENGTH, iv1, input, output ); |
ritesh2094 | 0:20df98087dc9 | 67 | pc.printf("%d - Ret: %d - strlen(input)=%d - ENCRYPTED output %s\n", i, ret , TXT_VALID_LENGTH, output); |
ritesh2094 | 0:20df98087dc9 | 68 | |
ritesh2094 | 0:20df98087dc9 | 69 | // We can use 64encoding to send data and to avoid encoding problems |
ritesh2094 | 0:20df98087dc9 | 70 | // In our case we use haxadecimal representation of the characters so no need. |
ritesh2094 | 0:20df98087dc9 | 71 | // mbedtls_base64_encode(encoded64, 200, &out_len, (const unsigned char *)output, strlen((const char*)output) ); |
ritesh2094 | 0:20df98087dc9 | 72 | // printf("%d 64ENCODED ENCRYPTED output %s\n", i, encoded64); |
ritesh2094 | 0:20df98087dc9 | 73 | |
ritesh2094 | 0:20df98087dc9 | 74 | pc.printf("%d HEX ENCRYPTED output\n", i); |
ritesh2094 | 0:20df98087dc9 | 75 | for(int j=0; j<TXT_VALID_LENGTH; j++) |
ritesh2094 | 0:20df98087dc9 | 76 | { |
ritesh2094 | 0:20df98087dc9 | 77 | pc.printf("%02x ", output[j]); |
ritesh2094 | 0:20df98087dc9 | 78 | } |
ritesh2094 | 0:20df98087dc9 | 79 | pc.printf("\n"); |
ritesh2094 | 0:20df98087dc9 | 80 | |
ritesh2094 | 0:20df98087dc9 | 81 | // We decrypt the OUTPUT1 and we get the same input string |
ritesh2094 | 0:20df98087dc9 | 82 | |
ritesh2094 | 0:20df98087dc9 | 83 | mbedtls_aes_setkey_dec( &aes2, key, 256); |
ritesh2094 | 0:20df98087dc9 | 84 | ret = mbedtls_aes_crypt_cbc(&aes2, MBEDTLS_AES_DECRYPT, TXT_VALID_LENGTH, iv2, output, output2 ); |
ritesh2094 | 0:20df98087dc9 | 85 | pc.printf("%d - Ret: %d - DECRYPTED output %s\n\n", i , ret, output2); |
ritesh2094 | 0:20df98087dc9 | 86 | |
ritesh2094 | 0:20df98087dc9 | 87 | // Showing the decrypted result in HEX |
ritesh2094 | 0:20df98087dc9 | 88 | pc.printf("%d HEX DECRYPTED output\n", i, output2); |
ritesh2094 | 0:20df98087dc9 | 89 | for(int j=0; j<TXT_VALID_LENGTH; j++) |
ritesh2094 | 0:20df98087dc9 | 90 | { |
ritesh2094 | 0:20df98087dc9 | 91 | pc.printf("%02x ", output2[j]); |
ritesh2094 | 0:20df98087dc9 | 92 | } |
ritesh2094 | 0:20df98087dc9 | 93 | pc.printf("\n\n\n"); |
ritesh2094 | 0:20df98087dc9 | 94 | |
ritesh2094 | 0:20df98087dc9 | 95 | i++; |
ritesh2094 | 0:20df98087dc9 | 96 | // Wait 2 seconds and re-do it. |
ritesh2094 | 0:20df98087dc9 | 97 | wait(2); |
ritesh2094 | 0:20df98087dc9 | 98 | } |
ritesh2094 | 0:20df98087dc9 | 99 | |
ritesh2094 | 0:20df98087dc9 | 100 | } |
ritesh2094 | 0:20df98087dc9 | 101 | |
ritesh2094 | 0:20df98087dc9 | 102 | void init_ivs(unsigned char iv_orig[], unsigned char iv1[], unsigned char iv2[]) |
ritesh2094 | 0:20df98087dc9 | 103 | { |
ritesh2094 | 0:20df98087dc9 | 104 | for(int i=0; i<IV_SIZE; i++){ |
ritesh2094 | 0:20df98087dc9 | 105 | |
ritesh2094 | 0:20df98087dc9 | 106 | iv2[i] = iv1[i] = iv_orig[i]; |
ritesh2094 | 0:20df98087dc9 | 107 | |
ritesh2094 | 0:20df98087dc9 | 108 | } |
ritesh2094 | 0:20df98087dc9 | 109 | } |