Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arduino.h"
- const uint32_t serverPublicKey = 7;
- const uint32_t serverPrivateKey = 27103;
- const uint32_t serverModulus = 95477;
- const uint32_t clientPublicKey = 11;
- const uint32_t clientPrivateKey = 38291;
- const uint32_t clientModulus = 84823;
- void setup() {
- init();
- Serial.begin(9600);
- Serial3.begin(9600);
- pinMode(13, INPUT);
- }
- /*
- Computes a*b mod m for m at most 31 bits, a and b nonnegative.
- */
- uint32_t mulmod(uint32_t a, uint32_t b, uint32_t m) {
- a = a % m;
- b = b % m;
- uint32_t currentAdjusted = a;
- uint32_t product = 0;
- for (int bit=0; bit < 32; bit++) {
- if (1 & (b >> bit)) {
- product = (product + currentAdjusted) % m;
- }
- currentAdjusted = (2 * currentAdjusted) % m;
- }
- // Serial.println(product); // test
- return product;
- }
- /*
- Computes a^b mod m for m at most 31 bits, a and b nonnegative.
- */
- uint32_t powmod(uint32_t x, uint32_t pow, uint32_t m) {
- uint32_t ans = 1;
- uint32_t pow_x = x;
- while (pow > 0) {
- if ((pow & 1) == 1) {
- ans = mulmod(ans, pow_x, m);
- }
- pow_x = mulmod(pow_x, pow_x, m);
- pow >>= 1; // divides by 2
- }
- return ans;
- }
- uint32_t encrypt(char inputChar, uint32_t e, uint32_t m) {
- return powmod(inputChar, e, m);
- }
- char decrypt(uint32_t receivedInt, uint32_t d, uint32_t n) {
- return (char)powmod(receivedInt, d, n);
- }
- /** Writes an uint32_t to Serial3.
- */
- void uint32_to_serial3(uint32_t num) {
- Serial3.write((char) (num >> 24));
- Serial3.write((char) (num >> 16));
- Serial3.write((char) (num >> 8));
- Serial3.write((char) (num >> 0));
- }
- /** Reads an uint32_t from Serial3.
- */
- uint32_t uint32_from_serial3() {
- uint32_t num = 0;
- for (int i = 0; i < 4; i++) {
- while (!(Serial3.available() > 0));
- num = (num << 8) | Serial3.read();
- }
- Serial3.flush();
- return num;
- }
- void loop(uint32_t d, uint32_t n, uint32_t e, uint32_t m) {
- // read character from serial monitor
- if (Serial.available() > 0) {
- // encrypt character
- char inputChar = Serial.read();
- Serial.write(inputChar);
- if (inputChar == '\r') {
- uint32_t intValue = encrypt(inputChar, e, m);
- uint32_to_serial3(intValue);
- inputChar = '\n';
- Serial.write(inputChar);
- }
- uint32_t intValue = encrypt(inputChar, e, m);
- uint32_to_serial3(intValue);
- }
- // receive encrypted integer
- if (Serial3.available() > 0) {
- uint32_t receivedValue = uint32_from_serial3();
- char receivedChar = decrypt(receivedValue, d, n);
- Serial.write(receivedChar);
- }
- }
- int main() {
- setup();
- Serial.write((uint32_t)4294967137);
- uint32_t testingThing = powmod(97,11,84823);
- Serial.print("test: ");
- Serial.println(testingThing);
- bool isServer = digitalRead(13) == HIGH;
- uint32_t d, n, e, m;
- if (isServer) {
- d = serverPrivateKey;
- n = serverModulus;
- e = clientPublicKey;
- m = clientModulus;
- } else {
- d = clientPrivateKey;
- n = clientModulus;
- e = serverPublicKey;
- m = serverModulus;
- }
- while (true) {
- loop(d, n, e, m);
- }
- Serial.flush();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement