Jump to content

RSA 128 (1024, 2048..)


 Share

Recommended Posts

My RSA script, 128 bit and lower fast (for UDF attachment)

Using :

"RSATool2v17.exe" open and input (E) key and Keysize (Bits) "128", Generate "P, Q, D".  Copy "P, Q, D" to my code

; Script Start - Add your code below here

#include <String.au3>
#include "BigNum.au3"

Global $P, $Q, $SelectBase = 64

$base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;base64
;Sexagesimal
$base60_2 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx" ;sexagesimal
$base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" ;base32
$base24 = "0123456789ABCDEFGHJKLMNP" ;base24
$base16 = "0123456789ABCDEF" ;hex
;Duodecimal system or dozenal
$base12 = "0123456789AB" ;duodecimal
$base10 = "0123456789" ;base10
$base8 = "01234567" ;oct
$base2 = "01" ;binary


$P = _BigNum_Add($P, "18429553113751821539")
$Q = _BigNum_Add($Q, "14963134653035728619")

$n = _BigNum_Mul($P, $Q)
$PHI = _BigNum_Mul(_BigNum_Sub($P, 1), _BigNum_Sub($Q, 1))

$e = _NumToDec("10001100011000110001", $base16)
$D = "263455903565562556840568120179103558669"

ConsoleWrite(@CRLF)
ConsoleWrite("P: " & NumberBase($P) & @CRLF)
ConsoleWrite("Q: " & NumberBase($Q) & @CRLF)
ConsoleWrite("N: " & NumberBase($n) & @CRLF)

ConsoleWrite("PHI: " & NumberBase($PHI) & @CRLF & @CRLF)
ConsoleWrite("D: " & NumberBase($D) & @CRLF)
ConsoleWrite("E: " & NumberBase($e) & @CRLF & @CRLF)

$Message = _NumToDec(_StringToHex("TEST MESSAGE"), $base16)

If StringLen($Message) > StringLen($n) Then Exit (1)

$c = _BigNum_PowerMod($Message, $e, $n)
$D = _BigNum_PowerMod($c, $D, $n)

ConsoleWrite("C: " & NumberBase($c) & @CRLF)
ConsoleWrite("D: " & _HexToString(_DecToNum($D, $base16)) & @CRLF)
ConsoleWrite(@CRLF)

;====================================================================================

;~ Func modpow($a, $b, $c)
;~  $res = 1
;~  While $b > 0
;~      ;/* Need long multiplication else this will overflow... */
;~      If Mod(StringRight($b, 1), 2) Then ;If BitAND($b,1) Then
;~          $res = _BigNum_Mod(_BigNum_Mul($res, $a), $c)
;~      EndIf
;~      $b = BitShift($b, 1)
;~      $a = _BigNum_Mod(_BigNum_Mul($a, $a), $c) ; /* Same deal here */
;~  WEnd
;~  Return $res
;~ EndFunc   ;==>modpow

Func NumberBase($num, $base = $SelectBase)
    If $base = 10 Then
        Return _DecToNum($num, $base10)
    ElseIf $base = 16 Then
        Return _DecToNum($num, $base16)
    ElseIf $base = 60 Then
        Return _DecToNum($num, $base60_2)
    ElseIf $base = 64 Then
        Return _DecToNum($num, $base64)
    EndIf
    Return $num
EndFunc   ;==>NumberBase

Func _DecToNum($iDec, $Symbol)
    Local $Out, $ost
    $Symbol = StringSplit($Symbol, '')
    If @error Or $Symbol[0] < 2 Then Return SetError(1, 0, $iDec)
    Do
        $ost = _BigNum_Mod($iDec, $Symbol[0])
        $iDec = _BigNum_Div(_BigNum_Sub($iDec, $ost), $Symbol[0])
        $Out = $Symbol[$ost + 1] & $Out
    Until Not Number($iDec)
    Return SetError(0, $Symbol[0], $Out)
EndFunc   ;==>_DecToNum

Func _NumToDec($num, $sSymbol, $casesense = 1)
    Local $i, $iPos, $Len, $n, $Out
    $Len = StringLen($sSymbol)
    If $Len < 2 Then Return SetError(1, 0, $num)
    
    $n = StringSplit($num, '')
    For $i = 1 To $n[0]
        $iPos = StringInStr($sSymbol, $n[$i], $casesense)
        If Not $iPos Then Return SetError(2, 0, $num)
        $Out = _BigNum_Add(_BigNum_Mul($iPos - 1, _BigNum_Pow($Len, $n[0] - $i)), $Out)
    Next
    Return SetError(0, $Len, $Out)
EndFunc   ;==>_NumToDec


; #FUNCTION# ;====================================================================================
;
; Name...........: _BigNum_PowerMod
; Description ...: Modular Exponentiation Mod($n^$e, $k)
; Syntax.........: _BigNum_Pow($n, $e, $k)
; Parameters ....: $n - Positive StringNumber: Digits"0"..."9"
;                  $e - Positive StringNumber: Exponent
;                  $k - Positive StringNumber: Modulus
; Return values .: Success - Result Mod($n^$e, $k)
;                  Failure - -1, sets @error to 1 if $n is not a positive valid StringNumber
;                            -1, sets @error to 2 if $e is not a positive valid StringNumber
;                            -1, sets @error to 3 if $k is not a positive valid StringNumber
; Author ........: jchd
; Date ..........: 17.12.13
; Remarks .......: Fractional exponents not allowed - use BigNum_n_root instead.
; ;===============================================================================================
Func _BigNum_PowerMod($n, $e, $k)
    If Not __BigNum_IsValid_3($n, $e, $k) Then Return SetError(1, 0, -1)

    Local $res = "1"

    While $e <> "0"
        If Mod(StringRight($e, 1), 2) Then
            $res = _BigNum_Mod(_BigNum_Mul($res, $n), $k)
            $e = _BigNum_Sub($e, "1")
        EndIf
        $n = _BigNum_Mod(_BigNum_Mul($n, $n), $k)
        $e = _BigNum_Div($e, "2")
    WEnd

    Return $res
EndFunc   ;==>_BigNum_PowerMod

Func __BigNum_IsValid_3($sX, $sY, $sZ)
    If StringRegExp($sX, "[^0-9.-]") Or StringRegExp($sY, "[^0-9.-]") Or StringRegExp($sZ, "[^0-9.-]") Then Return False
    Return True
EndFunc   ;==>__BigNum_IsValid_3

 

 

RSA 2 (TEST NOW).zip

Edited by Osys2010
Link to comment
Share on other sites

  • 2 years later...

Hello, Osys2010, thank you very much for the code provided. It is normal to test with your code. It is a pity that the decryption fails after replacing the P, Q, and D values generated by RSATool2v17.exe. I don't know why.

20190301151206.thumb.jpg.f8a92e0fced9e503a09f26d0fabf936e.jpg

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...