一、Base64加密解密的簡介
Base64是一種基於64個可列印字元來表示二進位數據的表示方法,主要應用於電子郵件、網頁傳輸、音樂播放器等多媒體文件的傳輸和保存.由於Base64加密的結果字元都是字母和數字,所以可以通過任意渠道傳輸,不容易被屏蔽,相比其他加密演算法具有更好的通用性和可移植性,支持中文,不用考慮編碼問題。
二、Base64加密的原理
Base64是一種將二進位數據轉換成ASCII字元的編碼方式,基於Base64編碼的密文,是通過對原文進行可逆操作得到的。Base64編碼會將每3個8位位元組轉換成4個可列印字元,因此Base64編碼後的密文長度會比原文長度多出1/3左右。
三、Base64編碼的演算法實現
unsigned char *base64Encrypt(unsigned char *input, int length) { const char pictureTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//定義64位碼字元 unsigned char *current = input; unsigned char *output; int i = 0; output = (unsigned char*)malloc(length*4/3+4); while (length > 2)//每次操作三個位元組(即3*8=24位),操作後輸出4個(4*6=24位)位元組的可打字元。 { output[i++] = pictureTable[current[0] >> 2]; output[i++] = pictureTable[((current[0] & 0x03) <> 4)]; output[i++] = pictureTable[((current[1] & 0x0f) <> 6)]; output[i++] = pictureTable[current[2] & 0x3f]; current += 3; length -= 3; } if (length != 0) { output[i++] = pictureTable[current[0] >> 2]; if (length > 1) { output[i++] = pictureTable[((current[0] & 0x03) <> 4)]; output[i++] = pictureTable[(current[1] & 0x0f) << 2]; output[i++] = '='; } else { output[i++] = pictureTable[(current[0] & 0x03) << 4]; output[i++] = '='; output[i++] = '='; } } output[i] = '\0'; return output; }
Base64編碼演算法是基於ASCII字元的編碼表,通過一定規則去匹配ASCII碼中的字元,並將數字和字母編碼成相應的字元(如「0」對應編碼表的「Q」),最終將輸入字元串轉換成以可列印字元表示的新字元串。
四、Base64解碼的演算法實現
unsigned char *base64Decrypt(unsigned char *input, int length) { const char reverseTable[] = { // 解密表 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', }; unsigned char *current = input; int ch, i = 0, j = 0, k; unsigned char *output; output = (unsigned char*)malloc(length*3/4+4); while ((ch = *current++) != '\0' && length-- > 0) { if (ch == '=')//Base64編碼填充名為「=」 { break; } if (ch == ' ') { continue; } j++; k = ch; if (j % 4 == 1) { output[i] = k <> 4; output[i] = (k & 0x0f) <>2; output[i] = (k & 0x03) << 6; } else { output[i++] |= k; } } output[i] = '\0'; return output; }
Base64解碼演算法是將輸入的密文轉換回原始的二進位形式。解密過程中針對輸入的每4位進行分組,然後按照Base64編碼表進行反查表操作,找出對應的十進位值,最終還原出二進位數據。
五、Base64加密解密的使用實例
#include #include #include #include "encrypt.h" #define BUFFER_SIZE 1024 int main() { unsigned char bufferEncrypt[BUFFER_SIZE], bufferDecrypt[BUFFER_SIZE]; printf("Please input the string needed to be encrypted:\n");//輸入需要加密的字元串 fgets(bufferEncrypt, BUFFER_SIZE, stdin); bufferEncrypt[strlen(bufferEncrypt)-1] = '\0'; printf("Before Encrypted: %s\n", bufferEncrypt); unsigned char *ciphertext = base64Encrypt(bufferEncrypt, strlen(bufferEncrypt)); printf("After Encrypted: %s\n", ciphertext);//輸出加密後的字元串 printf("---------------------------------------------------------\n"); printf("Please input the string needed to be decrypted:\n");//輸入需要解密的字元串 fgets(bufferDecrypt, BUFFER_SIZE, stdin); bufferDecrypt[strlen(bufferDecrypt)-1] = '\0'; printf("Before Decrypted: %s\n", bufferDecrypt); unsigned char *plaintext = base64Decrypt(bufferDecrypt, strlen(bufferDecrypt)); printf("After Decrypted: %s\n", plaintext);//輸出解密後的字元串 return 0; }
六、Base64加密解密的相關安全問題
儘管Base64加密具有更好的通用性和可移植性,但它也存在一些安全性弱點。首先,由於Base64加密具有眾所周知的演算法,所以逆向破解一般十分容易。其次,由於Base64編碼後的字元串長度與原始的二進位數據長度相比增加了約1/3,從而也增加了數據傳輸的帶寬和時間開銷。
但如果對需要傳輸的實際數據進行再加密或哈希等安全處理操作並結合Base64加密技術綜合使用,可以增強數據的安全性,減少泄密的可能性,實現更加安全的數據傳輸。
原創文章,作者:CHPMT,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/370768.html