PSC1 Errors

New to FreeBASIC? Post your questions here.
Post Reply
gerry
Posts: 50
Joined: Oct 04, 2021 7:29

PSC1 Errors

Post by gerry »

Help with this code.. Too many compilation errors. :|

Code: Select all

Public n As Long

Public PrvI As Long
Public PubI As Long

Public Prv As Long
Public Pub As Long

Public ValueIndex As Long

Sub GenKey(ByVal NMin As Long, ByVal NMax As Long)

Dim tPub As Long

Randomize

Top:

n = Int((NMax * Rnd) + NMax)
Prv = RndPrime(1, n)
Pub = Int((n * Rnd) + 1)

tPub = Pub
Do Until Pub * Prv Mod n = 1
   Pub = Pub + 1
   If Pub = tPub Then GoTo Top
   If Pub > n Then Pub = 1
Loop

PrvI = 1
PubI = n - PrvI
ValueIndex = 1

End Sub

Function RndPrime(Min As Long, Max As Long) As Long

LoopBig:
    
    RndPrime = Int((Max * Rnd) + Min)

loopSmall:
    
    RndPrime = RndPrime + 1
    If RndPrime > Max Then GoTo LoopBig
    If IsPrime(RndPrime) = False Then GoTo loopSmall
    If RndPrime = 0 Or RndPrime = 1 Then GoTo LoopBig

End Function

Private Function IsPrime(lngNumber) As Boolean

Dim lngCount As Long
Dim lngSqr As Long
Dim X As Long
lngSqr = Sqr(lngNumber)

If lngNumber < 2 Then
    IsPrime = False
    Exit Function
End If

lngCount = 2
IsPrime = True

If lngNumber Mod lngCount = 0& Then
    IsPrime = False
    Exit Function
End If

lngCount = 3

For X& = lngCount To lngSqr Step 2
    If lngNumber Mod X& = 0 Then
        IsPrime = False
        Exit Function
    End If
Next

End Function

Function Encrypt(m As Long) As Long

    Encrypt = ((m + PubI) * Pub) Mod n
    PubI = (PubI * (ValueIndex * m + 1)) Mod n
    
End Function

Function Decrypt(C As Long) As Long

    Decrypt = ((C * Prv) + PrvI) Mod n
    PrvI = (PrvI * (ValueIndex * Decrypt + 1)) Mod n
   
    ValueIndex = ValueIndex Mod n

End Function

Function EncryptBt(b As String) As String

    EncryptBt = Hex(Encrypt(Asc(Mid(b, 1, 1))))

End Function

Function DecryptBt(b As String) As String

    DecryptBt = Chr(Decrypt(Val("&H" + b)))

End Function

Function EncryptBk(Block As String) As String

Dim Length As Long
Dim iDX As Long

Length = Len(Block) + 1
iDX = 1
EncryptBk = ""

Do Until iDX = Length
    
    EncryptBk = EncryptBk + EncryptBt(Mid(Block, iDX, 1)) + " "
    iDX = iDX + 1

Loop

End Function

Function DecryptBk(Block As String) As String

Dim temp As String
Dim iDX As Long

temp = Block
iDX = 1
DecryptBk = ""


Do Until InStr(1, temp, " ") = 0

    DecryptBk = DecryptBk + DecryptBt(Mid(temp, 1, InStr(1, temp, " ")))
    temp = Mid(temp, InStr(1, temp, " ") + 1, Len(temp) - InStr(1, temp, " "))
    iDX = iDX + 1

Loop

End Function
fxm
Moderator
Posts: 11204
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: PSC1 Errors

Post by fxm »

Many errors repeated from the first line.
They are easily correctable by you if you look a little at the documentation (how the Public keyword can be used).

Make an effort to make the first corrections by yourself.
It's not a good thing for you that we provide yourself directly with a corrected version
gerry
Posts: 50
Joined: Oct 04, 2021 7:29

Re: PSC1 Errors

Post by gerry »

fxm wrote: Jun 26, 2022 15:32 Many errors repeated from the first line.
They are easily correctable by you if you look a little at the documentation (how the Public keyword can be used).

Make an effort to make the first corrections by yourself.
It's not a good thing for you that we provide yourself directly with a corrected version
I can't do this.. :|
gerry
Posts: 50
Joined: Oct 04, 2021 7:29

Re: PSC1 Errors

Post by gerry »

gerry wrote: Jun 27, 2022 6:26
fxm wrote: Jun 26, 2022 15:32 Many errors repeated from the first line.
They are easily correctable by you if you look a little at the documentation (how the Public keyword can be used).

Make an effort to make the first corrections by yourself.
It's not a good thing for you that we provide yourself directly with a corrected version
I can't do this.. :|
Will anyone help?..
srvaldez
Posts: 3026
Joined: Sep 25, 2005 21:54

Re: PSC1 Errors

Post by srvaldez »

gerry, have you read the help file on Public ?
Public in FB has a different meaning than in VB, try replacing public with dim shared
gerry
Posts: 50
Joined: Oct 04, 2021 7:29

Re: PSC1 Errors

Post by gerry »

