Asymmetric key encryption & BigInt functionality (code included)


SUBMITTED BY: Guest

DATE: May 30, 2013, 7:48 a.m.

FORMAT: Text only

SIZE: 7.3 kB

HITS: 2231

  1. #include <Math.au3>
  2. #include <String.au3>
  3. ;374a760e6d241 - Private Key - 972687164232257[/i]
  4. $privateKey = "972687164232257"
  5. ;69eda918eb380d - Modulus - 29816183077943309[/i]
  6. $modulus = "29816183077943309"
  7. ;10001 - Public Key - 65537[/i]
  8. $publicKey = "65537"
  9. ; Make some test text and turn it into a binary string[/i]
  10. $text = "Test"
  11. $bin = ""
  12. For $i = 1 to StringLen($text)
  13. $temp = decbin(Asc(StringMid($text, $i, 1)))
  14. $temp = _StringRepeat("0", 8 - StringLen($temp)) & $temp
  15. $bin = $bin & $temp
  16. Next
  17. ConsoleWrite( "Text: " & $bin & @CRLF)
  18. ; Encrypt our test binary string with our private key[/i]
  19. $begin = TimerInit()
  20. $cipherInt = binaryPowMod($bin, decbin($privateKey), decbin($modulus))
  21. ConsoleWrite("Encryption took: " & Round(TimerDiff($begin) / 1000, 3) & "s" & @CRLF)
  22. ConsoleWrite( "EncryptedInt: " & $cipherInt & @CRLF)
  23. ; Decrypt our encrypted binary string with the public key[/i]
  24. $begin = TimerInit()
  25. $decryptedInt = binaryPowMod($cipherInt, decbin($publicKey), decbin($modulus))[i];[/i]
  26. ConsoleWrite("Decryption took: " & Round(TimerDiff($begin) / 1000, 3) & "s" & @CRLF)
  27. ; Pad our decrypted binary string to a byte length[/i]
  28. $decryptedInt = _StringRepeat("0", 8 - Mod(StringLen($decryptedInt), 8)) & $decryptedInt
  29. ConsoleWrite( "DecryptedInt: " & $decryptedInt & @CRLF)
  30. ; Convert our decrypted binary string back into ASCII and display it on the Console[/i]
  31. For $i = 1 to StringLen($decryptedInt) Step 8
  32. ConsoleWrite(Chr(bindec(StringMid($decryptedInt, $i, 8))))
  33. Next
  34. ConsoleWrite(@CRLF)
  35. Exit
  36. ;********* FUNCTIONS *********[/i]
  37. ; Convert a decimal number (in a string) into a string of 1s and 0s[/i]
  38. Func decbin($decString)
  39. $stack = ""
  40. While $decString <> "0"
  41. If 1 = Mod(Number(StringRight($decString, 1)), 2) Then
  42. $stack = "1" & $stack
  43. Else
  44. $stack = "0" & $stack
  45. EndIf
  46. $decString = divByTwo($decString)
  47. WEnd
  48. While "0" = StringLeft($stack, 1)
  49. $stack = StringTrimLeft($stack, 1)
  50. WEnd
  51. return $stack
  52. EndFunc
  53. ; Utility function for decbin[/i]
  54. Func divByTwo($string)
  55. $result = ""
  56. $next_add = 0
  57. $size = StringLen($string)
  58. For $i = 1 to $size [i];for ch in s:[/i]
  59. $add = $next_add
  60. $cur = Number(StringMid($string, $i, 1))
  61. If 1 = Mod($cur, 2) Then
  62. $next_add = 5
  63. Else
  64. $next_add = 0
  65. EndIf
  66. $result = $result & Floor(($cur / 2) + $add)
  67. Next
  68. If $result <> "0" Then
  69. While "0" = StringLeft($result, 1)
  70. $result = StringTrimLeft($result, 1)
  71. WEnd
  72. EndIf
  73. return $result
  74. EndFunc
  75. ; Convert a string of 1s and 0s into a decimal number (in a string)[/i]
  76. Func bindec($binString)
  77. Local $size = StringLen($binString)
  78. $result = "0"
  79. For $i = 1 to $size
  80. $result = bigIntAdd($result, $result)
  81. $result = bigIntAdd($result, Number(StringMid($binString, $i, 1)))
  82. Next
  83. Return $result
  84. EndFunc
  85. ; Add two large decimal numbers (in strings)[/i]
  86. Func bigIntAdd($a, $b, $base = 10)
  87. $size = _Max(StringLen($a), StringLen($b))
  88. $a = _StringRepeat("0", $size - StringLen($a)) & $a
  89. $b = _StringRepeat("0", $size - StringLen($b)) & $b
  90. $result = ""[i];[/i]
  91. $carry = 0[i];[/i]
  92. For $i = $size to 1 Step -1
  93. $valA = Number(StringMid($a, $i, 1))
  94. $valB = Number(StringMid($b, $i, 1))
  95. $val = $valA + $valB + $carry
  96. If $val >= $base Then
  97. $carry = 1
  98. $val = Mod($val, $base)
  99. Else
  100. $carry = 0
  101. EndIf
  102. $result = $val & $result
  103. Next
  104. If 1 = $carry Then
  105. $result = "1" & $result
  106. EndIf
  107. While "0" = StringLeft($result, 1)
  108. $result = StringTrimLeft($result, 1)
  109. WEnd
  110. Return $result
  111. EndFunc
  112. ; Multiply two large decimal numbers (in strings)[/i]
  113. Func bigIntMultiply($a, $b)
  114. Return bindec(binaryMultiply(decbin($a), decbin($b)))
  115. EndFunc
  116. ; Return the modulus of two large decimal numbers (in strings)[/i]
  117. Func bigIntMod($a, $b)
  118. Return bindec(binaryMod(decbin($a), decbin($b)))
  119. EndFunc
  120. ; Raise a binary string to a given power, modulo another binary string[/i]
  121. Func binaryPowMod($num, $exp, $mod)
  122. $result = "1"
  123. While $exp <> "0" And StringLen($exp) > 0
  124. If "1" = StringRight($exp, 1) Then
  125. $result = binaryMod(binaryMultiply($result, $num), $mod)
  126. EndIf
  127. $exp = StringTrimRight($exp, 1)
  128. $num = binaryMod(binaryMultiply($num, $num), $mod)
  129. WEnd
  130. Return $result
  131. EndFunc
  132. ; Add two binary strings together, or subtract them[/i]
  133. Func binaryAdd($a, $b, $subtract = False)
  134. $size = _Max(StringLen($a), StringLen($b))
  135. $a = _StringRepeat("0", $size - StringLen($a)) & $a
  136. $b = _StringRepeat("0", $size - StringLen($b)) & $b
  137. $result = ""[i];[/i]
  138. $carry = 0[i];[/i]
  139. If($subtract) Then $carry = 1
  140. If($subtract) Then
  141. $matchString = "0"
  142. Else
  143. $matchString = "1"
  144. EndIf
  145. For $i = $size to 1 Step -1
  146. $val = 0
  147. $bitA = StringMid($a, $i, 1)
  148. $bitB = StringMid($b, $i, 1)
  149. If "1" = $bitA Then $val = $val + 1
  150. If $matchString = $bitB Then $val = $val + 1
  151. $val = $val + $carry
  152. Switch $val
  153. case 3
  154. $carry = 1
  155. $result = "1" & $result
  156. case 1
  157. $carry = 0
  158. $result = "1" & $result
  159. case 2
  160. $carry = 1
  161. $result = "0" & $result
  162. case 0
  163. $result = "0" & $result
  164. EndSwitch
  165. Next
  166. If 1 = $carry Then
  167. If Not $subtract Then
  168. $result = "1" & $result
  169. EndIf
  170. EndIf
  171. While "0" = StringLeft($result, 1)
  172. $result = StringTrimLeft($result, 1)
  173. WEnd
  174. Return $result
  175. EndFunc
  176. ; Multiply two binary strings together[/i]
  177. Func binaryMultiply($a, $b)
  178. $shiftString = $a
  179. $result = "0"
  180. For $i = StringLen($b) to 1 Step -1
  181. If "1" = StringMid($b, $i, 1) Then
  182. $result = binaryAdd($result, $shiftString)
  183. EndIf
  184. $shiftString = $shiftString & "0"
  185. Next
  186. return $result
  187. EndFunc
  188. ; Return the modulus of two binary strings[/i]
  189. Func binaryMod($a, $b)
  190. $ramp = $b & _StringRepeat("0", (StringLen($a) - StringLen($b)) - 1)
  191. While binaryIsGreater($a, $b)
  192. $temp = binaryAdd($a, $ramp, TRUE)
  193. If binaryIsGreater($temp, $a) Then Return $a
  194. $a = $temp
  195. While (StringLen($ramp) >= StringLen($a))
  196. If "1" = StringRight($ramp, 1) Then ExitLoop
  197. $ramp = StringTrimRight($ramp, 1)
  198. WEnd
  199. WEnd
  200. Return $a
  201. EndFunc
  202. ; Tell whether or not a binary string is greater than another binary string[/i]
  203. Func binaryIsGreater($a, $b)
  204. If StringLen($a) > StringLen($b) Then Return True
  205. If StringLen($a) < StringLen($b) Then Return False
  206. $size = StringLen($a)
  207. For $i = 1 to $size
  208. $bitA = Number(StringMid($a, $i, 1))
  209. $bitB = Number(StringMid($b, $i, 1))
  210. If $bitA <> $bitB Then Return ($bitA > $bitB)
  211. Next
  212. Return False
  213. EndFunc

comments powered by Disqus