Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #include "Arduino.h"
  2.  
  3.  
  4. const uint32_t serverPublicKey = 7;
  5. const uint32_t serverPrivateKey = 27103;
  6. const uint32_t serverModulus = 95477;
  7. const uint32_t clientPublicKey = 11;
  8. const uint32_t clientPrivateKey = 38291;
  9. const uint32_t clientModulus = 84823;
  10.  
  11. void setup() {
  12. init();
  13. Serial.begin(9600);
  14. Serial3.begin(9600);
  15.  
  16. pinMode(13, INPUT);
  17. }
  18.  
  19. /*
  20. Computes a*b mod m for m at most 31 bits, a and b nonnegative.
  21. */
  22. uint32_t mulmod(uint32_t a, uint32_t b, uint32_t m) {
  23. a = a % m;
  24. b = b % m;
  25. uint32_t currentAdjusted = a;
  26. uint32_t product = 0;
  27.  
  28. for (int bit=0; bit < 32; bit++) {
  29. if (1 & (b >> bit)) {
  30. product = (product + currentAdjusted) % m;
  31. }
  32. currentAdjusted = (2 * currentAdjusted) % m;
  33. }
  34. // Serial.println(product); // test
  35. return product;
  36. }
  37.  
  38.  
  39. /*
  40. Computes a^b mod m for m at most 31 bits, a and b nonnegative.
  41. */
  42. uint32_t powmod(uint32_t x, uint32_t pow, uint32_t m) {
  43. uint32_t ans = 1;
  44. uint32_t pow_x = x;
  45.  
  46. while (pow > 0) {
  47. if ((pow & 1) == 1) {
  48. ans = mulmod(ans, pow_x, m);
  49. }
  50. pow_x = mulmod(pow_x, pow_x, m);
  51. pow >>= 1; // divides by 2
  52. }
  53.  
  54. return ans;
  55. }
  56.  
  57. uint32_t encrypt(char inputChar, uint32_t e, uint32_t m) {
  58. return powmod(inputChar, e, m);
  59. }
  60.  
  61. char decrypt(uint32_t receivedInt, uint32_t d, uint32_t n) {
  62. return (char)powmod(receivedInt, d, n);
  63. }
  64.  
  65. /** Writes an uint32_t to Serial3.
  66. */
  67. void uint32_to_serial3(uint32_t num) {
  68. Serial3.write((char) (num >> 24));
  69. Serial3.write((char) (num >> 16));
  70. Serial3.write((char) (num >> 8));
  71. Serial3.write((char) (num >> 0));
  72. }
  73.  
  74. /** Reads an uint32_t from Serial3.
  75. */
  76. uint32_t uint32_from_serial3() {
  77. uint32_t num = 0;
  78. for (int i = 0; i < 4; i++) {
  79. while (!(Serial3.available() > 0));
  80. num = (num << 8) | Serial3.read();
  81. }
  82. Serial3.flush();
  83. return num;
  84. }
  85.  
  86.  
  87. void loop(uint32_t d, uint32_t n, uint32_t e, uint32_t m) {
  88. // read character from serial monitor
  89. if (Serial.available() > 0) {
  90. // encrypt character
  91. char inputChar = Serial.read();
  92. Serial.write(inputChar);
  93.  
  94. if (inputChar == '\r') {
  95. uint32_t intValue = encrypt(inputChar, e, m);
  96. uint32_to_serial3(intValue);
  97.  
  98. inputChar = '\n';
  99. Serial.write(inputChar);
  100. }
  101.  
  102. uint32_t intValue = encrypt(inputChar, e, m);
  103. uint32_to_serial3(intValue);
  104. }
  105.  
  106. // receive encrypted integer
  107. if (Serial3.available() > 0) {
  108. uint32_t receivedValue = uint32_from_serial3();
  109. char receivedChar = decrypt(receivedValue, d, n);
  110. Serial.write(receivedChar);
  111. }
  112. }
  113.  
  114. int main() {
  115. setup();
  116.  
  117.  
  118.  
  119.  
  120. Serial.write((uint32_t)4294967137);
  121.  
  122. uint32_t testingThing = powmod(97,11,84823);
  123. Serial.print("test: ");
  124. Serial.println(testingThing);
  125.  
  126. bool isServer = digitalRead(13) == HIGH;
  127. uint32_t d, n, e, m;
  128. if (isServer) {
  129. d = serverPrivateKey;
  130. n = serverModulus;
  131. e = clientPublicKey;
  132. m = clientModulus;
  133. } else {
  134. d = clientPrivateKey;
  135. n = clientModulus;
  136. e = serverPublicKey;
  137. m = serverModulus;
  138. }
  139.  
  140. while (true) {
  141. loop(d, n, e, m);
  142. }
  143.  
  144. Serial.flush();
  145. return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement