301N November 2018
/
encrypt
15:21 9nov
Diff: main.cpp
- Revision:
- 0:5d1b6c97e2fe
- Child:
- 1:c8f7fe71d151
diff -r 000000000000 -r 5d1b6c97e2fe main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Nov 09 09:50:56 2018 +0000 @@ -0,0 +1,48 @@ +#include "mbed.h" +#include "mbedtls/pk.h" +#include "mbedtls/ctr_drbg.h" + + +int main(){ + int ret = 0; + mbedtls_pk_context pk; + + mbedtls_pk_init( &pk ); + const unsigned char key[]="-----BEGIN RSA PUBLIC KEY-----" +"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDMYfnvWtC8Id5bPKae5yXSxQTt" +"+Zpul6AnnZWfI2TtIarvjHBFUtXRo96y7hoL4VWOPKGCsRqMFDkrbeUjRrx8iL91" +"4/srnyf6sh9c8Zk04xEOpK1ypvBz+Ks4uZObtjnnitf0NBGdjMKxveTq+VE7BWUI" +"yQjtQ8mbDOsiLLvh7wIDAQAB" +"-----END RSA PUBLIC KEY-----"; + + + /* + * Read the RSA public key + */ + if( ( ret = mbedtls_pk_parse_public_key( &pk, key,sizeof(key)) ) != 0 ) + { + printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x\n", -ret ); + goto exit; + } + + unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; + size_t olen = 0; + + /* + * Calculate the RSA encryption of the data. + */ + printf( "\n . Generating the encrypted value" ); + fflush( stdout ); + const unsigned char to_encrypt[] ="HelloWorld"; + mbedtls_ctr_drbg_context ctr_drbg; + + if( ( ret = mbedtls_pk_encrypt( &pk, to_encrypt, sizeof(to_encrypt), + buf, &olen, sizeof(buf), + mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) + { + printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n", -ret ); + goto exit; + } + exit: + return 0; +}