0% found this document useful (0 votes)
57 views7 pages

Sketch Jan16a Arduino Keyboard Client

This document contains code for an Arduino project that implements RSA encryption and decryption. It includes functions for: - Generating prime numbers and RSA public/private keys - Encrypting and decrypting messages using the RSA keys - Displaying messages on an LCD screen - Receiving keyboard input and sending encrypted data to an ESP8266 module over serial

Uploaded by

Subu Menon
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
57 views7 pages

Sketch Jan16a Arduino Keyboard Client

This document contains code for an Arduino project that implements RSA encryption and decryption. It includes functions for: - Generating prime numbers and RSA public/private keys - Encrypting and decrypting messages using the RSA keys - Displaying messages on an LCD screen - Receiving keyboard input and sending encrypted data to an ESP8266 module over serial

Uploaded by

Subu Menon
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 7

#include <PS2Keyboard.

h>
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

SoftwareSerial mySerial(4, 5); // RX, TX

const int DataPin = 3;


const int IRQpin = 2;

PS2Keyboard keyboard;

const int rs = 19, en = 18, d4 = 17, d5 = 16, d6 = 15, d7 = 14;


LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int p=7, q, n, e, d, phi_n;


String key_data;
int key_data_int;

#define SMS_SIZE 16
#define PLAINTEXT_SIZE (SMS_SIZE / sizeof(int))
#define CIPHERTEXT_SIZE (SMS_SIZE)
class RSA {
private:

public:
RSA();
~RSA();
void encrypt(char *plainText, char *chipherText, int *publicKey);
void decrypt(char *plainText, char *cipherText, int *privateKey);
bool compare(char *arr1, char *arr2, int len);
};
RSA rsa;

char msg[PLAINTEXT_SIZE] = "";


int tx_publicKey[2] = {0, 0};
int tx_privateKey[2] = {0, 0};
char cipher_msg[CIPHERTEXT_SIZE];

String esp_voice,esp_data;

