Code: Select all
; Description :
; AES ( Rijndael ) 256-Bit Encryption with added features ( Requires Droopy Lib )
; =======================================
; * Base64 Encoding
; * Can choose to encrypyt the data so it can be ...
; 0 : Decrypted on ANY computer
; 1 : Decrypted only on the SAME computer it was ENCRYPTED on.
;
; Note : In this case it uses the Computer Serial # , but you can use other data such as the HDD
; serial #, the Windows CD-Key , or any combination of other unique system identifying info
;
; OS : Windows
; PB Version : Should work with any version of PB that has AES Encoder() & Base64Encoder() commands.
; I only tested on PB 5.11 Final
; =========================================================================
;
; Rijndael+ 256-Bit Key Encryption / Decryption
;
DisableASM
DisableDebugger
Global sOut.s
Macro Sha(in)
SHA1Fingerprint(@in,MemoryStringLength(@in,#PB_Ascii))
EndMacro
Macro Md5(in)
MD5Fingerprint(@in,MemoryStringLength(@in,#PB_Ascii))
EndMacro
Macro Padded(x)
x + RSet(Chr(16-(Len(x)%16)),16-(Len(x)%16),Chr(16-(Len(x)%16)))
EndMacro
Procedure.s Rijndael256Encrypt(pTxt.s,pWrd.s,sec.a=0,sOut.s=#NULL$)
ok = #True
pTxt.s = Padded(pTxt.s)
bytes.q = MemoryStringLength(@pTxt,#PB_Ascii)
blocks.q = Int(bytes/16)-1
*cipher = AllocateMemory(bytes)
b64.q = Int(bytes*1.5)
*AesK = AllocateMemory(32)
*AesV = AllocateMemory(32)
Select sec.a
Case 0 ; <--------------------------------- Decrypt on ANY PC !
hash.s = Sha(pWrd)+Md5(pWrd)
Default ; Non-Zero <--------------------------------- Decrypt ONLY on same PC !
x.s = ComputerSerialNumber()
hash.s = Sha(x)+Md5(pWrd)
EndSelect
CopyMemory(@hash,*AesV,32)
CopyMemory(@hash+32,*AesK,32)
For block = 0 To blocks
os.q = block*16
If AESEncoder(@pTxt+os,*cipher+os,16,*AesK,256,*AesV) = 0
ok = #False
MessageRequester("Error","AES Encryption Error at Block Level",16)
Break
EndIf
Next
Select ok
Case #False
sOut.s = #NULL$
Case #True
*base64 = AllocateMemory(b64)
r.q = Base64Encoder(*cipher,bytes,*base64,b64)
sOut.s = PeekS(*base64,r)
FreeMemory(*base64)
EndSelect
FreeMemory(*AesV)
FreeMemory(*AesK)
FreeMemory(*cipher)
ProcedureReturn sOut
EndProcedure
Procedure.s Rijndael256Decrypt(eTxt.s,pWrd.s,sec.a=0,sOut.s=#NULL$)
ok = #True
b64.q = MemoryStringLength(@eTxt,#PB_Ascii)
*eBuff = AllocateMemory(Int(b64*1.5))
bytes.q = Base64Decoder(@eTxt,b64,*eBuff,Int(b64*1.5))
blocks.q=Int(bytes/16)-1
*cipher = AllocateMemory(bytes)
*AesK = AllocateMemory(32)
*AesV = AllocateMemory(32)
Select sec.a
Case 0 ; <--------------------------------- Decrypt on ANY PC !
hash.s = Sha(pWrd)+Md5(pWrd)
Default ; Non-Zero <--------------------------------- Decrypt ONLY on same PC !
x.s = ComputerSerialNumber()
hash.s = Sha(x)+Md5(pWrd)
EndSelect
CopyMemory(@hash,*AesV,32)
CopyMemory(@hash+32,*AesK,32)
For block = 0 To blocks
os.q = block*16
If AESDecoder(*eBuff+os,*cipher+os,16,*AesK,256,*AesV) = 0
ok = #False
MessageRequester("Error","AES Encryption Error at Block Level",16)
Break
EndIf
Next
Select ok
Case #False
sOut.s = #NULL$
Case #True
sOut.s = PeekS(*cipher,bytes)
pad.a = PeekA(*cipher+(bytes-1))
sOut.s = Left(sOut,MemoryStringLength(@sOut,#PB_Ascii)-pad.a)
EndSelect
FreeMemory(*AesV)
FreeMemory(*AesK)
FreeMemory(*cipher)
FreeMemory(*eBuff)
ProcedureReturn sOut
EndProcedure
; /************/
; /* TEST CODE */
; /************/
; /****************************************************************/
; /* Security is OFF - Allow the encrypted data to be decrypted on ANY computer ! */
; /****************************************************************/
text.s = "This is a secret message !!!"
pass.s = "password"
security.a = 0
enc.s = #NULL$
dec.s = #NULL$
sOut.s = #NULL$
enc.s = Rijndael256Encrypt(text.s,pass.s,security.a,sOut.s)
dec.s = Rijndael256Decrypt(enc.s,pass.s,security.a,sOut.s)
Caption.s = "Rijndael+ 256-Bit : Security = ANY PC"
Message.s = "PASSWORD" + Chr(10) + pass + Chr(10) + Chr(10)
Message.s + "PLAINTEXT" + Chr(10) + text + Chr(10) + Chr(10)
Message.s + "ENCRYPTED" + Chr(10) + enc + Chr(10) + Chr(10)
Message.s + "DECRYPTED"+Chr(10)+dec
MessageRequester(Caption.s,Message.s,80)
; /**************************************************************************************/
; /*Security is ON - Only allow the encrypted data to be decrypted on the SAME computer it was encrypted on */
; /**************************************************************************************/
text.s = "This is a secret message !!!"
pass.s = "password"
security.a = 1
enc.s = #NULL$
dec.s = #NULL$
sOut.s = #NULL$
enc.s = Rijndael256Encrypt(text.s,pass.s,security.a,sOut.s)
dec.s = Rijndael256Decrypt(enc.s,pass.s,security.a,sOut.s)
Caption.s = "Rijndael+ 256-Bit : Security = LOCAL PC"
Message.s = "PASSWORD" + Chr(10) + pass + Chr(10) + Chr(10)
Message.s + "PLAINTEXT" + Chr(10) + text + Chr(10) + Chr(10)
Message.s + "ENCRYPTED" + Chr(10) + enc + Chr(10) + Chr(10)
Message.s + "DECRYPTED"+Chr(10)+dec
MessageRequester(Caption.s,Message.s,80)