I need encrypt data with RSA using public key and decrypt using private key. Public and private keys generation not needed. I know that algorithm require computing resources, but I use only 32-128-bit keys or smaller. I can't find RSA lib for Arduino in internet.
I found cryptography AVR-Crypto-Lib library for AVR microcontrollers. How to use this library with Arduino? I think Arduino compatible, because it use ATmega328P, ATmega2560 microcontrollers etc. How include this library in Arduino IDE. It would be good that someone will write simple hello world example. Or can suggest other library that are implemented RSA algorithm.
2
votes
That library is four years old and does not even have a ReadMe and very low-level code. What are you trying to accomplish?
- zaph
You might do better by googling for "arduino SSL library". Such libraries will likely contain RSA and other crypto functions even if you aren't interested in SSL.
- President James K. Polk
@zaph Then again, this page of "das labor" (Bochum's Hacker space) seems more up to date and does contain a Wiki. The source on github seems to be a mindless copy. Maybe that's not what was intended, but yeah...
- Maarten Bodewes
You're better off trying other asymmetric primitives. A quick look shows up ArduinoLibs which contains e.g Elliptic Curve crypto (much better suited to embedded devices) but not RSA.
- Maarten Bodewes
1 Answers
0
votes
I have developed the following code using random() of Arduino. But the code function once in 10 iterations that too at 8bit key.
#include "pRNG.h"
#include <Arduino.h>
unsigned int Firstprime, Secondprime, Privatekey, Publickey;
unsigned int Field, phin, Enc, Dec;
bool Hasrun= false;
unsigned int Text = 12;
pRNG prn;
unsigned int modMult(unsigned int a, unsigned int b, unsigned int mod) //modulo multiplication function
{
unsigned int res = 0; // Initialize result
a = a % mod;
while (b > 0)
{
// If b is odd, add 'a' to result
if (b % 2 == 1)
res = (res + a) % mod;
// Multiply 'a' with 2
a = (a * 2) % mod;
// Divide b by 2
b /= 2;
}
// Return result
return res % mod;
}
bool prime(unsigned int number) //primality check for prime numbers
{
for (unsigned int i = 2; i <=sqrt(number); ++i)
{
if (number % i == 0)
{
return false;
}
}
return true;
}
unsigned int PRN() //generation of a prime random number
{
unsigned int n1;
do
{
n1= prn.getRndByte();
//n1= random(100);
}while(n1==0||prime(n1)==false);
return n1;
}
unsigned int gcd(unsigned int a, unsigned int b) //function to check GCD
{
unsigned int temp;
while (1)
{
temp = a%b;
if (temp == 0)
return b;
a = b;
b= temp;
}
}
unsigned int E_gen(unsigned int n, unsigned int phi) //publickey generation e
{
for(unsigned int i=2; i<n; i++)
{
if(gcd(i,n)==1 && gcd(i,phi)==1)
{
return i;
//break;
}
}
Serial.println("Public key generated");
}
unsigned int D_gen(unsigned int en, unsigned int phi) //privatekey generation d
{
for(unsigned int i=2; i<phi; i++)
{
if(modMult(en,i,phi)==1)
{
return i;
//break;
}
}
Serial.println("Private key generated");
}
unsigned int power(unsigned int base, unsigned int expo, unsigned int mod)
{
unsigned int test;
for(test = 1; expo; expo >>= 1)
{
if (expo & 1)
test = (test * base) % mod;
base = (base * base) % mod;
}
return test;
}
/*unsigned int keygen()
{
}*/
void setup()
{
Serial.begin(9600);
// randomSeed(analogRead(A0));
Firstprime=PRN();
Serial.println(Firstprime);
do
{
Secondprime=PRN();
Serial.println(Secondprime);
}while(Firstprime==Secondprime);
Field=Firstprime*Secondprime;
phin=(Firstprime-1)*(Secondprime-1);
Publickey=E_gen(Field, phin);
Privatekey=D_gen(Publickey,phin);
}
void loop()
{
if(Hasrun==false)
{
//Serial.println(prn.getRndunsigned int());
Serial.print("Public key is:");
Serial.println(Publickey);
Serial.print("Private key is:");
Serial.println(Privatekey);
Serial.println("Encrypting....");
Enc= power(Text,Publickey, Field);
Serial.println(Enc);
Serial.println("Decrypting...");
Dec=power(Enc,Privatekey, Field);
Serial.println(Dec);
/*Serial.println(p);
Serial.println(q);*/
Hasrun=true;
}
}