void setup() {
keyboard.begin(DataPin, IRQpin, PS2Keymap_US);
Serial.begin(115200);
mySerial.begin(115200);
lcd.begin(16, 2);
lcd.clear();
get_prime_no();
create_keys();
}
void create_keys(){
q = key_data_int;
n = p * q;
phi_n = ( p - 1) * (q - 1);
for (int i = 1; i <= phi_n; i++) {
if (checkprime(i)) {
int gcd_value = gcd(phi_n,i);
if(gcd_value == 1){
e = i;
break;
}
}
}
Serial.print("Public Key ( e, n ) =
(");Serial.print(e);Serial.print(",");Serial.print(n);Serial.println(")");
d = modInverse(e,phi_n);
Serial.print("Private Key ( d, n ) =
(");Serial.print(d);Serial.print(",");Serial.print(n);Serial.println(")");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Note Private Key");
lcd.setCursor(0,1);
lcd.print("d = ");
lcd.print(d);
lcd.print(",");
lcd.print("n = ");
lcd.print(n);
delay(5000);
}
int modInverse(int e, int m){
e = e % m;
for (int x=1;x<m;x++){
if ((e * x) % m == 1)
return x;
}
return 1;
}
bool checkprime(int n)
{
int flag = 0;
for (int i = 2; i <= n / 2; ++i) {
// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
return false;
}
else {
if (flag == 0) {
return true;
}
else {
return false;
}
}
}

int gcd(int a, int b)


{
while (a != b)
{
if (a > b)
{
return gcd(a - b, b);
}
else
{
return gcd(a, b - a);
}
}
return a;
}
void get_prime_no(){
int input = 1;
lcd.setCursor(0, 0);
lcd.print("Enter Prime No");
lcd.setCursor(0, 1);
while(input){
while(keyboard.available()) {
char c = keyboard.read();
// Serial.print(c);
lcd.print(c);
if (c == '\r') {
Serial.println();
Serial.print("Key data:");
Serial.println(key_data);
key_data_int = (key_data).toInt();
input = 0;
break;
}
key_data = key_data + c;
}
}
}
void get_message(){
String message_text;
int input = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Message ");
lcd.setCursor(0, 1);
while(input){
while(keyboard.available()) {
char c = keyboard.read();
// Serial.print(c);
lcd.print(c);
if (c == '\r') {
Serial.println();
Serial.print("Message:");
Serial.println(message_text);
encrypt_message(message_text);
input = 0;
break;
}
message_text = message_text + c;
}
rxSerialEvent();
}
}
void rxSerialEvent() {
while (mySerial.available()){
delay(1);
char c = mySerial.read();
if (c == '#') {
Serial.print("Data from ESP:");
Serial.println(esp_voice);
esp_data = esp_voice;
esp_voice = "";
break;
}
esp_voice += c;
}
}
void encrypt_message(String mesg){
tx_publicKey[0] = n;
tx_publicKey[1] = e;
tx_privateKey[0] = n;
tx_privateKey[1] = d;

for(int i=0;i<PLAINTEXT_SIZE;i++){
msg[i] = NULL;
}
for(int i=0;i<mesg.length();i++){
msg[i] = mesg[i];
}

Serial.print("Encrypted Message:");
rsa.encrypt(msg, cipher_msg, tx_publicKey);
for(int i = 0; i < CIPHERTEXT_SIZE; i++){
Serial.write(cipher_msg[i]);
mySerial.print(cipher_msg[i]);
}
mySerial.print('#');
delay(100);
Serial.println();

}
void decrypt_message(){
char cipher[CIPHERTEXT_SIZE];
char plain[PLAINTEXT_SIZE];

memset(cipher, 0, CIPHERTEXT_SIZE);
memset(plain, 0, PLAINTEXT_SIZE);

for(int i=0;i<CIPHERTEXT_SIZE;i++)
cipher[i]= cipher_msg[i];

Serial.print("Decrypted Message:");
rsa.decrypt(plain, cipher, tx_privateKey);
Serial.println(plain);
}
void loop() {
get_message();
}
RSA::RSA()
{

RSA::~RSA()
{

}
void RSA::encrypt(char *plainText, char *cipherText, int *publicKey)
{
long m = 1;
int n = publicKey[0];
int e = publicKey[1];
int ctr = 0;

for (int i = 0; i < PLAINTEXT_SIZE; i++) {


for (int j = 0; j < e; j++) {
m = (m * plainText[i]) % n;
}

ctr = i * sizeof(int);

cipherText[ctr] = (char) (m & 0x00ff);


cipherText[ctr + 1] = (char) ((m & 0xff00) >> 8);

m = 1;
}
}

void RSA::decrypt(char *plainText, char *cipherText, int *privateKey)


{
long M = 1;
int n = privateKey[0];
int d = privateKey[1];
int temp = 0;
int ctr = 0;

//re-assemble char array to array of int


for (int i = 0; i < PLAINTEXT_SIZE; i++) {
ctr = i * sizeof(int);
temp = (((unsigned char)cipherText[ctr + 1] << 8) | (unsigned
char)cipherText[ctr]);

for (int j = 0; j < d; j++) {


M = (M * temp) % n;
}

plainText[i] = (unsigned char)(M & 0xFF);


M = 1;
}

bool RSA::compare(char *arr1, char *arr2, int len)


{
int res = 0;

res = memcmp(arr1, arr2, len);


if (res == 0)
return true;
else
return false;
}

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(D5, D6); // RX, TX

byte ledPin = 2;
char ssid[] = "embedded"; // SSID of your home WiFi
char pass[] = "embedded123"; // password of your home WiFi

unsigned long askTimer = 0;

IPAddress server(192,168,43,80); // the fix IP address of the server


WiFiClient client;

void setup() {
Serial.begin(115200); // only for debug
mySerial.begin(115200);
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // Network parameters
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());
pinMode(ledPin, OUTPUT);
}
String voice;
String data ="hello";

void loop () {
serialEvent();

client.connect(server, 80); // Connection to the server


digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
if(data != "0"){
client.print(data);
client.println("\r"); // sends the message to the server
data ="0";
delay(100);
}
String answer = client.readStringUntil('\r'); // receives the answer from the
sever
if(answer != ""){
Serial.print("I am ESP Client- Data From ESP Server: ");
Serial.println(answer);
mySerial.print(answer);
mySerial.println("#");
client.flush();
}
digitalWrite(ledPin, HIGH);
}
void serialEvent() {
while (mySerial.available()){ //Check if there is an available byte to read
delay(1); //Delay added to make thing stable
char c = mySerial.read(); //Conduct a serial read
if (c == '#') {
Serial.print("I am ESP Client - Data from Arduino Client:");
Serial.println(voice);
data = voice;
voice = "";
break;
} //Exit the loop when the # is detected after the word
voice += c; //Shorthand for voice = voice + c
}
}

You might also like