PC SOFT

GRUPOS DE DISCUSSÃO PROFISSIONAL
WINDEVWEBDEV e WINDEV Mobile

Inicio → WINDEV 25 → Como trabalhar com Chaves assimétricas?
Como trabalhar com Chaves assimétricas?
Iniciado por Boller, mai., 09 2023 12:17 PM - Sem resposta
Membro registado
3.659 mensagems
Popularité : +175 (223 votes)
Publicado em maio, 09 2023 - 12:17 PM
Eu achei isso mas tem que melhorar:

WLanguage, a linguagem de programação usada no ambiente de desenvolvimento da PC SOFT, não possui uma biblioteca nativa para criptografia assimétrica. No entanto, você pode usar uma biblioteca externa, como o OpenSSL, para criar chaves públicas e privadas.

Para usar o OpenSSL, instale a biblioteca e importe os arquivos .dll necessários para o projeto. Em seguida, use o código abaixo para criar um procedimento que cria pares de chaves públicas e privadas RSA:

plaintext
Copy code
// Importando a biblioteca OpenSSL
ImportC "libeay32.dll" stdcall
RSA_generate_key (nBits As int, e As int, callback As Pointer, cb_arg As Pointer) As Pointer
RSA_free (rsa As Pointer) As void
PEM_read_bio_RSAPrivateKey (bp As Pointer, x As Pointer, cb As Pointer, u As Pointer) As Pointer
PEM_read_bio_RSAPublicKey (bp As Pointer, x As Pointer, cb As Pointer, u As Pointer) As Pointer
PEM_write_bio_RSAPrivateKey (bp As Pointer, x As Pointer, enc As Pointer, kstr As Pointer, klen As int, cb As Pointer, u As Pointer) As int
PEM_write_bio_RSAPublicKey (bp As Pointer, x As Pointer) As int
BIO_new_fp (stream As int, close_flag As int) As Pointer
BIO_free (a As Pointer) As int
ERR_load_crypto_strings () As void
ERR_free_strings () As void
ERR_error_string (e As int, buf As Pointer) As Pointer
EndImport

PROCEDURE CriarChavesRSA(sChavePrivada, sChavePublica)
// Carregando strings de erro para a biblioteca OpenSSL
ERR_load_crypto_strings()

// Gerando um par de chaves RSA
nBits, e, callback, cb_arg: int
nBits = 2048
e = 65537
RSA* rsa = RSA_generate_key(nBits, e, NULL, NULL)

// Verificando se a chave RSA foi gerada corretamente
IF rsa = NULL
Error("Erro ao gerar a chave RSA")
RETURN
END

// Escrevendo as chaves em formato PEM
Pointer pBioPrivate, pBioPublic
int iResult

pBioPrivate = BIO_new_fp(fCreate("chave_privada.pem", foCreate), 1)
pBioPublic = BIO_new_fp(fCreate("chave_publica.pem", foCreate), 1)

iResult = PEM_write_bio_RSAPrivateKey(pBioPrivate, rsa, NULL, NULL, 0, NULL, NULL)
IF iResult <= 0
Error("Erro ao escrever a chave privada")
END

iResult = PEM_write_bio_RSAPublicKey(pBioPublic, rsa)
IF iResult <= 0
Error("Erro ao escrever a chave pública")
END

// Lendo as chaves do arquivo e armazenando-as em variáveis
sChavePrivada = fLoadText("chave_privada.pem")
sChavePublica = fLoadText("chave_publica.pem")

// Fechando e liberando recursos
BIO_free(pBioPrivate)
BIO_free(pBioPublic)
RSA_free(rsa)

// Liberando strings de erro da biblioteca OpenSSL
ERR_free_strings

Aceito sugestões!

Muito obrigado
Gratidão eterna

--
Adriano José Boller
______________________________________________
Consultor e Representante Oficial da
PcSoft no Brasil
+55 (41) 99949 1800
adrianoboller@gmail.com
skype: adrianoboller
http://wxinformatica.com.br/