2017-12-11 4 views
0

저는 지금 당분간이 문제에 봉착했습니다.C에서 OpenSSL RSA 함수를 사용하여 메시지를 암호 해독 할 때

bad decrypt 
140380701197976:error:0606506D:digital envelope 
routines:EVP_DecryptFinal_ex:wrong final block length:evp_enc.c:518: 

때때로, 나는 이러한 오류를 참조하십시오 :

RSA operation error 
139986632922776:error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error:rsa_pk1.c:273: 
139986632922776:error:04065072:rsa 
routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed:rsa_eay.c:602: 
Error reading password from BIO 
Error getting password 

을 그리고 때때로, 나는 전혀 오류가 표시되지 않습니다 내 프로그램을 실행하면, 때로는 이러한 오류를 참조하십시오! Chrome 하드웨어 용 우분투 (Ubuntu)의 맛 인 GalliumOS를 사용하고 있습니다.

무엇이 누락 되었습니까? 나는 온통 보았고, 나는 관련성이있는 것을 찾을 수 없다. 참고로 아래 코드를 첨부했습니다. 나는 내가 마지막 두 명령 중 하나에 오류를 좁혔다 고 생각하지만, 나는 긍정적이지 않다.

#include <cstdlib> 
#include <string> 
using namespace std; 

int main() 
{ 
    string bob_keys, alice_plaintext, alice_encrypted, bob_decrypted; 

    // ----- USER INPUT ----- 
    // TODO: switch to user input 
    bob_keys = "bob_keys.pem"; 
    bob_decrypted = "bob_decrypted.txt"; 
    alice_encrypted = "alice_encrypted.txt"; 
    alice_plaintext = "alice_plaintext.txt"; 

    // ----- CONFIDENTIALITY: MESSAGE ENCRYPTION ----- 
    // generate session key 
    system("openssl rand -base64 64 -out key.bin"); 

    // encrypt message using session key 
    system(("openssl enc -aes-128-cbc -salt -in " + alice_plaintext 
     + " -out alice_plaintext.txt.enc -pass file:./key.bin").c_str()); 

    // encrypt session key using Bob's public key 
    system(("openssl rsautl -encrypt -inkey " + bob_keys 
     + " -pubin -in key.bin -out key.bin.enc").c_str()); 

    // write encrypted message and encrypted session key to file 
    system(("cat alice_plaintext.txt.enc > " + alice_encrypted).c_str()); 
    system(("echo >> " + alice_encrypted).c_str()); 
    system(("cat key.bin.enc >> " + alice_encrypted).c_str()); 

    // ----- CONFIDENTIALITY: MESSAGE DECRYPTION ----- 
    // get encrypted message and encrypted session key from file (and remove newlines) 
    system(("head -1 " + alice_encrypted + " > message.bin.enc").c_str());  
    system("tr -d '\n' <message.bin.enc> temp.bin && mv temp.bin message.bin.enc"); 
    system(("tail -1 " + alice_encrypted + " > key.bin.enc").c_str());  

    // decrypt the key using Bob's private key 
    system(("openssl rsautl -decrypt -inkey " + bob_keys 
     + " -in key.bin.enc -out key.bin").c_str()); 

    // decrypt the message using the decrypted key 
    system(("openssl enc -d -aes-128-cbc -in message.bin.enc -out " 
     + bob_decrypted + " -pass file:./key.bin").c_str()); 

    return 0; 
} 
+0

귀하의 코드는 '시스템'에 대한 호출 일뿐입니다. 대신 셸 스크립트를 사용하지 않는 이유는 무엇입니까? – dbush

+0

그래, 그게 내가 생각한거야. 이유가 뭐든간에, 강사들은 우리가 C++ 파일을 제출하기를 원하지만, 시스템 호출을하는 것은 완전히 냉담하다. 나는 그것도 동의하지 않지만 작동한다. – hockeysaint

답변

0

이진 파일에는 '머리글'과 '꼬리말'이 사용됩니다. 그 전에는 바이너리를 텍스트로 해석 할 수있는 많은 함정을 피할 수 있었지만 여기에서는 결국 실패합니다. 여러분의 암호문에 개행 문자가 포함되어있을 가능성이 있으며, 암호문이 발견되면 암호문이 올바르지 않습니다. 당신은 바이너리 만 사용하거나 64 암호문을 기본으로 만들어야합니다.

C/C++의 사용을 우회하는 방식으로 처리하는 것이 좋지만 마지막으로 웃기는 부분이 있습니다. 키 대신 암호를 사용하고 있으므로 결과는 AES 키를 사용하는 것과 동일하지 않습니다. 똑똑하고 대신 C 라이브러리를 사용하십시오. 또는 C++ 이상을 원한다면 Crypto ++ 또는 Bothan을 사용하십시오. 그리고 everything에서 당신의 탈출구를 해킹하는 대신 실제로 프로그램하는 법을 배웁니다. 결국 - 결국 해킹으로 인해 가장 고통받는 사람이됩니다.