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

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

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

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

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

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:

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

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:

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..

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

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

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

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):

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()'

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

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

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):

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?

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

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

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