srvaldez wrote: Jun 27, 2022 16:27 gerry, have you read the help file on Public ?
Public in FB has a different meaning than in VB, try replacing public with dim shared
the compiler swears at this function.. I have set everything correctly

Code: Select all

C:\Users\User\Desktop>fbc32 1.bas
1.bas(13) error 1: Argument count mismatch, found '+' in 'DecryptBk = DecryptBk
+ DecryptBt(Mid(temp, 1, InStr(1, temp, " ")))'

C:\Users\User\Desktop>fbc32 1.bas
1.bas(13) error 1: Argument count mismatch, found '+' in 'DecryptBk = DecryptBk
+ DecryptBt(Mid(temp, 1, InStr(1, temp, " ")))'

C:\Users\User\Desktop>

Code: Select all

Function DecryptBk(Block As String) As String

Dim temp As String
Dim iDX As Long

temp = Block
iDX = 1
DecryptBk = ""


Do Until InStr(1, temp, " ") = 0

    DecryptBk = DecryptBk + DecryptBt(Mid(temp, 1, InStr(1, temp, " ")))
    temp = Mid(temp, InStr(1, temp, " ") + 1, Len(temp) - InStr(1, temp, " "))
    iDX = iDX + 1

Loop

End Function
fxm
Moderator
Posts: 11204
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: PSC1 Errors

Post by fxm »

Extract from FUNCTION documentation page:
Functions can also return values by assigning the Function keyword or the function's identifier to the desired return value (but Function keyword or function's identifier does not allow to evaluate the current assigned value).

Instead, you must use a local variable ('DB' there) as an intermediary:

Code: Select all

Function DecryptBk(Block As String) As String
    Dim temp As String
    Dim iDX As Long
    Dim DB As String

    temp = Block
    iDX = 1
    DB = ""
    Do Until InStr(1, temp, " ") = 0
        DB = DB + DecryptBt(Mid(temp, 1, InStr(1, temp, " ")))
        temp = Mid(temp, InStr(1, temp, " ") + 1, Len(temp) - InStr(1, temp, " "))
        iDX = iDX + 1
    Loop
    DecryptBk = DB
End Function
gerry
Posts: 50
Joined: Oct 04, 2021 7:29

Re: PSC1 Errors

Post by gerry »

fxm wrote: Jun 27, 2022 18:39 Extract from FUNCTION documentation page:
Functions can also return values by assigning the Function keyword or the function's identifier to the desired return value (but Function keyword or function's identifier does not allow to evaluate the current assigned value).

Instead, you must use a local variable ('DB' there) as an intermediary:

Code: Select all

Function DecryptBk(Block As String) As String
    Dim temp As String
    Dim iDX As Long
    Dim DB As String

    temp = Block
    iDX = 1
    DB = ""
    Do Until InStr(1, temp, " ") = 0
        DB = DB + DecryptBt(Mid(temp, 1, InStr(1, temp, " ")))
        temp = Mid(temp, InStr(1, temp, " ") + 1, Len(temp) - InStr(1, temp, " "))
        iDX = iDX + 1
    Loop
    DecryptBk = DB
End Function
everything works crookedly in this code, not only this function..
fxm
Moderator
Posts: 11204
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: PSC1 Errors

Post by fxm »

- For other compilation errors, you must modify with the same principle (than 'DecryptBk()') the other 3 functions:
'RndPrime()'
'Decrypt()'
'EncryptBk()'

Code: Select all

Function RndPrime(Min As Long, Max As Long) As Long
    Dim RP As Long
LoopBig:
    RP = Int((Max * Rnd) + Min)
loopSmall:
    RP = RP + 1
    If RP > Max Then GoTo LoopBig
    If IsPrime(RP) = False Then GoTo loopSmall
    If RP = 0 Or RP = 1 Then GoTo LoopBig
    RndPrime = RP
End Function

Code: Select all

Function Decrypt(C As Long) As Long
    Dim D As Long
    D = ((C * Prv) + PrvI) Mod n
    Decrypt = D
    PrvI = (PrvI * (ValueIndex * D + 1)) Mod n
    ValueIndex = ValueIndex Mod n
End Function

Code: Select all

Function EncryptBk(Block As String) As String
    Dim Length As Long
    Dim iDX As Long
    Dim EB As String

    Length = Len(Block) + 1
    iDX = 1
    EB = ""
    Do Until iDX = Length
        EB = EB+ EncryptBt(Mid(Block, iDX, 1)) + " "
        iDX = iDX + 1
    Loop
    EncryptBk = EB
End Function

- Otherwise, 2 functions to declare before calling them (at the top of the code for example):

Code: Select all

Declare Function RndPrime(Min As Long, Max As Long) As Long
Declare Function IsPrime(lngNumber As Long) As Boolean
gerry
Posts: 50
Joined: Oct 04, 2021 7:29

Re: PSC1 Errors

Post by gerry »

fxm wrote: Jun 28, 2022 6:04 - For other compilation errors, you must modify with the same principle (than 'DecryptBk()') the other 3 functions:
'RndPrime()'
'Decrypt()'
'EncryptBk()'

Code: Select all

