至Github下載ntlib Library並添加至Arduino中,步驟如下
2. 解壓縮ntlib-main.zip file
4. 在Arduino中找到Add ZIP Library選項
extern "C" {
#include "ntlib.h"
}
struct RSA rsa ;
void setup () {
Serial .begin (115200 );
delay (1000 );
rsa_init (& rsa , 29 , 31 , 113 ); //輸入參數 p = 29; q = 31; e = 113,並生成 d
delay (5000 );
// Demo
for (int i = 0 ; i < 256 ; ++ i ){
uint64_t cipher = rsa .Encrypt (& rsa , i );
uint64_t plantext = rsa .Decrypt (& rsa , cipher );
printf ("RSA: %10s: %6d , " , "message" , i );
printf (" %10s: %6d ," , "ciphertext" , cipher );
printf ("%10s: %6d \r\n" , "plantext" , plantext );
}
}
void loop () {
// put your main code here, to run repeatedly:
}
此函式用於初始化並配置heap給予RSA演算法相關參數使用,如(e,d,n),以及定義加密和解密相關函式指標。
rsa_init (& rsa , 29 , 31 , 113 );
@params
struct * rsa ,引入結構體rsa之位址 ,為指標變數
uint64_t p ,RSA 參數 p
uint64_t q ,RSA 參數 q
uint64_t e ,RSA 公鑰 e
此函式對數值進行加密,message為欲加密訊息之數值
rsa .Encrypt (& rsa , message )
@params
uint64_t message , 愈加密明文
@return
uint64_t cipher , 密文
此函式對數值進行解密',ciphet為欲解密訊息之密文數值
rsa .Decrypt (& rsa , cipher )
@params
uint64_t message , 愈解密密文
@return
uint64_t cipher , 明文