Ok, ein bisschen pas kann ich posten. RSA ist nicht dabei weil wir da eine spezielle Lib für haben, aber das Prinzip ist auch dafür schnell umgesetzt:
Code: Alles auswählen
Global libCryptocName.s = ""
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Macro CrProtoType
PrototypeC
EndMacro
CompilerEndIf
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
Macro CrProtoType
PrototypeC
EndMacro
CompilerEndIf
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Macro CrProtoType
PrototypeC
EndMacro
CompilerEndIf
; -------------------------------------
; INITIALIZE CRYPTO-API's
; -------------------------------------
#CC_ERROR_BUFFER = 512
#CC_NO_PADDING = 0
#CC_ZEROS_PADDING = 1
#CC_PKCS_PADDING = 2
#CC_STRING = 0
#CC_BYTES = 1
#CC_FILE = 2
;{ prototyping and opening cryptoc
CrProtoType cc_EncryptFile(szAlg.sbStr, szInFile.sbStr, szHexKey.sbStr, *szOutFileOrResult, padScheme.i, szHexIVector.sbStr)
CrProtoType cc_DecryptFile(szAlg.sbStr, szInFile.sbStr, szHexKey.sbStr, *szOutFileOrResult, padScheme.i, szHexIVector.sbStr)
CrProtoType cc_EncryptBytes(szAlg.sbStr, *input, *inLen, szHexKey.sbStr, *szHexResult, padScheme.i, szHexIVector.sbStr)
CrProtoType cc_DecryptBytes(szAlg.sbStr, *input, *inLen, szHexKey.sbStr, *szHexResult, padScheme.i, szHexIVector.sbStr)
CrProtoType cc_EncryptString(szAlg.sbStr, szHextext.sbStr, szHexKey.sbStr, *szHexResult, padScheme.i, szHexIVector.sbStr)
CrProtoType cc_DecryptString(szAlg.sbStr, szHextext.sbStr, szHexKey.sbStr, *szHexResult, padScheme.i, szHexIVector.sbStr)
CrProtoType cc_GenerateRandomHex(byteLen.i, *szHexResult)
CrProtoType cc_GetRemainderFromGEM(generator.sbStr, exponent.sbStr, modulus.sbStr, *szResult, *charLength);
CrProtoType cc_HexHashInit(szAlg.sbStr, *ppHashCtx)
CrProtoType cc_HexHashUpdate(*pHashCtx, *pBuf, len.i)
CrProtoType cc_HexHashFinish(*pHashCtx, *szResult)
CrProtoType cc_StringHexHash(szMessage.sbStr, szAlg.sbStr, *szResult)
CrProtoType cc_MemoryHexHash(*pBuf, len.i, szAlg.sbStr, *szResult)
CrProtoType cc_FileHexHash(*szFilename, szAlg.sbStr, *szResult)
CrProtoType cc_HexEncodeBytes(*input, len.i, *szResult)
CrProtoType cc_HexDecodeBytes(input.sbStr, *result)
CrProtoType cc_GenerateRSAKey(bitLen.i, *pubKey, *privKey, *charLength)
CrProtoType cc_RSAEncrypt(pubKey.sbStr, msg.sbStr, *cipher, *charLength)
CrProtoType cc_RSADecrypt(privKey.sbStr, cipher.sbStr, *msg, *charLength)
CrProtoType cc_RSASign(privKey.sbStr, msg.sbStr, *signature, *charLength)
CrProtoType cc_RSAVerify(pubKeyStr.sbStr, msg.sbStr, signature.sbStr, *errmsg, errLength)
Global cc_EncryptFile.cc_EncryptFile
Global cc_DecryptFile.cc_DecryptFile
Global cc_EncryptBytes.cc_EncryptBytes
Global cc_DecryptBytes.cc_DecryptBytes
Global cc_EncryptString.cc_EncryptString
Global cc_DecryptString.cc_DecryptString
Global cc_GenerateRandomHex.cc_GenerateRandomHex
Global cc_GetRemainderFromGEM.cc_GetRemainderFromGEM
Global cc_HexHashInit.cc_HexHashInit
Global cc_HexHashUpdate.cc_HexHashUpdate
Global cc_HexHashFinish.cc_HexHashFinish
Global cc_StringHexHash.cc_StringHexHash
Global cc_MemoryHexHash.cc_MemoryHexHash
Global cc_FileHexHash.cc_FileHexHash
Global cc_HexEncodeBytes.cc_HexEncodeBytes
Global cc_HexDecodeBytes.cc_HexDecodeBytes
Global cc_GenerateRSAKey.cc_GenerateRSAKey
Global cc_RSAEncrypt.cc_RSAEncrypt
Global cc_RSADecrypt.cc_RSADecrypt
Global cc_RSASign.cc_RSASign
Global cc_RSAVerify.cc_RSAVerify
;}
Global hCryptoc
Procedure.b __InitCryptoDLL()
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
libCryptocName.s = "cryptoc.dll"
CompilerEndIf
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
libCryptocName.s = "x86_64/libcryptoc.so"
CompilerElse
libCryptocName.s = "/usr/lib/libcryptoc.so"
If FileSize(libCryptocName.s) < 0
libCryptocName.s = GetHomeDirectory() + "git/includes/third-party/libcryptoc.so" ; try development system location
If FileSize(libCryptocName.s) < 0
libCryptocName.s = "libcryptoc.so"; use default system path (last chance)
EndIf
EndIf
CompilerEndIf
CompilerEndIf
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
libCryptocName.s = GetPathPart(ProgramFilename()) + "../System/Library/libcryptoc.dylib"
If FileSize(libCryptocName.s) < 0
libCryptocName.s = GetHomeDirectory() + "src/includes/third-party/libcryptoc.dylib" ; try development system location
If FileSize(libCryptocName.s) < 0
libCryptocName.s = "libcryptoc.dylib"; use default system path (last chance)
EndIf
EndIf
CompilerEndIf
hCryptoc = OpenLibrary(#PB_Any, libCryptocName.s)
If hCryptoc <> 0
cc_EncryptFile = GetFunction(hCryptoc, "EncryptFile")
cc_DecryptFile = GetFunction(hCryptoc, "DecryptFile")
cc_EncryptBytes = GetFunction(hCryptoc, "EncryptBytes")
cc_DecryptBytes = GetFunction(hCryptoc, "DecryptBytes")
cc_EncryptString = GetFunction(hCryptoc, "EncryptString")
cc_DecryptString = GetFunction(hCryptoc, "DecryptString")
cc_GenerateRandomHex = GetFunction(hCryptoc, "GenerateRandomHex")
cc_GetRemainderFromGEM = GetFunction(hCryptoc, "GetRemainderFromGEM")
cc_HexHashInit = GetFunction(hCryptoc, "HexHashInit")
cc_HexHashUpdate = GetFunction(hCryptoc, "HexHashUpdate")
cc_HexHashFinish = GetFunction(hCryptoc, "HexHashFinish")
cc_StringHexHash = GetFunction(hCryptoc, "StringHexHash")
cc_MemoryHexHash = GetFunction(hCryptoc, "MemoryHexHash")
cc_FileHexHash = GetFunction(hCryptoc, "FileHexHash")
cc_HexEncodeBytes = GetFunction(hCryptoc, "HexEncodeBytes")
cc_HexDecodeBytes = GetFunction(hCryptoc, "HexDecodeBytes")
cc_GenerateRSAKey = GetFunction(hCryptoc, "GenerateRSAKey")
cc_RSAEncrypt = GetFunction(hCryptoc, "RSAEncrypt")
cc_RSADecrypt = GetFunction(hCryptoc, "RSADecrypt")
cc_RSASign = GetFunction(hCryptoc, "RSASign")
cc_RSAVerify = GetFunction(hCryptoc, "RSAVerify")
ProcedureReturn #True
Else
Debug "Can not find " + libCryptocName.s
GlobalLog(#RF_LOG_CRIT, "Cant use " + libCryptocName.s + ". Please download the latest program-version and re-install.")
EndIf
ProcedureReturn #False
EndProcedure
Procedure.s GenerateRandomHEX(Bitlength.i, *resultCode)
; Generates a random key of Bitlength.i bits (double HEX-chars for the bytes)
Shared hCryptoc
If hCryptoc = 0
__InitCryptoDLL()
EndIf
Protected Bytes.i, keys.s, r.i
If Bitlength.i < 0 Or Bitlength.i > (1024*16)
GlobalLog(#RF_LOG_CRIT, "Unsupported bitlength: '" + Str(Bitlength.i) + "'")
PokeI(*resultCode, #RFEC_INVALID_PARAMETER)
ProcedureReturn ""
EndIf
If (Bitlength.i % 8) <> 0
GlobalLog(#RF_LOG_CRIT, "Unsupported bitlength must be divisible by 8: '" + Str(Bitlength.i) + "'")
PokeI(*resultCode, #RFEC_INVALID_PARAMETER)
ProcedureReturn ""
EndIf
Bytes.i = Bitlength.i / 8
keys.s = Space((Bytes.i * 2) + 1) ; for null termination
r.i = cc_GenerateRandomHex(Bytes.i, @keys)
Protected wKeys.s = PeekS(@keys, -1, #PB_UTF8)
wKeys = Trim(wKeys)
If r <> 0
GlobalLog(#RF_LOG_CRIT, "cc_GenerateRandomHex error code: " + GetErrorCodeMessage(r) + " [" + wKeys + "]")
PokeI(*resultCode, r)
ProcedureReturn ""
EndIf
PokeI(*resultCode, #RFEC_OK)
ProcedureReturn wKeys
EndProcedure
Sollte ein guter Start sein.