@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetloadInfo info=new GetloadInfo(this);
info.add("1", "b", "55adad", "12345W78d1234a6aA");
InfoDomel sDomel=info.find();
if(sDomel!=null)
Toast.makeText(this,sDomel.getIsload()+" "+sDomel.getIssvae()+" "+sDomel.getStucode()+" "+sDomel.getStupwd(), 0).show();
Log.i("t460470591",sDomel.getIsload()+" "+sDomel.getIssvae()+" "+sDomel.getStucode()+" "+sDomel.getStupwd());
}
主程序
public class GetloadInfo {
private Context context;
private DBhelp dBhelp;
public GetloadInfo(Context context)
{
this.context=context;
dBhelp=new DBhelp(context);
}
public void add(String isload,String isSave,String stuCode,String stuPwd)
{
byte[] bytes=Base64.decode(stuPwd,Base64.NO_WRAP);//转成2进制
//byte[] bytes=Base64.decode(stuPwd,0);//转成2进制
SQLiteDatabase db=dBhelp.getWritableDatabase();
db.execSQL("delete from McsCP_loadInfo where 1=1");
db.execSQL("insert into McsCP_loadInfo(isLoad,isSave,stucode,stupwd) values(?,?,?,?)",new Object[]{isload,isSave,stuCode,bytes});
db.close();
}
public InfoDomel find()
{
InfoDomel demol=new InfoDomel();
SQLiteDatabase db=dBhelp.getReadableDatabase();
Cursor cursor=db.rawQuery("Select isLoad,isSave,stucode,stupwd from McsCP_loadInfo",null);
if(cursor.getCount()>0)
{
while(cursor.moveToNext())
{
demol.isload=cursor.getString(0);
demol.issvae=cursor.getString(1);
demol.stucode=cursor.getString(2);
byte[] tempPwd=cursor.getBlob(3);
demol.stupwd=Base64.encodeToString(tempPwd, Base64.NO_PADDING);//2进制转回String
}
cursor.close();
db.close();
return demol;
}
else{
cursor.close();
db.close();
return null;
}
}
我发现 那个BASE64只能转String的长度为16以下的,像我上面那个String 17位了,进去立马在public void add()第一句话报错,

------解决方案--------------------
// 加密传入的数据是byte类型的,并非使用decode方法将原始数据转二进制,String类型的数据 使用 str.getBytes()即可
String str = "Hello!";
// 在这里使用的是encode方式,返回的是byte类型加密数据,可使用new String转为String类型
String strBase64 = new String(Base64.encode(str.getBytes(), Base64.DEFAULT));
Log.i("Test", "encode >>>" + strBase64);
// 这里 encodeToString 则直接将返回String类型的加密数据
String enToStr = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
Log.i("Test", "encodeToString >>> " + enToStr);
// 对base64加密后的数据进行解密
Log.i("Test", "decode >>>" + new String(Base64.decode(strBase64.getBytes(), Base64.DEFAULT)));