# Comandos SI (OpenSSL e Linux em geral) ## Linux - Criar ficheiro vazio ``` bash touch ficheiro.txt ``` - Comparar 2 ficheiros ``` bash diff ficheiro1.txt ficheiro2.txt # (não dá output nenhum se os ficheiros forem iguais) ``` - Acesso ao manual de uma opração / aplicação ``` bash man aplicação # (ex: man openssl) ``` - "Pesquisa" num output no terminal ``` bash man openssl | grep md5 ``` - Ver conteudo de um ficheiro ``` bash cat ficheiro.txt ``` - Criar um ficheiro com uma string **sem** incluir os \n ``` bash echo -n "texto" > texto-limpo.txt ``` - Adicionar mais string a um ficheiro existente sem os \n ``` bash echo -n "texto" >> texto-limpo.txt ``` - Verificar ficheiro em HEX ``` bash hexdump -C ficheiro.txt ``` ## Python - Converter HEX em inteiro ``` python int("HEX", 16) ``` - Converter BIN em inteiro ``` python int ("BIN", 2) ``` - Codigo Python para verificar se um numero é gerador :::danger (EU SEI QUE É PARVO POR ISTO AQUI!!!) ``` python for i in range (1,22): print(5**i % 23) #para Num = 5 e Z = 23 ``` ::: ## Parametros gerais OpenSSL - **-K** especifica chave em HEX - **-k** especifica chave em ASCII (Não usado na UC) - **-out** ficheiro de saida - **-in** ficheiro de entrada - **-e** operação de cifragem - **-d** operação de decifragem ## ENC ### RC4 #### Cifragem ``` bash openssl enc -rc4 -e -K "Chave" -in plaintext.txt -out ciphertext.rc4 ``` #### Decifragem ``` bash openssl enc -rc4 -d -in text.rc4 - out text.txt -K "Chave" ``` ### AES128 #### Cifragem ``` bash openssl enc -aes128 -K "Chave" -in texto-limpo.txt -out textocifrado.aes -iv 0 ``` #### Decifragem ``` bash openssl enc -aes128 -d -K "Chave" -out texto-limpo2.txt -in textocifrado.aes -iv 0 ``` ### AES128-CBC #### Cifragem ``` bash openssl enc -aes-128-cbc -K "Chave" -in texto-limpo.txt -out criptograma.cbc -iv0 ``` #### Decifragem ``` bash openssl enc -aes-128-cbc -d -K "Chave" -in texto-limpo.txt -out criptograma.cbc -iv0 ``` ## DGST ### MD5 ``` bash openssl dgst -md5 texto-limpo.txt ``` ### SHA1 ``` bash openssl dgst -sha1 texto-limpo.txt ``` ### HMAC #### Manualmente - Calcular o SHA1 ``` bash openssl dgst -sha1 texto-limpo.txt ``` - Cifrar com o algoritmo correspondente ``` bash openssl enc -aes256 -K "Chave" -in texto-limpo.txt -out textocifrado.aes -iv 0 ``` #### Num só comando ``` bash openssl dgst -sha256 -hmac "Chave" texto-limpo.txt ``` ### RSA (Chaves publicas e privadas) #### Gerar par de chaves publica e privada ``` bash openssl genrsa -out sk-and-pk.pem 1024 ``` #### Extraír chave publica para outro ficheiro ``` bash openssl rsa -in sk-and-pk.pem -pubout -out pk.pem ``` #### Ver a chave pública no ecrã ``` bash openssl rsa -in pk.pem -pubin -text -noout ``` #### Gerar par de chaves pública e privada protegidos por chave simétrica ``` bash openssl genrsa -aes128 -passout pass:"senha" ``` #### Gerar um valor aleatório em HEX com 128 bits (16 bytes) para um ficheiro ``` bash openssl rand -hex 16 > secret.key ``` #### Cifragem ``` bash openssl rsautl -in secret.key -out sig.enc -piblin -inkey pk-nome-aluno.pem -encrypt ``` #### Decifragem ``` bash openssl rsautl -decrypt -in sig.enc -out secret2.key -inkey sk-and-pk.pem ``` ### Gerar número primo: ``` bash openssl dhparam -text -C 512 ```