logcat에 암호화하고 표시하는 주요 기능
aes = new ofxLibcrypto();
// start
unsigned char *key = (unsigned char *)"qwertyuiqwertyuiqwertyuiqwertyui";
unsigned char *iv = (unsigned char *)"qwertyuiqwertyui";
unsigned char *plaintext = (unsigned char *)"The quick brown fox jumps over the lazy dog";
unsigned char *ciphertext;
unsigned char decryptedtext[128];
int ciphertext_len, decryptedtext_len;
ciphertext_len = aes->encrypt(plaintext, strlen((char*)plaintext), key, iv, ciphertext);
ofLogNotice("IDB") << "Encrpyted: " << ciphertext;
decryptedtext_len = aes->decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);
decryptedtext[decryptedtext_len] = (unsigned char) "\0";
ofLogNotice("IDB") << "Decrypted: " << decryptedtext;
// end
프로그램은 성공적으로 암호화되지만 암호문을 표시하려고하면 첫 번째 문자 만 표시됩니다. 루프에서 문자별로 문자를 표시하려고하면 모든 문자가 암호화 된 것으로 표시됩니다. 나는 그것을 고치기 위해 많은 코드를 조사했지만 그들은 이런 식으로 그것을 고칠 수 없습니다. 암호화 및 복호화 기능이 잘 작동하므로 첨부하지 않았지만 필요한 경우 첨부하겠습니다.
도움을 줄 사람은 이미 감사합니다.
암호화 된 텍스트를 표시 할 수 없습니다
암호화 된 텍스트 ( ciphertext
)는 이진 Blob입니다. A
또는 같은 일부 인쇄 가능한 문자가있을 수 ?
있지만 ASCII 값이 1, 2, 3 또는 0 인 문자와 같이 인쇄 할 수없는 문자 도있을 수 있습니다.
이진 Blob에서 다음 문자 시퀀스를 고려하십시오.
unsigned char data[] = {0x41, 0x00, 0x42, 0x43};
// 'A', '\0', 'B', 'C'
data
문자 포함 'A'
, '\0'
(널 바이트), 'B'
, 'C'
. 인쇄를 시도 하면 다음 문자가 널 바이트이고 해당 문자가 발견되면 즉시 인쇄를 중지하기 때문에 data
만 표시 A
됩니다.
그렇다면 이진 Blob을 어떻게 표시합니까? 이를 수행하는 일반적인 방법은 바이너리 데이터를 하나 base16
또는 다른 기본 으로 인코딩하는 것입니다 .
다음은 base16으로 데이터를 인코딩하는 간단한 함수입니다.
template <typename T>
std::string toBase16(const T itbegin, const T itend)
{
std::string rv;
static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
rv.reserve(std::distance(itbegin, itend) * 2);
for (T it = itbegin; it < itend; ++it) {
unsigned char val = (unsigned char)(*it);
rv.push_back(hexmap[val >> 4]);
rv.push_back(hexmap[val & 15]);
}
return rv;
}
int ciphertext_len, decryptedtext_len;
ciphertext_len = aes->encrypt(plaintext, strlen((char*)plaintext), key, iv, ciphertext);
ofLogNotice("IDB") << "Encrpyted: " << toBase16(ciphertext, ciphertext + ciphertext_len);
또 다른 방법은 모든 바이트를 캐스트 int
한 다음 10 진수 값을 표시하는 것입니다.
unsigned char data[] = {0x41, 0x00, 0x42, 0x43};
// 'A', '\0', 'B', 'C'
for (int i = 0; i < 4; ++i)
{
cout << static_cast<int>(data[i]) << ' ';
}
//Output: 65 0 66 67
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다