Jump to content

Recommended Posts

Posted (edited)
#include "bignum.au3"
#include <Array.au3>
#include <String.au3>


Global Const $ANS1_Tags[35][2] = [ _
["01","BOOLEAN"], _
["02","INTEGER"], _
["03","BIT STRING"], _
["04","OCTET STRING"], _
["05","NULL"], _
["06","OBJECT IDENTIFIER"], _
["07","ObjectDescriptor"], _
["08","EXTERNAL"], _
["09","REAL"], _
["0A","ENUMERATED"], _
["0B","EMBEDDED PDV"], _
["0C","UTF8String"], _
["0D","RELATIVE-OID"], _
["0E","TIME"], _
["0F","unknow"], _
["10","SEQUENCE"], _
["11","SET"], _
["12","NumericString"], _
["13","PrintableString"], _
["14","TeletexString, T61String"], _
["15","VideotexString"], _
["16","IA5String"], _
["17","UTCTime"], _
["18","GeneralizedTime"], _
["19","GraphicString"], _
["1A","VisibleString"], _
["1B","GeneralString"], _
["1C","UniversalString"], _
["1D","CHARACTER STRING"], _
["1E","BMPString"], _
["A0","[0]"], _
["A2","[2]"], _
["A5","[5]"], _
["40","Application"], _
["30","SEQUENCE"]]

;_ArrayDisplay($ANS1_Tags)

Local $exponent1, $exponent2, $coefficient, $prime1, $prime2, $privateExponent, $publicExponent, $modulus
Local $sFilePath = FileOpenDialog("Please select Key file", "", "Key File(*.*)", 1)
If @error Then Exit

Local $hFileOpen = FileOpen($sFilePath, 0)
Local $sFileRead = FileRead($hFileOpen)

if StringInStr($sFileRead,"-----BEGIN PRIVATE KEY-----") <> 0 Then
$PEM = Hex(_WinAPI_Base64Decode(_StringBetween($sFileRead,"-----BEGIN PRIVATE KEY-----","-----END PRIVATE KEY-----")[0]))
$arrayin = ASN1_Parse($PEM)
Private_MAP($arrayin)
ElseIf  StringInStr($sFileRead,"-----BEGIN RSA PRIVATE KEY-----") <> 0 Then
$PEM = Hex(_WinAPI_Base64Decode(_StringBetween($sFileRead,"-----BEGIN RSA PRIVATE KEY-----","-----END RSA PRIVATE KEY-----")[0]))
$arrayin = ASN1_Parse($PEM)
Private_MAP($arrayin)
ElseIf StringInStr($sFileRead,"-----BEGIN PUBLIC KEY-----") <> 0 Then
$PEM = Hex(_WinAPI_Base64Decode(_StringBetween($sFileRead,"-----BEGIN PUBLIC KEY-----","-----END PUBLIC KEY-----")[0]))
$arrayin = ASN1_Parse($PEM)
Puplic_MAP($arrayin)
ElseIf StringInStr($sFileRead,"-----BEGIN PUBLIC KEY-----") <> 0 Then
$PEM = Hex(_WinAPI_Base64Decode(_StringBetween($sFileRead,"-----BEGIN RSA PUBLIC KEY-----","-----END RSA PUBLIC KEY-----")[0]))
$arrayin = ASN1_Parse($PEM)
Puplic_MAP($arrayin)
EndIf


Func ASN1_Parse($data)
Local $dataLen, $selectstring, $selectstring_tag, $selectstring_len, $selectstring_len_Dec, $selectstring_data_start, $selectstring_data, $tag_offset, $hr, $result[0][8], $class = 0, $Data_offset = 0
Local $FC = 0, $temp_i, $temp_offset, $CD, $temp_data, $zero_add, $temp_original_len, $temp_original_data
$dataLen = StringLen($data)
$temp_original_data = $data
$temp_original_len = $dataLen
;MsgBox(0,"",$dataLen)
_ArrayAdd($result,"Class|ANS.1 TAG|Len TAG|Len Hex|Data String|TAG's Len|Len Dec|Data offset")
While $dataLen <> 0
for $i = 1 to $dataLen Step 2
    $selectstring = StringMid($data,$i,2)



    if $selectstring = "" Then
    ExitLoop
    EndIf

    if $selectstring = "00" Then
    $zero_add += 1
    EndIf

    $hr = _ArraySearch($ANS1_Tags, $selectstring)

    if $selectstring = "30" or $selectstring = "04"  or $selectstring = "03" or $selectstring = "A0" or $selectstring = "A5" or $selectstring = "A2" Then
    $FC = 1
    Else
    $FC = 0
    EndIf


    ;msgbox(0,$FC,$selectstring)
        if $hr <> -1  Then
        $selectstring_tag = StringMid($data,$i+2,2)
        if $selectstring_tag = "82" Then
        $selectstring_len = StringMid($data,$i+4,4)
        $selectstring_len_Dec = Dec(Hex(Binary("0x"&$selectstring_len)))
        $tag_offset = 8
        $selectstring_data_start = $i + 8
        ElseIf $selectstring_tag = "81" Then
        $selectstring_len = StringMid($data,$i+4,2)
        $selectstring_len_Dec = Dec(Hex(Binary("0x"&$selectstring_len)))
        $tag_offset = 6
        $selectstring_data_start = $i + 6
        Else
        $selectstring_len = $selectstring_tag
        $selectstring_len_Dec =  Dec(Hex(Binary("0x"&$selectstring_len)))
        $tag_offset = 4
        $selectstring_data_start = $i + 4
        EndIf
        if UBound($result) >= 2 Then
            if $class <> $result[UBound($result)-1][0] Then
            $Data_offset-=$result[UBound($result)-1][6]
            EndIf
        EndIf
        $Data_offset+=$zero_add
        $selectstring_data = StringMid($data,$selectstring_data_start,$selectstring_len_Dec*2)
        _ArrayAdd($result,$class&"|"&$selectstring&"|"&$selectstring_tag&"|"&$selectstring_len&"|"&$selectstring_data&"|"&$tag_offset/2&"|"&$selectstring_len_Dec&"|"&$Data_offset)
        $zero_add = 0
    ;if $FC = 1 Then
    if $FC = 1  and $class <> 0 Then
    $CD = $selectstring_len_Dec
    $temp_data = $data
    $class+=1
    $data = $selectstring_data
    $dataLen = StringLen($data)
    $i+=$tag_offset+$selectstring_len_Dec*2 - 2
    $temp_i = $i
    $Data_offset+=$tag_offset/2 + $selectstring_len_Dec
    $temp_Data_offset = $Data_offset
    $i = 1 - 2
    ContinueLoop
    EndIf
    if $CD <> 0 Then
    $CD = $CD - $selectstring_len_Dec - ($tag_offset/2)
    if $CD = 0 Then
        $i = $temp_i
        $Data_offset = $temp_Data_offset
        $data = $temp_data
        $dataLen = StringLen($data)
        $class-=1
        ContinueLoop
    EndIf
    EndIf
        $Data_offset+=$tag_offset/2 + $selectstring_len_Dec
        $i+=$tag_offset+$selectstring_len_Dec*2 - 2
        ;_ArrayDisplay($result)
        EndIf