Function RndPrime(Min As Long, Max As Long) As Long
    Dim RP As Long
LoopBig:
    RP = Int((Max * Rnd) + Min)
loopSmall:
    RP = RP + 1
    If RP > Max Then GoTo LoopBig
    If IsPrime(RP) = False Then GoTo loopSmall
    If RP = 0 Or RP = 1 Then GoTo LoopBig
    RndPrime = RP
End Function

Code: Select all

Function Decrypt(C As Long) As Long
    Dim D As Long
    D = ((C * Prv) + PrvI) Mod n
    Decrypt = D
    PrvI = (PrvI * (ValueIndex * D + 1)) Mod n
    ValueIndex = ValueIndex Mod n
End Function

Code: Select all

Function EncryptBk(Block As String) As String
    Dim Length As Long
    Dim iDX As Long
    Dim EB As String

    Length = Len(Block) + 1
    iDX = 1
    EB = ""
    Do Until iDX = Length
        EB = EB+ EncryptBt(Mid(Block, iDX, 1)) + " "
        iDX = iDX + 1
    Loop
    EncryptBk = EB
End Function

- Otherwise, 2 functions to declare before calling them (at the top of the code for example):

Code: Select all

Declare Function RndPrime(Min As Long, Max As Long) As Long
Declare Function IsPrime(lngNumber As Long) As Boolean
Thanks!!!

And how to encrypt and decrypt information?
fxm
Moderator
Posts: 11204
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: PSC1 Errors

Post by fxm »

gerry wrote: Jun 28, 2022 8:04 Thanks!!!

And how to encrypt and decrypt information?
That is a different topic than the one of compilation errors due to misuse of FreeBASIC syntax.
Where does your original code come from?
Have you searched the forum to encrypt and decrypt?
gerry
Posts: 50
Joined: Oct 04, 2021 7:29

Re: PSC1 Errors

Post by gerry »

fxm wrote: Jun 28, 2022 8:30
gerry wrote: Jun 28, 2022 8:04 Thanks!!!

And how to encrypt and decrypt information?
That is a different topic than the one of compilation errors due to misuse of FreeBASIC syntax.
Where does your original code come from?
Have you searched the forum to encrypt and decrypt?
I mean, how does this code work? What you helped me with, all this code somehow works
dodicat
Posts: 7519
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: PSC1 Errors

Post by dodicat »

Here is a fast random prime in the uinteger range.

Code: Select all


Function isprime(n As Ulongint) As boolean
    If (n=2) Or (n=3) Then Return 1
    If n Mod 2 = 0 Then Return 0
    If n Mod 3 = 0 Then Return 0
    Dim As Ulongint i=0
    While i*i<=n:i+=6
        If N Mod (i-1) = 0 Then Return 0
        If N Mod (i+1) = 0 Then Return 0
    Wend
    Return 1
End Function

Function random_prime(min As Ulongint,max As Ulongint) As Ulongint
    Const u32max = &hFFFFFFFFul 
    #define rnd64 (culngint(rnd*u32max)+(culngint(rnd*u32max) shl 32))
    #define Intrange(f,l)  (culngint(rnd64) mod (((l)-(f))+1)) + (f)
    Dim As Ulongint u=IntRange(min,max)
    If isprime(u) Then Return u Else Return random_prime(min, max)
End Function

Randomize
Dim As Double t=Timer
For z As Long=0 To 100
    Print random_prime(400000000,500000000);" ";
Next z
Print
Print Timer-t;"  seconds"

Print "Done press any key to end"
Sleep
fxm
Moderator
Posts: 11204
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: PSC1 Errors

Post by fxm »

See continuation topic: PSC1 Encrypt, Decrypt (viewtopic.php?p=292986#p292986 precisely)
gerry
Posts: 50
Joined: Oct 04, 2021 7:29

Re: PSC1 Errors

Post by gerry »

dodicat wrote: Jun 28, 2022 11:34 Here is a fast random prime in the uinteger range.

Code: Select all


Function isprime(n As Ulongint) As boolean
    If (n=2) Or (n=3) Then Return 1
    If n Mod 2 = 0 Then Return 0
    If n Mod 3 = 0 Then Return 0
    Dim As Ulongint i=0
    While i*i<=n:i+=6
        If N Mod (i-1) = 0 Then Return 0
        If N Mod (i+1) = 0 Then Return 0
    Wend
    Return 1
End Function

Function random_prime(min As Ulongint,max As Ulongint) As Ulongint
    Const u32max = &hFFFFFFFFul 
    #define rnd64 (culngint(rnd*u32max)+(culngint(rnd*u32max) shl 32))
    #define Intrange(f,l)  (culngint(rnd64) mod (((l)-(f))+1)) + (f)
    Dim As Ulongint u=IntRange(min,max)
    If isprime(u) Then Return u Else Return random_prime(min, max)
End Function

Randomize
Dim As Double t=Timer
For z As Long=0 To 100
    Print random_prime(400000000,500000000);" ";
Next z
Print
Print Timer-t;"  seconds"

Print "Done press any key to end"
Sleep
Thanks!
Post Reply