如何解码C中的字符串,在Java中编码为Base64? 我有字符串,在Java中编码为Base64。 现在我需要在C中解码这个字符串。我该怎么做?
我在C中使用了一些Base64库,但结果是一样的。 解码数据与Java中的原始数据不同。 当我使用可读的ASCII字符作为输入数据时,它会起作用,但我需要-127 +128范围内的所有数据。
我的编码数据的的短样品 “flBJQ1RVUkV8AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAAA / wAAAP8AAAD / AAA“ < / p>
这是我用于解码Base64的代码的一部分:
// Make sure we have enough space to add '\0' character at end.
dec = (unsigned char *) b64_realloc(dec, size + 1);
if (dec != NULL){
dec[size] = '\0';
} else {
return NULL;
}
// Return back the size of decoded string if demanded.
if (decsize != NULL) {
*decsize = size;
}
return dec;
}
unsigned char *b64_decode_ex3 (const char *src, size_t len, size_t *decsize) {
int i = 0;
int j = 0;
int l = 0;
size_t size = 0;
unsigned char *dec = NULL;
unsigned char buf[3];
unsigned char tmp[4];
// alloc
dec = (unsigned char *) b64_malloc(1);
if (NULL == dec) { return NULL; }
// parse until end of source
while (len--) {
// break if char is `=' or not base64 char
if ('=' == src[j]) { break; }
//if (!(isalnum(src[j]) || '+' == src[j] || '/' == src[j])) { break; }
// read up to 4 bytes at a time into `tmp'
tmp[i++] = src[j++];
// if 4 bytes read then decode into `buf'
if (4 == i) {
// translate values in `tmp' from table
for (i = 0; i < 4; ++i) {
// find translation char in `b64_table'
for (l = 0; l < 64; ++l) {
if (tmp[i] == b64_table[l]) {
tmp[i] = l;
break;
}
}
}
// decode
buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];
// write decoded buffer to `dec'
dec = (unsigned char *) b64_realloc(dec, size + 3);
if (dec != NULL){
for (i = 0; i < 3; ++i) {
dec[size++] = buf[i];
}
} else {
return NULL;
}
// reset
i = 0;
}
}
// remainder
if (i > 0) {
// fill `tmp' with `\0' at most 4 times
for (j = i; j < 4; ++j) {
tmp[j] = '\0';
}
// translate remainder
for (j = 0; j < 4; ++j) {
// find translation char in `b64_table'
for (l = 0; l < 64; ++l) {
if (tmp[j] == b64_table[l]) {
tmp[j] = l;
break;
}
}
}
// decode remainder
buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4);
buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2);
buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3];
// write remainer decoded buffer to `dec'
dec = (unsigned char *) b64_realloc(dec, size + (i - 1));
if (dec != NULL){
for (j = 0; (j < i - 1); ++j) {
dec[size++] = buf[j];
}
} else {
return NULL;
}
}
// Make sure we have enough space to add '\0' character at end.
dec = (unsigned char *) b64_realloc(dec, size + 1);
if (dec != NULL){
dec[size] = '\0';
} else {
return NULL;
}
// Return back the size of decoded string if demanded.
if (decsize != NULL) {
*decsize = size;
}
return dec;
}