Java对字符串进行编码和解码[重复]

时间:2022-12-02 23:44:49

This question already has an answer here:

这个问题已经有了答案:

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="

我想把一个字符串编码成base64,然后通过一个套接字将它转换回来。但解码后给出了不同的答案。下面是我的代码,结果是“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?

你知道怎么实现这个吗?

Thank You...

谢谢你!

6 个解决方案

#1


178  

You can use following approach:

你可采用以下方法:

// Encode data on your side using BASE64
byte[] bytesEncoded = Base64.encodeBase64(str.getBytes());
System.out.println("encoded 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.

希望这能回答你的疑问。

#2


100  

Java 8 now supports BASE64 Encoding and Decoding. You can use the following classes: java.util.Base64, java.util.Base64.Encoder and java.util.Base64.Decoder.

Java 8现在支持BASE64编码和解码。您可以使用以下类:java.util。java.util.Base64 Base64。编码器和java.util.Base64.Decoder。

Example usage:

使用示例:

// encode with padding
String encoded = Base64.getEncoder().encodeToString(someByteArray);

// encode without padding
String encoded = Base64.getEncoder().withoutPadding().encodeToString(someByteArray);

// decode a String
byte [] barr = Base64.getDecoder().decode(encoded); 

#3


43  

The accepted answer uses the Apache Commons package, but this is how I did it using javax.xml.bind.DatatypeConverter - this works from at least Java 6 on:

公认的答案使用Apache Commons包,但这是我使用javax.xml.bind的方式。DatatypeConverter——这至少可以从Java 6中得到:

import java.io.UnsupportedEncodingException;    
import javax.xml.bind.DatatypeConverter;

public class EncodeString64 {

    public static void main(String[] args) throws UnsupportedEncodingException {

        String str = "77+9x6s=";
        // encode data using BASE64
        String encoded = DatatypeConverter.printBase64Binary(str.getBytes());
        System.out.println("encoded value is \t" + encoded);

        // Decode data 
        String decoded = new String(DatatypeConverter.parseBase64Binary(encoded));
        System.out.println("decoded value is \t" + decoded);

        System.out.println("original value is \t" + str);

    }

}

The better way would be to try/catch the encoding/decoding steps but hopefully you get the idea.

更好的方法是尝试/捕获编码/解码步骤,但希望您能理解。

#4


21  

For Spring Users , Spring Security has a Base64 class in the org.springframework.security.crypto.codec package that can also be used for encoding and decoding of Base64. Ex.

对于Spring用户,Spring Security在org.springframework.security.crypto中有一个Base64类。也可用于Base64编码和解码的编解码包。前女友。

 public static String base64Encode(String token) {
    byte[] encodedBytes = Base64.encode(token.getBytes());
    return new String(encodedBytes, Charset.forName("UTF-8"));
}


public static String base64Decode(String token) {
    byte[] decodedBytes = Base64.decode(token.getBytes());
    return new String(decodedBytes, Charset.forName("UTF-8"));
}

#5


15  

The following is a good solution -

下面是一个很好的解决方案

import android.util.Base64;

String converted = Base64.encodeToString(toConvert.toString().getBytes(), Base64.DEFAULT);

String stringFromBase = new String(Base64.decode(converted, Base64.DEFAULT));

That's it. A single line encoding and decoding.

就是这样。单行编码和解码。

#6


7  

import javax.xml.bind.DatatypeConverter;

public class f{

   public static void main(String a[]){

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

#1


178  

You can use following approach:

你可采用以下方法:

// Encode data on your side using BASE64
byte[] bytesEncoded = Base64.encodeBase64(str.getBytes());
System.out.println("encoded 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.

希望这能回答你的疑问。

#2


100  

Java 8 now supports BASE64 Encoding and Decoding. You can use the following classes: java.util.Base64, java.util.Base64.Encoder and java.util.Base64.Decoder.

Java 8现在支持BASE64编码和解码。您可以使用以下类:java.util。java.util.Base64 Base64。编码器和java.util.Base64.Decoder。

Example usage:

使用示例:

// encode with padding
String encoded = Base64.getEncoder().encodeToString(someByteArray);

// encode without padding
String encoded = Base64.getEncoder().withoutPadding().encodeToString(someByteArray);

// decode a String
byte [] barr = Base64.getDecoder().decode(encoded); 

#3


43  

The accepted answer uses the Apache Commons package, but this is how I did it using javax.xml.bind.DatatypeConverter - this works from at least Java 6 on:

公认的答案使用Apache Commons包,但这是我使用javax.xml.bind的方式。DatatypeConverter——这至少可以从Java 6中得到:

import java.io.UnsupportedEncodingException;    
import javax.xml.bind.DatatypeConverter;

public class EncodeString64 {

    public static void main(String[] args) throws UnsupportedEncodingException {

        String str = "77+9x6s=";
        // encode data using BASE64
        String encoded = DatatypeConverter.printBase64Binary(str.getBytes());
        System.out.println("encoded value is \t" + encoded);

        // Decode data 
        String decoded = new String(DatatypeConverter.parseBase64Binary(encoded));
        System.out.println("decoded value is \t" + decoded);

        System.out.println("original value is \t" + str);

    }

}

The better way would be to try/catch the encoding/decoding steps but hopefully you get the idea.

更好的方法是尝试/捕获编码/解码步骤,但希望您能理解。

#4


21  

For Spring Users , Spring Security has a Base64 class in the org.springframework.security.crypto.codec package that can also be used for encoding and decoding of Base64. Ex.

对于Spring用户,Spring Security在org.springframework.security.crypto中有一个Base64类。也可用于Base64编码和解码的编解码包。前女友。

 public static String base64Encode(String token) {
    byte[] encodedBytes = Base64.encode(token.getBytes());
    return new String(encodedBytes, Charset.forName("UTF-8"));
}


public static String base64Decode(String token) {
    byte[] decodedBytes = Base64.decode(token.getBytes());
    return new String(decodedBytes, Charset.forName("UTF-8"));
}

#5


15  

The following is a good solution -

下面是一个很好的解决方案

import android.util.Base64;

String converted = Base64.encodeToString(toConvert.toString().getBytes(), Base64.DEFAULT);

String stringFromBase = new String(Base64.decode(converted, Base64.DEFAULT));

That's it. A single line encoding and decoding.

就是这样。单行编码和解码。

#6


7  

import javax.xml.bind.DatatypeConverter;

public class f{

   public static void main(String a[]){

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