Next
$class+=1
$data = $selectstring_data
$dataLen = StringLen($data)
if $FC = 0 Then
    ;MsgBox(0,"",$Data_offset)
    if $Data_offset*2 >= $temp_original_len Then
    ExitLoop
    Else
    $data = StringMid( $temp_original_data ,2*($i)+1,$temp_original_len-2*($i))
    $dataLen = StringLen($data)
    $class-=2
    EndIf
EndIf

WEnd
_ArrayDisplay($result)
Return $result
EndFunc


Func Puplic_MAP($data)
Local $CC = 0
For $i = UBound($data) - 1 to 0 step -1
    if $data[$i][1] = "02" Then
    $CC+=1
    if $CC = 2 Then
    $modulus = $data[$i][4]
    MsgBox(0,"modulus - n (HEX)",$modulus)
    MsgBox(0,"modulus - n (DEC)",_BigHex_ToBase2($modulus,16))
    elseif $CC = 1 Then
    $publicExponent = $data[$i][4]
    MsgBox(0,"publicExponent - e",Dec(Hex("0x"&$publicExponent)))
    EndIf
    EndIf
Next
EndFunc


Func Private_MAP($data)
Local $CC = 0
For $i = UBound($data) - 1 to 0 step -1
    if $data[$i][1] = "02" Then
    $CC+=1
    if $CC = 8 Then
    $modulus = $data[$i][4]
    MsgBox(0,"modulus - n (HEX)",$modulus)
    MsgBox(0,"modulus - n (DEC)",_BigHex_ToBase2($modulus,16))
    elseif $CC = 7 Then
    $publicExponent = $data[$i][4]
    MsgBox(0,"publicExponent - e",Dec(Hex("0x"&$publicExponent)))
    elseif $CC = 6 Then
    $privateExponent = $data[$i][4]
    MsgBox(0,"privateExponent - d",$privateExponent)
    elseif $CC = 5 Then
    $prime1 = $data[$i][4]
    MsgBox(0,"prime1 - p (HEX)",$prime1)
    MsgBox(0,"prime1 - p (DEC)",_BigHex_ToBase2($prime1,16))
    elseif $CC = 4 Then
    $prime2 = $data[$i][4]
    MsgBox(0,"prime2 - q (HEX)",$prime2)
    MsgBox(0,"prime2 - q (DEC)",_BigHex_ToBase2($prime2,16))
    elseif $CC = 3 Then
    $exponent1 = $data[$i][4]
    MsgBox(0,"exponent1 - d mod (p - 1)",$exponent1)
    elseif $CC = 2 Then
    $exponent2 = $data[$i][4]
    MsgBox(0,"exponent2 - d mod (q - 1)",$exponent2)
    elseif $CC = 1 Then
    $coefficient = $data[$i][4]
    MsgBox(0,"coefficient - q–1 mod p",$coefficient)
    EndIf
    EndIf

Next
EndFunc

Func _WinAPI_Base64Decode($sB64String)
    Local $aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", $sB64String, "dword", 0, "dword", 1, "ptr", 0, "dword*", 0, "ptr", 0, "ptr", 0)
    If @error Or Not $aCrypt[0] Then Return SetError(1, 0, "")
    Local $bBuffer = DllStructCreate("byte[" & $aCrypt[5] & "]")
    $aCrypt = DllCall("Crypt32.dll", "bool", "CryptStringToBinaryA", "str", $sB64String, "dword", 0, "dword", 1, "struct*", $bBuffer, "dword*", $aCrypt[5], "ptr", 0, "ptr", 0)
    If @error Or Not $aCrypt[0] Then Return SetError(2, 0, "")
    Return DllStructGetData($bBuffer, 1)
EndFunc

1.Execute process

2.Select key file

3.Pop the information

1.pem 2.pem

Edited by konya

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...