2024-11-21

Navigation

Skip Navigation Links

Hash algorithms

Asymmetric Algorithms

Symmetric Cipher Algorithms

Encoding Algorithms

Compression Algorithms

Pseudo Random Number Algorithms

Steganography

Library Wrappers

String Comparison

Others

Syntax highlighting by Prism
PBCrypto.com Mirror

PC1

Algorithm creator(s)

Alexander Pukall


PB author(s)

Wayne Diamond


Description

A stream cipher with a retroaction function, uses a 128-bit key.


Note


Source

https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/24919-pc1-encryption-128-bit-key-implementation?t=24270


See also

n/a


Source Code

Download source code file pc1.bas (Right-click -> "Save as ...")

'#COMPILE EXE
'#INCLUDE "WIN32API.INC"

GLOBAL x1a0() AS LONG
GLOBAL cle() AS LONG
GLOBAL x1a2 AS LONG
GLOBAL inter AS LONG, res AS LONG, ax AS LONG, bx AS LONG
GLOBAL cx AS LONG, dx AS LONG, si AS LONG, tmp AS LONG
GLOBAL I AS LONG, c AS BYTE

SUB code()
ON ERROR RESUME NEXT
dx = (x1a2 + I) MOD 65536
ax = x1a0(I)
cx = &H15A
bx = &H4E35
tmp = ax
ax = si
si = tmp
tmp = ax
ax = dx
dx = tmp
IF (ax <> 0) THEN
ax = (ax * bx) MOD 65536
END IF
tmp = ax
ax = cx
cx = tmp
IF (ax <> 0) THEN
ax = (ax * si) MOD 65536
cx = (ax + cx) MOD 65536
END IF
tmp = ax
ax = si
si = tmp
ax = (ax * bx) MOD 65536
dx = (cx + dx) MOD 65536
ax = ax + 1
x1a2 = dx
x1a0(I) = ax
res = ax XOR dx
I = I + 1
END SUB
SUB Assemble()
ON ERROR RESUME NEXT
x1a0(0) = ((cle(1) * 256) + cle(2)) MOD 65536
code
inter = res
x1a0(1) = x1a0(0) XOR ((cle(3) * 256) + cle(4))
code
inter = inter XOR res
x1a0(2) = x1a0(1) XOR ((cle(5) * 256) + cle(6))
code
inter = inter XOR res
x1a0(3) = x1a0(2) XOR ((cle(7) * 256) + cle(8))
code
inter = inter XOR res
x1a0(4) = x1a0(3) XOR ((cle(9) * 256) + cle(10))
code
inter = inter XOR res
x1a0(5) = x1a0(4) XOR ((cle(11) * 256) + cle(12))
code
inter = inter XOR res
x1a0(6) = x1a0(5) XOR ((cle(13) * 256) + cle(14))
code
inter = inter XOR res
x1a0(7) = x1a0(6) XOR ((cle(15) * 256) + cle(16))
code
inter = inter XOR res
I = 0
END SUB
FUNCTION PC1ENC(encPassword AS STRING, encStringOut AS STRING) EXPORT AS STRING
ON ERROR RESUME NEXT
DIM encStringIn AS STRING
DIM fois AS LONG
DIM champ1 AS STRING
DIM lngchamp1 AS LONG
DIM cfc AS LONG, cfd AS LONG
DIM compte AS LONG
DIM c AS LONG, D AS LONG, E AS LONG
REDIM x1a0(9) AS LONG
REDIM cle(17) AS LONG
encStringIn = ""
si = 0
x1a2 = 0
I = 0
FOR fois = 1 TO 16
cle(fois) = 0
NEXT fois
champ1 = encPassword
lngchamp1 = LEN(champ1)
FOR fois = 1 TO lngchamp1
cle(fois) = ASC(MID$(champ1, fois, 1))
NEXT fois
champ1 = encStringOut
lngchamp1 = LEN(champ1)
FOR fois = 1 TO lngchamp1
c = ASC(MID$(champ1, fois, 1))
Assemble
cfc = (((inter / 256) * 256) - (inter MOD 256)) / 256
cfd = inter MOD 256
FOR compte = 1 TO 16
cle(compte) = cle(compte) XOR c
NEXT compte
c = c XOR (cfc XOR cfd)
D = (((c / 16) * 16) - (c MOD 16)) / 16
E = c MOD 16
encStringIn = encStringIn + CHR$(&H61 + D) ' d+&h61 give one letter range from a to p for the 4 high bits of c
encStringIn = encStringIn + CHR$(&H61 + E) ' e+&h61 give one letter range from a to p for the 4 low bits of c
NEXT fois
PC1ENC = encStringIn
END FUNCTION
FUNCTION PC1DEC(encPassword AS STRING, encStringIn AS STRING) EXPORT AS STRING
ON ERROR RESUME NEXT
DIM encStringOut AS STRING
DIM fois AS LONG
DIM champ1 AS STRING
DIM lngchamp1 AS LONG
DIM cfc AS LONG, cfd AS LONG
DIM compte AS LONG
DIM c AS LONG, D AS LONG, E AS LONG
REDIM x1a0(9) AS LONG
REDIM cle(17) AS LONG
encStringOut = ""
si = 0
x1a2 = 0
I = 0
FOR fois = 1 TO 16
cle(fois) = 0
NEXT fois
champ1 = encPassword
lngchamp1 = LEN(champ1)
FOR fois = 1 TO lngchamp1
cle(fois) = ASC(MID$(champ1, fois, 1))
NEXT fois
champ1 = encStringIn
lngchamp1 = LEN(champ1)
FOR fois = 1 TO lngchamp1
D = ASC(MID$(champ1, fois, 1))
IF (D - &H61) >= 0 THEN
D = D - &H61  ' to transform the letter to the 4 high bits of c
IF (D >= 0) AND (D <= 15) THEN
D = D * 16
END IF
END IF
IF (fois <> lngchamp1) THEN
fois = fois + 1
END IF
E = ASC(MID$(champ1, fois, 1))
IF (E - &H61) >= 0 THEN
E = E - &H61 ' to transform the letter to the 4 low bits of c
IF (E >= 0) AND (E <= 15) THEN
c = D + E
END IF
END IF
Assemble
cfc = (((inter / 256) * 256) - (inter MOD 256)) / 256
cfd = inter MOD 256
c = c XOR (cfc XOR cfd)
FOR compte = 1 TO 16
cle(compte) = cle(compte) XOR c
NEXT compte
encStringOut = encStringOut + CHR$(c)
NEXT fois
PC1DEC = encStringOut
END FUNCTION

'FUNCTION PBMAIN()
' Orig$ = "Original unencrypted string"
' Enc$ = PC1ENC("password",Orig$)
' Dec$ = PC1DEC("password",Enc$)
' MSGBOX " Original=" & Orig$ & Chr$(13) & Chr$(10) & _
'        "Encrypted=" & Enc$ & Chr$(13) & Chr$(10) & _
'        "Decrypted=" & Dec$
'END FUNCTION

Mirror provided by Knuth Konrad