歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Base64編碼解碼算法

Base64編碼解碼算法

日期:2017/3/1 9:18:27   编辑:Linux編程

Base64使用US-ASCII子集的64個字符,即大小寫的26個英文字母,0~9,+,/。編碼總是基於3個字符,每個字符用8位二進制表示,因此一共24位,再分為4四組,每組6位表示一個Base64的值。其值如下:

  1. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  2. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  3. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  4. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  5. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  6. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  7. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  8. '4', '5', '6', '7', '8', '9', '+', '/',

Base64值為0就是A,為27的就是b。如果被加密的字符串每3個一組,還剩1或2個字符,使用特殊字符"="補齊。例如編碼只有2個字符“me”,m的ascii是109,e的是101,用二進制表示分別是01101101、01100101,連接起來就是0110110101100101,再按6位分為一組:011011、010110、010100(不足6位補0),ascii分別是27、22、 20,即Base64值為bWU,Base64不足4字,用=補齊,因此bWU=就me的Base64值。

在 這裡 可以找到一個c語言的base32/base64的開源庫。以下是goahead中base64加密解密的實現源碼:

  1. static char_t map64[] = {
  2. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  3. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  4. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  5. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  6. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  7. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  8. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  9. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
  10. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  11. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  12. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  13. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  14. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  15. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  16. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  17. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  18. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  19. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  20. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  21. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  22. };
  23. static char_t alphabet64[] = {
  24. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  25. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  26. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  27. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  28. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  29. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  30. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  31. '4', '5', '6', '7', '8', '9', '+', '/',
  32. };
  33. /*********************************** Code *************************************/
  34. /*
  35. * Decode a buffer from "string" and into "outbuf"
  36. */
  37. int websDecode64(char_t *outbuf, char_t *string, int outlen)
  38. {
  39. unsigned long shiftbuf;
  40. char_t *cp, *op;
  41. int c, i, j, shift;
  42. op = outbuf;
  43. *op = '\0';
  44. cp = string;
  45. while (*cp && *cp != '=') {
  46. /*
  47. * Map 4 (6bit) input bytes and store in a single long (shiftbuf)
  48. */
  49. shiftbuf = 0;
  50. shift = 18;
  51. for (i = 0; i < 4 && *cp && *cp != '='; i++, cp++) {
  52. c = map64[*cp & 0xff];
  53. if (c == -1) {
  54. error(E_L, E_LOG, T("Bad string: %s at %c index %d"), string,
  55. c, i);
  56. return -1;
  57. }
  58. shiftbuf = shiftbuf | (c << shift);
  59. shift -= 6;
  60. }
  61. /*
  62. * Interpret as 3 normal 8 bit bytes (fill in reverse order).
  63. * Check for potential buffer overflow before filling.
  64. */
  65. --i;
  66. if ((op + i) >= &outbuf[outlen]) {
  67. gstrcpy(outbuf, T("String too big"));
  68. return -1;
  69. }
  70. for (j = 0; j < i; j++) {
  71. *op++ = (char_t) ((shiftbuf >> (8 * (2 - j))) & 0xff);
  72. }
  73. *op = '\0';
  74. }
  75. return 0;
  76. }
  77. /******************************************************************************/
  78. /*
  79. * Encode a buffer from "string" into "outbuf"
  80. */
  81. void websEncode64(char_t *outbuf, char_t *string, int outlen)
  82. {
  83. unsigned long shiftbuf;
  84. char_t *cp, *op;
  85. int x, i, j, shift;
  86. op = outbuf;
  87. *op = '\0';
  88. cp = string;
  89. while (*cp) {
  90. /*
  91. * Take three characters and create a 24 bit number in shiftbuf
  92. */
  93. shiftbuf = 0;
  94. for (j = 2; j >= 0 && *cp; j--, cp++) {
  95. shiftbuf |= ((*cp & 0xff) << (j * 8));
  96. }
  97. /*
  98. * Now convert shiftbuf to 4 base64 letters. The i,j magic calculates
  99. * how many letters need to be output.
  100. */
  101. shift = 18;
  102. for (i = ++j; i < 4 && op < &outbuf[outlen] ; i++) {
  103. x = (shiftbuf >> shift) & 0x3f;
  104. *op++ = alphabet64[(shiftbuf >> shift) & 0x3f];
  105. shift -= 6;
  106. }
  107. /*
  108. * Pad at the end with '='
  109. */
  110. while (j-- > 0) {
  111. *op++ = '=';
  112. }
  113. *op = '\0';
  114. }
  115. }

Linux提供了命令行方式的base64編碼和解碼。

格式:echo "str" | base64

將字符串str+換行 編碼為base64字符串輸出。

格式:echo -n "str" | base64

將字符串str編碼為base64字符串輸出。

格式:base64 file

從指定的文件file中讀取數據,編碼為base64字符串輸出。

格式:base64 -d

從標准輸入中讀取已經進行base64編碼的內容,解碼輸出。

格式:base64 -d -i

從標准輸入中讀取已經進行base64編碼的內容,解碼輸出。加上-i參數,忽略非字母表字符,比如換行符。

格式:echo "str" | base64 -d

將base64編碼的字符串str+換行 解碼輸出。

格式:echo -n "str" | base64 -d

將base64編碼的字符串str解碼輸出。

格式:base64 -d file

從指定的文件file中讀取base64編碼的內容,解碼輸出。

Copyright © Linux教程網 All Rights Reserved