Base64的编码(Encode)与解码(Decode)

推荐第一种 效率更高。
第一种:java8 新版本

@Test
    public void test1(){
        //现在Base64编码   import java.util.Base64;
        String s = "zhangjilin";
        String encodeToString = Base64.getEncoder().encodeToString(s.getBytes());
        System.out.println(encodeToString);
        //解码
        byte[] decode = Base64.getDecoder().decode(encodeToString);
        System.out.println(new String(decode));
    }

第二种:旧版本

@Test
    public void test1(){
        //之前 import org.apache.commons.codec.binary.Base64;
        String s = "zhangjilin";
        String s1 = Base64.encodeBase64String(s.getBytes());
        System.out.println(s1);
        System.out.println(new String(Base64.decodeBase64(s1)));
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于Base64编码解码涉及到字符编码和位运算等细节,实现起来比较复杂,因此我们可以使用现成的库函数来实现。以下是使用OpenSSL库函数实现Base64编码解码的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/bio.h> #include <openssl/evp.h> #define MAX_BUF_LEN 1024 int base64_encode(const char *src, int src_len, char *dst, int dst_len) { BIO *b64 = NULL; BIO *bio = NULL; BUF_MEM *bptr = NULL; int ret = -1; if (src == NULL || dst == NULL) { return -1; } b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) { return -1; } bio = BIO_new(BIO_s_mem()); if (bio == NULL) { BIO_free_all(b64); return -1; } bio = BIO_push(b64, bio); BIO_write(bio, src, src_len); BIO_flush(bio); BIO_get_mem_ptr(bio, &bptr); if (bptr == NULL) { BIO_free_all(bio); return -1; } if (bptr->length > dst_len) { BIO_free_all(bio); return -1; } memcpy(dst, bptr->data, bptr->length); dst[bptr->length] = '\0'; ret = bptr->length; BIO_free_all(bio); return ret; } int base64_decode(const char *src, int src_len, char *dst, int dst_len) { BIO *b64 = NULL; BIO *bio = NULL; int ret = -1; if (src == NULL || dst == NULL) { return -1; } b64 = BIO_new(BIO_f_base64()); if (b64 == NULL) { return -1; } bio = BIO_new_mem_buf(src, src_len); if (bio == NULL) { BIO_free_all(b64); return -1; } bio = BIO_push(b64, bio); ret = BIO_read(bio, dst, dst_len); BIO_free_all(bio); return ret; } int main() { char src[MAX_BUF_LEN] = "Hello, world!"; char dst[MAX_BUF_LEN] = {0}; char out[MAX_BUF_LEN] = {0}; int src_len = strlen(src); int dst_len = MAX_BUF_LEN; int out_len = MAX_BUF_LEN; // Base64编码 int len = base64_encode(src, src_len, dst, dst_len); if (len >= 0) { printf("Base64 encoded: %s\n", dst); } else { printf("Base64 encode failed.\n"); return -1; } // Base64解码 len = base64_decode(dst, len, out, out_len); if (len >= 0) { printf("Base64 decoded: %s\n", out); } else { printf("Base64 decode failed.\n"); return -1; } return 0; } ``` 在上面的示例程序中,我们使用了OpenSSL库中的BIO和EVP模块来实现Base64编码解码。其中,`base64_encode`函数接受原始数据和长度,输出Base64编码后的数据和长度。`base64_decode`函数接受Base64编码后的数据和长度,输出解码后的原始数据和长度。在主函数中,我们先对原始数据进行Base64编码,然后再将编码后的数据解码回原始数据。如果编码解码都成功,则输出编码后的数据和解码后的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值