编码和解码字符串Java

时间:2015-05-29 05:19:46

标签: java android string encode

我有这个代码,我试图在java中编码和解码和字符串,但是我得到了编译错误,这里是代码中带有错误的代码:

public static String encrypt(String plainText, SecretKey secretKey)
        throws Exception {
    byte[] plainTextByte = plainText.getBytes();
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] encryptedByte = cipher.doFinal(plainTextByte);
    Encoder encoder = Base64.getEncoder(); //ERROR "cannot resolve method"
    String encryptedText = encoder.encodeToString(encryptedByte);
    return encryptedText;
}

public static String decrypt(String encryptedText, SecretKey secretKey)
        throws Exception {
    Decoder decoder = Base64.getDecoder(); //ERROR "cannot resolve method"
    byte[] encryptedTextByte = (byte[]) decoder.decode(encryptedText);
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] decryptedByte = cipher.doFinal(encryptedTextByte);
    String decryptedText = new String(decryptedByte);
    return decryptedText;
}

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

检查您的导入并确保您正在导入:

Base64

我的IDE发现了几十个名为static class ViewHolder { TextView strAccountNo,strCustName, strCurInt,strLoanBal,strSavingBal; EditText strExtraInt,strSaving,strFine; CheckBox StatusCheckBox; } private StatementBean[] statementBean_arr; Activity context; EditText txtTotalCur,txtTotalExtra,txtTotalSaving,txtTotalFine,txtTotalCol; String ExtrInt="",Saving="",Fine=""; int totCurInt=0,oldExtra=0,newExtra=0,oldSav=0,newSav=0,newFin=0,oldFin=0,totExtra=0,totSav=0,totFin=0,totCol=0,intCur,intExtra,intSaving,intFine; public CustomAdapterListForCollection(Activity context,StatementBean[] statementBean_arr,EditText et1 ,EditText et2 ,EditText et3,EditText et4,EditText et5) { super(context, R.layout.collectionrow, statementBean_arr); this.context = context; this.statementBean_arr=statementBean_arr; txtTotalCur=et1; txtTotalExtra=et2; txtTotalSaving=et3; txtTotalFine=et4; txtTotalCol=et5; } @Override public View getView(int position, View rowView, ViewGroup parent) { final ViewHolder viewHolder; if(rowView==null) { LayoutInflater inflater = context.getLayoutInflater(); viewHolder=new ViewHolder(); rowView= inflater.inflate(R.layout.collectionrow, null, false); viewHolder.strAccountNo=(TextView)rowView.findViewById(R.id.th_acntno); viewHolder.strCustName = (TextView) rowView.findViewById(R.id.th_acntnm); viewHolder.strCurInt = (TextView) rowView.findViewById(R.id.th_acntcur); viewHolder.strExtraInt = (EditText) rowView.findViewById(R.id.th_acntextra); viewHolder.strSaving = (EditText) rowView.findViewById(R.id.th_acntsaving); viewHolder.strFine = (EditText) rowView.findViewById(R.id.th_fine); viewHolder.strLoanBal = (TextView) rowView.findViewById(R.id.th_loanBal); viewHolder.strSavingBal = (TextView) rowView.findViewById(R.id.th_saveBal); viewHolder.StatusCheckBox=(CheckBox)rowView.findViewById(R.id.statusCheckBox); viewHolder.strAccountNo.setText(statementBean_arr[position].getAccount_no()); viewHolder.strCustName.setText(statementBean_arr[position].getCust_name()); viewHolder.strCurInt.setText(""+statementBean_arr[position].getCur_int_amt()); try { totCurInt= totCurInt+Integer.valueOf(viewHolder.strCurInt.getText().toString()); } catch(Exception e) { e.printStackTrace(); } totCol=totCurInt; viewHolder.strExtraInt.setText(""+statementBean_arr[position].getExtra_int_amt()); viewHolder.strSaving.setText(""+statementBean_arr[position].getSaving()); viewHolder.strFine.setText(""+statementBean_arr[position].getFine()); viewHolder.strLoanBal.setText(statementBean_arr[position].getLoan_bal()); viewHolder.strSavingBal.setText(statementBean_arr[position].getSaving_bal()); viewHolder.StatusCheckBox.setChecked(statementBean_arr[position].isCheckFlag()); txtTotalCur.setText(""+totCurInt); txtTotalCol.setText(""+totCol); txtTotalExtra.setText(""+totExtra); txtTotalFine.setText(""+totFin); txtTotalSaving.setText(""+totSav); rowView.setTag(viewHolder); viewHolder.strExtraInt.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {} @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) { if(!(viewHolder.strExtraInt.getText().toString().equals(""))) { try { ExtrInt=viewHolder.strExtraInt.getText().toString().trim(); oldExtra= Integer.valueOf(viewHolder.strExtraInt.getText().toString()); Log.e("CustomAdapterListForCollection","totCol 1 "+totCol); totCol=totCol-oldExtra; Log.e("CustomAdapterListForCollection","totCol 2 "+totCol); totExtra=totExtra-oldExtra; txtTotalExtra.setText(""+totExtra); txtTotalCol.setText(""+totCol); Log.e("CustomAdapterListForCollection","totCol 3 "+totCol); Log.e("CustomAdapterListForCollection","oldExtra "+oldExtra); } catch(Exception e) { e.printStackTrace(); } } else { oldExtra=0; } } @Override public void afterTextChanged(Editable arg0) { if(!(viewHolder.strExtraInt.getText().toString().equals(""))) { try { ExtrInt =viewHolder.strExtraInt.getText().toString(); newExtra =Integer.parseInt(viewHolder.strExtraInt.getText().toString()); totExtra=totExtra+newExtra; totCol=totCol+newExtra; txtTotalExtra.setText(""+totExtra); txtTotalCol.setText(""+totCol); } catch(Exception e) { e.printStackTrace(); } } else { newExtra=0; } } }); viewHolder.strSaving.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {} @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) { if(!(viewHolder.strSaving.getText().toString().equals(""))) { try { oldSav=Integer.parseInt(viewHolder.strSaving.getText().toString()); totCol=totCol-oldSav; totSav=totSav-oldSav; txtTotalSaving.setText(""+totSav); txtTotalCol.setText(""+totCol); } catch(Exception e) { e.printStackTrace(); } } else { oldSav=0; } } @Override public void afterTextChanged(Editable arg0) { if(!(viewHolder.strSaving.getText().toString().equals(""))) { try { Saving=viewHolder.strSaving.getText().toString(); newSav=Integer.parseInt(viewHolder.strSaving.getText().toString()); totSav=totSav+newSav; totCol=totCol+newSav; txtTotalSaving.setText(""+totSav); txtTotalCol.setText(""+totCol); } catch(Exception e) { e.printStackTrace(); } } else { newSav=0; } } }); viewHolder.strFine.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {} @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { if(!(viewHolder.strFine.getText().toString().equals(""))) { try { oldFin=Integer.parseInt(viewHolder.strFine.getText().toString()); totCol=totCol-oldFin; totFin=totFin-oldFin; txtTotalFine.setText(""+totFin); txtTotalCol.setText(""+totCol); } catch(Exception e) { e.printStackTrace(); } } else { oldFin=0; } } @Override public void afterTextChanged(Editable arg0) { if(!(viewHolder.strFine.getText().toString().equals(""))) { try { Fine=viewHolder.strFine.getText().toString(); newFin=Integer.parseInt(viewHolder.strFine.getText().toString()); totFin=totFin+newFin; totCol=totCol+newFin; txtTotalFine.setText(""+totFin); txtTotalCol.setText(""+totCol); } catch(Exception e) { e.printStackTrace(); } } else { newFin=0; } } }); viewHolder.StatusCheckBox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int totalCurent,totalExtra,totalSaving,totalFine; boolean checked = ((CheckBox) v).isChecked(); Log.e("StatusCheckBox","StatusCheckBox onClickListener1"+checked); if(checked==false) { Log.e("StatusCheckBox","StatusCheckBox onClickListener2"+checked); try { if(!(viewHolder.strCurInt.getText().toString().equals(""))) intCur=Integer.parseInt(viewHolder.strCurInt.getText().toString()); else intCur=0; if(!(viewHolder.strExtraInt.getText().toString().equals(""))) intExtra= Integer.valueOf(viewHolder.strExtraInt.getText().toString()); else intExtra=0; if(!(viewHolder.strSaving.getText().toString().equals(""))) intSaving=Integer.parseInt(viewHolder.strSaving.getText().toString()); else intSaving=0; if(!(viewHolder.strFine.getText().toString().equals(""))) intFine=Integer.parseInt(viewHolder.strFine.getText().toString()); else intFine=0; if(!(txtTotalCur.getText().toString().equals(""))) totalCurent=Integer.parseInt(txtTotalCur.getText().toString()); else totalCurent=0; if(!(txtTotalExtra.getText().toString().equals(""))) totalExtra=Integer.parseInt(txtTotalExtra.getText().toString()); else totalExtra=0; if(!(txtTotalSaving.getText().toString().equals(""))) totalSaving=Integer.parseInt(txtTotalSaving.getText().toString()); else totalSaving=0; if(!(txtTotalFine.getText().toString().equals(""))) totalFine=Integer.parseInt(txtTotalFine.getText().toString()); else totalFine=0; totCurInt=totalCurent-intCur; totExtra=totalExtra-intExtra; totSav=totalSaving-intSaving; totFin=totalFine-intFine; totCol=totCurInt+totExtra+totSav+totFin; txtTotalCur.setText(""+totCurInt); txtTotalExtra.setText(""+totExtra); txtTotalSaving.setText(""+totSav); txtTotalFine.setText(""+totFin); txtTotalCol.setText(""+totCol); viewHolder.strCurInt.setText(""); viewHolder.strExtraInt.setText(""); viewHolder.strSaving.setText(""); viewHolder.strFine.setText(""); viewHolder.strCurInt.setEnabled(false); viewHolder.strExtraInt.setEnabled(false); viewHolder.strSaving.setEnabled(false); viewHolder.strFine.setEnabled(false); } catch(Exception e) { e.printStackTrace(); } } else { viewHolder.strCurInt.setEnabled(true); viewHolder.strExtraInt.setEnabled(true); viewHolder.strSaving.setEnabled(true); viewHolder.strFine.setEnabled(true); } } }); } else { viewHolder = (ViewHolder) rowView.getTag(); } return rowView; } 的类,所以即使名称匹配,您完全可能导入错误的类。

另请注意,{1.8}中添加了java.util.Base64类,因此,如果您使用旧版本,则无法使用该版本。

答案 1 :(得分:0)

您可以使用org.apache.commons.codec.binary.Base64

import org.apache.commons.codec.binary.Base64;
...
byte[] encodedBytes = Base64.encodeBase64(byteToEncode);

和解码

byte[] bytes = Base64.decodeBase64(base64String);