Most of the code found on the Internet does not support Chinese encoding and decoding. Some modifications have been made to support Chinese decoding. The encoding has not been completed yet.
Public Key (1 to 3) as long
Private const base64 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /"
Public sub genkey ()
Dim D as long, Phi as long, e as long
Dim M as long, X as long, Q As long
Dim P as long
Randomize
On Error goto top
Top:
P = RND * 1000/1
If isprime (p) = false Then goto top
Sel_q:
Q = RND * 1000/1
If isprime (q) = false Then goto sel_q
N = p * q/1
Phi = (p-1) * (Q-1)/1
D = RND * n/1
If D = 0 or n = 0 or D = 1 Then goto top
E = Euler (PHI, d)
If e = 0 or E = 1 Then goto top
X = mult (255, E, n)
If not mult (x, D, n) = 255 then
Doevents
Goto top
Elseif mult (x, D, n) = 255 then
Key (1) = E
Key (2) = d
Key (3) = N
End if
End sub
Public Function Euler (byval A as long, byval B as long) as long
On Error goto error2
R1 = A: r = B
P1 = 0: P = 1
Q1 = 2: q = 0
N =-1
Do until r = 0
R2 = R1: r1 = r
P2 = p1: p1 = P
Q2 = Q1: Q1 = Q
N = n + 1
R = R2 mod r1
C = R2/r1
P = (C * P1) + p2
Q = (C * Q1) + q2
Loop
S = (B * P1)-(A * Q1)
If S> 0 then
X = p1
Else
X = (0-P1) +
End if
Euler = x
Exit Function
Error2:
Euler = 0
End Function
Public Function mult (byval X as long, byval P as long, byval M as long) as long
Y = 1
On Error goto error1
Do While P> 0
Do While (p/2) = (p/2)
X = (x * X) mod m
P = p/2
Loop
Y = (x * Y) mod m
P = p-1
Loop
Mult = y
Exit Function
Error1:
Y = 0
End Function
Public Function isprime (lngnumber as long) as Boolean
Dim lngcount as long
Dim lngsqr as long
Dim X as long
Lngsqr = sqr (lngnumber) 'Get the int square root
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
Public Function base64_encode (decryptedtext as string) as string
Dim C1, C2, C3 as integer
Dim W1 as integer
Dim W2 as integer
Dim W3 as integer
Dim W4 as integer
Dim N as integer
Dim retry as string
For n = 1 to lenb (strconv (decryptedtext, vbfromunicode) Step 3
C1 = ASCB (midb $ (decryptedtext, N, 1 ))
C2 = ASCB (mid $ (decryptedtext, n + 1, 1) + chrb $(0 ))
C3 = ASCB (mid $ (decryptedtext, N + 2, 1) + chrb $(0 ))
W1 = int (C1/4)
W2 = (C1 and 3) * 16 + int (C2/16)
If lenb (strconv (decryptedtext, vbfromunicode)> = n + 1 then W3 = (C2 and 15) * 4 + int (C3/64) else W3 =-1
If lenb (strconv (decryptedtext, vbfromunicode)> = n + 2 then W4 = C3 and 63 else W4 =-1
Retry = retry + mimeencode (W1) + mimeencode (W2) + mimeencode (W3) + mimeencode (W4)
Next
Base64_encode = retry
End Function
Public Function base64_decode (A as string) as string
Dim W1 as integer
Dim W2 as integer
Dim W3 as integer
Dim W4 as integer
Dim N as integer
Dim retry as string
For n = 1 to Len (a) Step 4
W1 = mimedecode (mid $ (A, N, 1 ))
W2 = mimedecode (mid $ (A, n + 1, 1 ))
W3 = mimedecode (mid $ (A, N + 2, 1 ))
W4 = mimedecode (mid $ (A, N + 3, 1 ))
If W2> = 0 then retry = retry + chrb $(W1*4 + int (W2/16) and 255 ))
If W3> = 0 then retry = retry + chrb $(W2*16 + int (W3/4) and 255 ))
If W4> = 0 then retry = retry + chrb $(W3*64 + W4) and 255 ))
Next
Base64_decode = strconv (retry, vbunicode)
End Function
Public Function mimeencode (w as integer) as string
If W> = 0 then mimeencode = mid $ (base64, W + 1, 1) else mimeencode = ""
End Function
Private function mimedecode (A as string) as integer
If Len (A) = 0 then mimedecode =-1: Exit Function
Mimedecode = instr (base64, a)-1
End Function
Public Function encode (byval indium as string, byval e as long, byval N as long) as string
Dim s as string
S = ""
M = Indium
If M = "" Then exit function
S = mult (clng (ASC (mid (M, 1, 1), E, n)
For I = 2 to Len (m)
S = S & "+" & mult (clng (ASC (mid (M, I, 1), E, n)
Next I
Encode = base64_encode (s)
End Function
Public Function decode (byval indium as string, byval D as long, byval N as long) as string
St = ""
IND = base64_decode (indium)
For I = 1 to Len (IND)
NXT = instr (I, IND, "+ ")
If not NXT = 0 then
Tok = Val (mid (IND, I, NXT ))
Else
Tok = Val (mid (IND, I ))
End if
St = ST + CHR (mult (clng (Tok), d, n ))
If not NXT = 0 then
I = NXT
Else
I = Len (IND)
End if
Next I
Decode = ST
End Function