Does anyone know how to decode and encode a string in Base64 using the Base64. I am using the following code, but it\'s not working.
String source = \"passwo
something like
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();
}
Based on the previous answers I'm using the following utility methods in case anyone would like to use it.
/**
* @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;
}
First:
Transmitting end:
text.getBytes(encodingName)
)Base64
classReceiving end:
Base64
classnew String(bytes, encodingName)
)So something like:
// 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");
Or with 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);
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)));
}
});
}
}
To encrypt:
byte[] encrpt= text.getBytes("UTF-8");
String base64 = Base64.encodeToString(encrpt, Base64.DEFAULT);
To decrypt:
byte[] decrypt= Base64.decode(base64, Base64.DEFAULT);
String text = new String(decrypt, "UTF-8");
<?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>