Base64编码的Java连接code和德code字符串 [英] Base64 Java encode and decode a string

查看:222
本文介绍了Base64编码的Java连接code和德code字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要带code字符串转换成Base64和通过套接字和DE code将其传送回来。但解码之后它给了不同的答案。以下是我的code和结果为77 + 9x6s =

I want to encode a string into base64 and transfer it through a socket and decode it back. But after decoding it gives different answer. Following is my code and result is "77+9x6s="

import javax.xml.bind.DatatypeConverter;

    public class f{

       public static void main(String a[]){

          String str = new String(DatatypeConverter.parseBase64Binary("user:123"));
          String res = DatatypeConverter.printBase64Binary(str.getBytes());
          System.out.println(res);
       }
    }

有关如何实现这个任何想法?

Any idea about how to implement this?

感谢你......

推荐答案

您可以用下面的办法,

// encode data on your side using BASE64
byte[]   bytesEncoded = Base64.encodeBase64(str .getBytes());
System.out.println("ecncoded value is " + new String(bytesEncoded ));

// Decode data on other side, by processing encoded data
byte[] valueDecoded= Base64.decodeBase64(bytesEncoded );
System.out.println("Decoded value is " + new String(valueDecoded));

希望这回答了你的疑问。

Hope this answers your doubt.

这篇关于Base64编码的Java连接code和德code字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