Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Caesar-cipher/PureBasic/caesar-cipher-1.basic
Normal file
37
Task/Caesar-cipher/PureBasic/caesar-cipher-1.basic
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
Procedure.s CC_encrypt(plainText.s, key, reverse = 0)
|
||||
;if reverse <> 0 then reverse the encryption (decrypt)
|
||||
If reverse: reverse = 26: key = 26 - key: EndIf
|
||||
|
||||
Static alphabet$ = "ABCEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
|
||||
|
||||
Protected result.s, i, length = Len(plainText), letter.s, legal
|
||||
If key < 1 Or key > 25: ProcedureReturn: EndIf ;keep key in range
|
||||
|
||||
For i = 1 To length
|
||||
letter = Mid(plainText, i, 1)
|
||||
legal = FindString(alphabet$, letter, 1 + reverse)
|
||||
If legal
|
||||
result + Mid(alphabet$, legal + key, 1)
|
||||
Else
|
||||
result + letter
|
||||
EndIf
|
||||
Next
|
||||
ProcedureReturn result
|
||||
EndProcedure
|
||||
|
||||
Procedure.s CC_decrypt(cypherText.s, key)
|
||||
ProcedureReturn CC_encrypt(cypherText, key, 1)
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
Define key, plainText.s, encryptedText.s, decryptedText.s
|
||||
|
||||
key = Random(24) + 1 ;get a random key in the range 1 -> 25
|
||||
|
||||
plainText = "The quick brown fox jumped over the lazy dogs.": PrintN(RSet("Plain text = ", 17) + #DQUOTE$ + plainText + #DQUOTE$)
|
||||
encryptedText = CC_encrypt(plainText, key): PrintN(RSet("Encrypted text = ", 17) + #DQUOTE$ + encryptedText + #DQUOTE$)
|
||||
decryptedText = CC_decrypt(encryptedText, key): PrintN(RSet("Decrypted text = ", 17) + #DQUOTE$ + decryptedText + #DQUOTE$)
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
Loading…
Add table
Add a link
Reference in a new issue