Sign in to follow this  
Followers 0
Osys2010

RSA 128 (1024, 2048..)

1 post in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • argumentum
      how to use RSA with Crypt.au3 ( or anything else )
      By argumentum
      so there is this post "Holographic Encryption with DARTIS" and the RSA came up.
      There is  _RSA_crypt.7z  from autoit-script.ru but the file is not available ( if anyone has the file, please get me a working link ) , so I don't know what or how it was done.
      My idea is to do the public key / private key (RSA) to exchange the hash/password ( call it what you will ), then, carry the rest of the communication with $CALG_AES_256 or the like. So it'd be doing a hybrid TCP/IP SSL, let's call it TCL 2.5  
      Anyhow, I need help for the simple reason that I'm quite clueless.
      CryptoAPI Cryptographic Service Providers may have a clue via the CryptoAPI ( but to me is all just words ).
      CryptEncrypt function say that: 
      The Microsoft Enhanced Cryptographic Provider supports direct encryption with RSA public keys and decryption with RSA private keys. The encryption uses PKCS #1 padding. On decryption, this padding is verified. The length of plaintext data that can be encrypted with a call to CryptEncrypt with an RSA key is the length of the key modulus minus eleven bytes. The eleven bytes is the chosen minimum for PKCS #1 padding. The ciphertext is returned in little-endian format. so it should be possible from server 2003 / XP onwards.
      Thank you all who dare to go at it  
    • giangnguyen
      Public Private Key encryption?
      By giangnguyen
      Anybody knows how I can apply Public-Private Key encryption? I found several threads but they are all outdated 
      Any ideas? I don't think it is included in advapi32 either, which is used by AutoIt atm