Willie Solano
/
Encypting_Funcional
Encypting_Funcional
main.cpp@0:fb407fe1db0f, 2019-06-04 (annotated)
- Committer:
- williequesada
- Date:
- Tue Jun 04 17:53:15 2019 +0000
- Revision:
- 0:fb407fe1db0f
pablo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
williequesada | 0:fb407fe1db0f | 1 | #include "mbed.h" |
williequesada | 0:fb407fe1db0f | 2 | #include <stdio.h> |
williequesada | 0:fb407fe1db0f | 3 | #include <stdlib.h> |
williequesada | 0:fb407fe1db0f | 4 | |
williequesada | 0:fb407fe1db0f | 5 | #include "mbedtls/rsa.h" |
williequesada | 0:fb407fe1db0f | 6 | #include "mbedtls/pk.h" |
williequesada | 0:fb407fe1db0f | 7 | #include "mbedtls/base64.h" |
williequesada | 0:fb407fe1db0f | 8 | #include "mbedtls/ctr_drbg.h" |
williequesada | 0:fb407fe1db0f | 9 | #include "mbedtls/error.h" |
williequesada | 0:fb407fe1db0f | 10 | #include "mbedtls/ctr_drbg.h" |
williequesada | 0:fb407fe1db0f | 11 | #include "mbedtls/platform.h" |
williequesada | 0:fb407fe1db0f | 12 | #include "mbedtls/entropy.h" |
williequesada | 0:fb407fe1db0f | 13 | |
williequesada | 0:fb407fe1db0f | 14 | // ------------------Examples keys from http://travistidwell.com/jsencrypt/demo/ |
williequesada | 0:fb407fe1db0f | 15 | |
williequesada | 0:fb407fe1db0f | 16 | static const uint8_t PrivateKeyTXT[] = |
williequesada | 0:fb407fe1db0f | 17 | "-----BEGIN RSA PRIVATE KEY-----\n" |
williequesada | 0:fb407fe1db0f | 18 | "MIICXQIBAAKBgQCtlciI/Mk4fkxeOwiB0iwiBmoSyhqQNKBg+KmzpZDcLkl8ShDY\n" |
williequesada | 0:fb407fe1db0f | 19 | "/BjZjW9UXYX8ptCHwV9WU4ltdA4fBTm51m52fOGvZPODurIbNEnaFMRn9hsDJRA6\n" |
williequesada | 0:fb407fe1db0f | 20 | "adz32XRQVsjQYLMnQnlaMYGKvuatHPPK/ZCuJRmdttiijs3t/bmCU/Vi8wIDAQAB\n" |
williequesada | 0:fb407fe1db0f | 21 | "AoGBAJ9alhJMHl3idDE9eH3Vd/Z0dQRyzRf9eA4NuZzWLbo0M/oJ9JV+OUHe+Yk1\n" |
williequesada | 0:fb407fe1db0f | 22 | "oj9i1KUL/H2/irYUiLIplejo1dYkpdD8gEBpNK5HLh9rd2IPxyyS8PkkeKwWnj35\n" |
williequesada | 0:fb407fe1db0f | 23 | "4N4CEf8d8+JF/VA2KvPVyqUC8m8JAFwQkNioyZmGnFhJ/m5hAkEA4irJ4emRtr5T\n" |
williequesada | 0:fb407fe1db0f | 24 | "U2ic4Vbvb7k8rnh/xBpPRtJbx219VQItxFECdm6bIExZevVNGZx2YKZIsklVUIbu\n" |
williequesada | 0:fb407fe1db0f | 25 | "wFjBzXY/JQJBAMR7Z3fEKK2NqX+BgbecdPimYce0QSbMpZ1Co9U6tts3yT2sKaqQ\n" |
williequesada | 0:fb407fe1db0f | 26 | "8Zz0l8/0eNQlBe9PUBywwhRhkplqOE2e6jcCQCcez5zMEfBSK4eZ6MjxiKkCmYR0\n" |
williequesada | 0:fb407fe1db0f | 27 | "Q2VgR6YwbL+qKKuznMcAXDSwHRURH8ks9REhBzjgimKMXL15GYMb1yn68iECQC/A\n" |
williequesada | 0:fb407fe1db0f | 28 | "rkz1Z82GeIN8vkIZzHQCPBGFRbBgXH/0Ck1wj7CHxmkuxpGl2JME/0CTcipdNSwM\n" |
williequesada | 0:fb407fe1db0f | 29 | "YrQWYZUCgda3cierT9ECQQDZUgRkBsVsL60neHskYq2xCJO7RGxwrqekBvtXjo4i\n" |
williequesada | 0:fb407fe1db0f | 30 | "6IHgjeagH9Mf1hLVZQ7aPU3EFwk4jcb6FOhTqSqrZWKH\n" |
williequesada | 0:fb407fe1db0f | 31 | "-----END RSA PRIVATE KEY-----\n"; |
williequesada | 0:fb407fe1db0f | 32 | |
williequesada | 0:fb407fe1db0f | 33 | static const uint8_t PublicKeyTXT[] = |
williequesada | 0:fb407fe1db0f | 34 | "-----BEGIN PUBLIC KEY-----\n" |
williequesada | 0:fb407fe1db0f | 35 | "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtlciI/Mk4fkxeOwiB0iwiBmoS\n" |
williequesada | 0:fb407fe1db0f | 36 | "yhqQNKBg+KmzpZDcLkl8ShDY/BjZjW9UXYX8ptCHwV9WU4ltdA4fBTm51m52fOGv\n" |
williequesada | 0:fb407fe1db0f | 37 | "ZPODurIbNEnaFMRn9hsDJRA6adz32XRQVsjQYLMnQnlaMYGKvuatHPPK/ZCuJRmd\n" |
williequesada | 0:fb407fe1db0f | 38 | "ttiijs3t/bmCU/Vi8wIDAQAB\n" |
williequesada | 0:fb407fe1db0f | 39 | "-----END PUBLIC KEY-----\n"; |
williequesada | 0:fb407fe1db0f | 40 | |
williequesada | 0:fb407fe1db0f | 41 | // Privada ---> Encriptacion |
williequesada | 0:fb407fe1db0f | 42 | // Publica ---> Desencripta |
williequesada | 0:fb407fe1db0f | 43 | |
williequesada | 0:fb407fe1db0f | 44 | uint8_t Message[] ="{\"municipio\":\"Santa Ana\",\"id\":1067,\"place\":\"2222\",\"time\":120,\"t2\":\"%5303105315252296=2108?\"}1111111199999991111111111"; |
williequesada | 0:fb407fe1db0f | 45 | uint8_t Encryption1[200]; |
williequesada | 0:fb407fe1db0f | 46 | uint8_t Encryption[200]; |
williequesada | 0:fb407fe1db0f | 47 | uint8_t Encryption_HEX[]= "\x43\x60\x5f\x65\xbc\x78\x89\x57\x5c\x6d\xc6\x47\x53\x57\x10\xd3\x45\x0e\x77\xe0\x85\x5f\xf8\x3d\x32\x6e\xcf\x94\x4c\x1f\xa8\x11\xe6\x6b\x93\x5e\xc7\x37\xe2\xee\x63\x99\x6e\x80\x70\x6e\xe2\x38\xcc\x0b\x9d\x82\x83\x47\x38\xed\xe4\x21\x4d\x13\x3c\x94\x01\xfc\x00"; |
williequesada | 0:fb407fe1db0f | 48 | uint8_t Decryption[200]; |
williequesada | 0:fb407fe1db0f | 49 | |
williequesada | 0:fb407fe1db0f | 50 | int main() |
williequesada | 0:fb407fe1db0f | 51 | { |
williequesada | 0:fb407fe1db0f | 52 | mbedtls_entropy_context entropy; |
williequesada | 0:fb407fe1db0f | 53 | mbedtls_entropy_init( &entropy ); |
williequesada | 0:fb407fe1db0f | 54 | |
williequesada | 0:fb407fe1db0f | 55 | mbedtls_ctr_drbg_context ctr_drbg; |
williequesada | 0:fb407fe1db0f | 56 | mbedtls_ctr_drbg_init(&ctr_drbg); |
williequesada | 0:fb407fe1db0f | 57 | |
williequesada | 0:fb407fe1db0f | 58 | mbedtls_pk_context PrivateKey; // Crea la llave |
williequesada | 0:fb407fe1db0f | 59 | mbedtls_pk_init(&PrivateKey); // Inicializa la llave |
williequesada | 0:fb407fe1db0f | 60 | |
williequesada | 0:fb407fe1db0f | 61 | mbedtls_pk_context PublicKey; // Crea la llave |
williequesada | 0:fb407fe1db0f | 62 | mbedtls_pk_init(&PublicKey); // Inicializa la llave |
williequesada | 0:fb407fe1db0f | 63 | |
williequesada | 0:fb407fe1db0f | 64 | int ret; |
williequesada | 0:fb407fe1db0f | 65 | size_t olen = 0; |
williequesada | 0:fb407fe1db0f | 66 | uint8_t error[100]; |
williequesada | 0:fb407fe1db0f | 67 | size_t errorlen=100; |
williequesada | 0:fb407fe1db0f | 68 | int SizeOutputKey; |
williequesada | 0:fb407fe1db0f | 69 | const char *pers = "mbedtls_pk_encrypt"; |
williequesada | 0:fb407fe1db0f | 70 | |
williequesada | 0:fb407fe1db0f | 71 | // ------------------------Random number generator--------------------------- |
williequesada | 0:fb407fe1db0f | 72 | if( ( ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char*)pers, strlen(pers))) != 0) |
williequesada | 0:fb407fe1db0f | 73 | { |
williequesada | 0:fb407fe1db0f | 74 | printf( "Entropy generator fail\n"); |
williequesada | 0:fb407fe1db0f | 75 | printf(" Ret: 0x%04x\n",ret); |
williequesada | 0:fb407fe1db0f | 76 | mbedtls_strerror(ret,(char*)error,errorlen); |
williequesada | 0:fb407fe1db0f | 77 | printf( "Error: %s\n",error); |
williequesada | 0:fb407fe1db0f | 78 | } else { |
williequesada | 0:fb407fe1db0f | 79 | printf( "Entropy generator ok\n"); |
williequesada | 0:fb407fe1db0f | 80 | } |
williequesada | 0:fb407fe1db0f | 81 | |
williequesada | 0:fb407fe1db0f | 82 | // ------------------------Create the public keys--------------------------- |
williequesada | 0:fb407fe1db0f | 83 | if ((ret=mbedtls_pk_parse_public_key(&PublicKey, PublicKeyTXT, sizeof(PublicKeyTXT))) != 0) |
williequesada | 0:fb407fe1db0f | 84 | { |
williequesada | 0:fb407fe1db0f | 85 | printf( "Public key fail\n"); |
williequesada | 0:fb407fe1db0f | 86 | printf(" Ret: 0x%04x\n",ret); |
williequesada | 0:fb407fe1db0f | 87 | mbedtls_strerror(ret,(char*)error,errorlen); |
williequesada | 0:fb407fe1db0f | 88 | printf( "Error: %s\n",error); |
williequesada | 0:fb407fe1db0f | 89 | } else { |
williequesada | 0:fb407fe1db0f | 90 | printf( "Public key ok "); |
williequesada | 0:fb407fe1db0f | 91 | SizeOutputKey=mbedtls_pk_get_len(&PublicKey); |
williequesada | 0:fb407fe1db0f | 92 | printf("--> %i\n",SizeOutputKey); |
williequesada | 0:fb407fe1db0f | 93 | } |
williequesada | 0:fb407fe1db0f | 94 | printf( "Llave publica: \n%s\n\n",PublicKeyTXT); |
williequesada | 0:fb407fe1db0f | 95 | |
williequesada | 0:fb407fe1db0f | 96 | // -----------------------Create the private keys--------------------------- |
williequesada | 0:fb407fe1db0f | 97 | if ((ret=mbedtls_pk_parse_key(&PrivateKey, PrivateKeyTXT, sizeof(PrivateKeyTXT), NULL, 0)) != 0) |
williequesada | 0:fb407fe1db0f | 98 | { |
williequesada | 0:fb407fe1db0f | 99 | printf( "Private key fail\n"); |
williequesada | 0:fb407fe1db0f | 100 | printf(" Ret: 0x%04x\n",ret); |
williequesada | 0:fb407fe1db0f | 101 | mbedtls_strerror(ret,(char*)error,errorlen); |
williequesada | 0:fb407fe1db0f | 102 | printf( "Error: %s\n",error); |
williequesada | 0:fb407fe1db0f | 103 | } else { |
williequesada | 0:fb407fe1db0f | 104 | printf( "Private key ok "); |
williequesada | 0:fb407fe1db0f | 105 | SizeOutputKey=mbedtls_pk_get_len(&PrivateKey); |
williequesada | 0:fb407fe1db0f | 106 | printf("--> %i\n",SizeOutputKey); |
williequesada | 0:fb407fe1db0f | 107 | } |
williequesada | 0:fb407fe1db0f | 108 | printf( "Llave privada: \n%s\n\n",PrivateKeyTXT); |
williequesada | 0:fb407fe1db0f | 109 | |
williequesada | 0:fb407fe1db0f | 110 | // ------------------------Check the pair of keys before-------------------- |
williequesada | 0:fb407fe1db0f | 111 | if ((ret=mbedtls_pk_check_pair(&PublicKey,&PrivateKey))!= 0) |
williequesada | 0:fb407fe1db0f | 112 | { |
williequesada | 0:fb407fe1db0f | 113 | printf( "\n\nKeys check fail\n"); |
williequesada | 0:fb407fe1db0f | 114 | mbedtls_strerror(ret,(char*)error,errorlen); |
williequesada | 0:fb407fe1db0f | 115 | printf( "Error: %s\n",error); |
williequesada | 0:fb407fe1db0f | 116 | } else { |
williequesada | 0:fb407fe1db0f | 117 | printf( "Keys check ok\n\n"); |
williequesada | 0:fb407fe1db0f | 118 | } |
williequesada | 0:fb407fe1db0f | 119 | //--------------------------------Encryption-------------------------------- |
williequesada | 0:fb407fe1db0f | 120 | if ((ret=mbedtls_pk_encrypt(&PublicKey, Message, sizeof(Message)-1,Encryption, &olen, 200 ,mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) |
williequesada | 0:fb407fe1db0f | 121 | { |
williequesada | 0:fb407fe1db0f | 122 | printf("Encrypt failed\n"); |
williequesada | 0:fb407fe1db0f | 123 | printf("Ret: 0x%04x, Olen: %i,\n",ret,olen); |
williequesada | 0:fb407fe1db0f | 124 | mbedtls_strerror(ret,(char*)error,errorlen); |
williequesada | 0:fb407fe1db0f | 125 | printf( "Error: %s\n",error); |
williequesada | 0:fb407fe1db0f | 126 | } |
williequesada | 0:fb407fe1db0f | 127 | else |
williequesada | 0:fb407fe1db0f | 128 | { printf(" Encrypt ok\n"); |
williequesada | 0:fb407fe1db0f | 129 | printf(" Ret: 0x%04x, Olen: %i,\n",ret,olen); |
williequesada | 0:fb407fe1db0f | 130 | printf(" Encryt message: %s\n",Encryption); // should be "0xFF...." |
williequesada | 0:fb407fe1db0f | 131 | printf(" \n"); |
williequesada | 0:fb407fe1db0f | 132 | } |
williequesada | 0:fb407fe1db0f | 133 | |
williequesada | 0:fb407fe1db0f | 134 | //--------------------------------Decryption-------------------------------- |
williequesada | 0:fb407fe1db0f | 135 | if((ret=mbedtls_pk_decrypt(&PrivateKey, Encryption, 128, Decryption, &olen, 1024,mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
williequesada | 0:fb407fe1db0f | 136 | { |
williequesada | 0:fb407fe1db0f | 137 | printf( "Decrypt failed\n"); |
williequesada | 0:fb407fe1db0f | 138 | printf(" Ret: 0x%04x, Olen: %i,\n",ret,olen); |
williequesada | 0:fb407fe1db0f | 139 | mbedtls_strerror(ret,(char*)error,errorlen); |
williequesada | 0:fb407fe1db0f | 140 | printf( "Error: %s\n",error); |
williequesada | 0:fb407fe1db0f | 141 | } |
williequesada | 0:fb407fe1db0f | 142 | else |
williequesada | 0:fb407fe1db0f | 143 | { |
williequesada | 0:fb407fe1db0f | 144 | printf(" Decryption Ok\n"); |
williequesada | 0:fb407fe1db0f | 145 | printf(" Decryption message: %s\n",Decryption); // should be "Hola mundo" |
williequesada | 0:fb407fe1db0f | 146 | } |
williequesada | 0:fb407fe1db0f | 147 | |
williequesada | 0:fb407fe1db0f | 148 | |
williequesada | 0:fb407fe1db0f | 149 | printf( "\nMensaje original: %s\n\n",Message); |
williequesada | 0:fb407fe1db0f | 150 | printf( "Mensaje encriptado: %s\n\n",Encryption); |
williequesada | 0:fb407fe1db0f | 151 | printf( "Mensaje encriptado HEX: "); |
williequesada | 0:fb407fe1db0f | 152 | for(int i=0; i<=130; i++){ |
williequesada | 0:fb407fe1db0f | 153 | printf(" %02x",Encryption[i]); |
williequesada | 0:fb407fe1db0f | 154 | } |
williequesada | 0:fb407fe1db0f | 155 | printf(" \n\n"); |
williequesada | 0:fb407fe1db0f | 156 | printf( "Mensaje desencriptado: %s\n\n",Decryption); |
williequesada | 0:fb407fe1db0f | 157 | |
williequesada | 0:fb407fe1db0f | 158 | size_t *Olen2; |
williequesada | 0:fb407fe1db0f | 159 | mbedtls_base64_encode(Encryption1,200,Olen2,Encryption,128); |
williequesada | 0:fb407fe1db0f | 160 | printf( "Mensaje encriptado: %s\n\n",Encryption1); |
williequesada | 0:fb407fe1db0f | 161 | printf( "Mensaje encriptado: %i\n\n",Olen2); |
williequesada | 0:fb407fe1db0f | 162 | |
williequesada | 0:fb407fe1db0f | 163 | mbedtls_pk_free(&PrivateKey); // Inicializa la llave |
williequesada | 0:fb407fe1db0f | 164 | mbedtls_pk_free(&PublicKey); // Inicializa la llave |
williequesada | 0:fb407fe1db0f | 165 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
williequesada | 0:fb407fe1db0f | 166 | mbedtls_entropy_free( &entropy ); |
williequesada | 0:fb407fe1db0f | 167 | |
williequesada | 0:fb407fe1db0f | 168 | while(1) { |
williequesada | 0:fb407fe1db0f | 169 | |
williequesada | 0:fb407fe1db0f | 170 | } |
williequesada | 0:fb407fe1db0f | 171 | } |
williequesada | 0:fb407fe1db0f | 172 | |
williequesada | 0:fb407fe1db0f | 173 | |
williequesada | 0:fb407fe1db0f | 174 | |
williequesada | 0:fb407fe1db0f | 175 | |
williequesada | 0:fb407fe1db0f | 176 |