有人知道如何解码和编码Base64中的字符串使用Base64吗?

我正在使用下面的代码,但它不起作用。

String source = "password";
byte[] byteArray = source.getBytes("UTF-16");
Base64 bs = new Base64();
//bs.encodeBytes(byteArray);
System.out.println(bs.encodeBytes(byteArray));
//bs.decode(bs.encodeBytes(byteArray));
System.out.println(bs.decode(bs.encodeBytes(byteArray)));
400051 次浏览

第一:

  • 选择编码。UTF-8通常是一个不错的选择;坚持对两边都有效的编码。很少使用UTF-8或UTF-16以外的代码。

发送端:

  • 将字符串编码为字节(例如text.getBytes(encodingName))
  • 使用Base64类将字节编码到base64
  • 传输base64

接收端:

  • 接收base64
  • 使用Base64类将base64解码为字节
  • 将字节解码为字符串(例如new String(bytes, encodingName))

比如:

// Sending side
byte[] data = text.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);


// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");

或者使用StandardCharsets:

// Sending side
byte[] data = text.getBytes(StandardCharsets.UTF_8);
String base64 = Base64.encodeToString(data, Base64.DEFAULT);


// Receiving side
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, StandardCharsets.UTF_8);

类似的

String source = "password";
byte[] byteArray;
try {
byteArray = source.getBytes("UTF-16");
System.out.println(new String(Base64.decode(Base64.encode(byteArray,
Base64.DEFAULT), Base64.DEFAULT)));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

对于在这里搜索如何解码用Base64.encodeBytes()编码的字符串的信息的其他人,这是我的解决方案:

// encode
String ps = "techPass";
String tmp = Base64.encodeBytes(ps.getBytes());


// decode
String ps2 = "dGVjaFBhC3M=";
byte[] tmp2 = Base64.decode(ps2);
String val2 = new String(tmp2, "UTF-8");

此外,我支持旧版本的Android,所以我使用Robert Harder的Base64库从http://iharder.net/base64

加密:

byte[] encrpt= text.getBytes("UTF-8");
String base64 = Base64.encodeToString(encrpt, Base64.DEFAULT);

解密:

byte[] decrypt= Base64.decode(base64, Base64.DEFAULT);
String text = new String(decrypt, "UTF-8");

基于之前的答案,我使用以下实用程序方法,以防有人想使用它。

    /**
* @param message the message to be encoded
*
* @return the enooded from of the message
*/
public static String toBase64(String message) {
byte[] data;
try {
data = message.getBytes("UTF-8");
String base64Sms = Base64.encodeToString(data, Base64.DEFAULT);
return base64Sms;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}


return null;
}


/**
* @param message the encoded message
*
* @return the decoded message
*/
public static String fromBase64(String message) {
byte[] data = Base64.decode(message, Base64.DEFAULT);
try {
return new String(data, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}


return null;
}

如果你正在使用芬兰湾的科特林,请像这样使用

对编码

val password = "Here Your String"
val data = password.toByteArray(charset("UTF-8"))
val base64 = Base64.encodeToString(data, Base64.DEFAULT)

为解码

val datasd = Base64.decode(base64, Base64.DEFAULT)
val text = String(datasd, charset("UTF-8"))

对于芬兰湾的科特林 mb,最好使用这个:

fun String.decode(): String {
return Base64.decode(this, Base64.DEFAULT).toString(charset("UTF-8"))
}


fun String.encode(): String {
return Base64.encodeToString(this.toByteArray(charset("UTF-8")), Base64.DEFAULT)
}

例子:

Log.d("LOGIN", "TEST")
Log.d("LOGIN", "TEST".encode())
Log.d("LOGIN", "TEST".encode().decode())

用于android API byte[]Base64String编码器

byte[] data=new byte[];
String Base64encodeString=android.util.Base64.encodeToString(data, android.util.Base64.DEFAULT);

适用于API级别26+

String encodedString = Base64.getEncoder().encodeToString(byteArray);
< p >参考: https://developer.android.com/reference/java/util/Base64.Encoder.html encodeToString (byte []) < / p >
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".BaseActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edt"
android:paddingTop="30dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1"
android:text="Encode"
android:textSize="20dp"
android:padding="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv2"


android:textSize="20dp"
android:padding="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv3"
android:text="decode"
android:textSize="20dp"
android:padding="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv4"
android:textSize="20dp"
android:padding="20dp"




/>
</LinearLayout>
package net.itempire.virtualapp;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class BaseActivity extends AppCompatActivity {
EditText editText;
TextView textView;
TextView textView2;
TextView textView3;
TextView textView4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
editText=(EditText)findViewById(R.id.edt);
textView=(TextView) findViewById(R.id.tv1);
textView2=(TextView) findViewById(R.id.tv2);
textView3=(TextView) findViewById(R.id.tv3);
textView4=(TextView) findViewById(R.id.tv4);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText(Base64.encodeToString(editText.getText().toString().getBytes(),Base64.DEFAULT));
}
});


textView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView4.setText(new String(Base64.decode(textView2.getText().toString(),Base64.DEFAULT)));


}
});




}
}

“java.util。类提供了以Base64格式编码和解码信息的功能。

如何获得Base64编码器?

Encoder encoder = Base64.getEncoder();

如何获得Base64解码器?

Decoder decoder = Base64.getDecoder();

如何对数据进行编码?

Encoder encoder = Base64.getEncoder();
String originalData = "java";
byte[] encodedBytes = encoder.encode(originalData.getBytes());

如何解码数据?

Decoder decoder = Base64.getDecoder();
byte[] decodedBytes = decoder.decode(encodedBytes);
String decodedStr = new String(decodedBytes);

你可以在链接中获得更多细节。

以上的许多答案对我来说都不适用,其中一些并没有以正确的方式处理异常。在这里我添加了一个完美的解决方案,对我来说是惊人的,对你也一样。

//base64 decode string
String s = "ewoic2VydmVyIjoic2cuenhjLmx1IiwKInNuaSI6InRlc3RpbmciLAoidXNlcm5hbWUiOiJ0ZXN0
ZXIiLAoicGFzc3dvcmQiOiJ0ZXN0ZXIiLAoicG9ydCI6IjQ0MyIKfQ==";
String val = a(s) ;
Toast.makeText(this, ""+val, Toast.LENGTH_SHORT).show();
    

public static String a(String str) {
try {
return new String(Base64.decode(str, 0), "UTF-8");
} catch (UnsupportedEncodingException | IllegalArgumentException unused) {
return "This is not a base64 data";
}
}

2021年在Kotlin回答。

编码:

val data: String = "Hello"
val dataByteArray: ByteArray = data.toByteArray()
val dataInBase64: String = Base64Utils.encode(dataByteArray)

解码:

val dataInBase64: String = "..."
val dataByteArray: ByteArray = Base64Utils.decode(dataInBase64)
val data: String = dataByteArray.toString()