Android 使用密码算法加密解密显示错误

Android 使用密码算法加密解密显示错误,android,encryption,Android,Encryption,我正在创建对字符串进行加密和解密的android应用程序我正在使用密码算法该应用程序可以很好地进行加密,但当我尝试解密系统时,会显示一个错误: 01-10 09:50:54.364: E/AndroidRuntime(602): Caused by: javax.crypto.IllegalBlockSizeException: last block incomplete in decryption 有人能帮我纠正这个错误吗 代码 package com.devleb.encdecapp; 导

我正在创建对字符串进行加密和解密的android应用程序我正在使用密码算法该应用程序可以很好地进行加密,但当我尝试解密系统时,会显示一个错误:

01-10 09:50:54.364: E/AndroidRuntime(602): Caused by: javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
有人能帮我纠正这个错误吗

代码
package com.devleb.encdecapp;
导入java.io.UnsupportedEncodingException;
导入java.security.GeneralSecurityException;
导入java.security.SecureRandom;
导入javax.crypto.Cipher;
导入javax.crypto.KeyGenerator;
导入javax.crypto.SecretKey;
导入javax.crypto.SecretKeyFactory;
导入javax.crypto.spec.IvParameterSpec;
导入javax.crypto.spec.PBEKeySpec;
导入javax.crypto.spec.PBEParameterSpec;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Base64;
导入android.util.Log;
导入android.view.ContextMenu;
导入android.view.ContextMenu.ContextMenuInfo;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemSelectedListener;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Spinner;
公共类MainActivity扩展活动实现OnClickListener{
//布局的视图
旋转器旋转;
编辑文本编辑\u txt\u传递;
静态编辑文本编辑文本加密字符串;
编辑文本编辑_txt_原始;
静态编辑文本编辑字符串;
按钮btn_加密、btn_解密、btn_清除;
静态字符串cyphertext=“”;
静态字符串stredittxpass;
字符串strPaddingencryption;
静态整数迭代=1000;
私有静态最终字符串[]项={“填充密钥派生”,
“SHA1PRNG密钥派生”、“PBKDF2密钥派生”,
“PKCS#12键派生”};
私有静态最终字符串标记=MainActivity.class.getSimpleName();
私有静态最终字符串[]密码={“密码”,“加密”,
“密码”、“算法”、“qwerty”};
//将与键绑定以生成密码文本的mesage
private static String PlainText=“这是将被加密的文本”;
//将用于OnItemSelection方法的列表
私有静态最终整数填充_ENC_IDX=0;
专用静态最终整数SHA1PRNG_ENC_IDX=1;
私有静态最终int PBKDF2_ENC_IDX=2;
私有静态最终整数PKCS12_ENC_IDX=3;
字节[]salt={(字节)0x11,(字节)0x9B,(字节)0xC6,(字节)0xFE,
(字节)0x33,(字节)0x44,(字节)0x55,(字节)0x77};;
静态字节[]ivBytes={0,0,0,0,0,0,0};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//使用设置数组适配器和
//下拉资源
spin=(微调器)findviewbyd(R.id.spiner);
spin.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、,
整数arg2,长arg3){
//TODO自动生成的方法存根
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
});
ArrayAdapter aa=新的ArrayAdapter(此,
android.R.layout.simple\u微调器\u项目,项目);
aa.setDropDownViewResource(android.R.layout.simple\u dropdown\u item\u 1line);
旋转适配器(aa);
//微调器代码结束
edit_txt_pass=(EditText)findviewbyd(R.id.edittextpass);
edit_txt_enc_string=(EditText)findviewbyd(R.id.editTxtEncString);
edit_txt_raw=(EditText)findViewById(R.id.editTxtRawKey);
edit_txt_dec_string=(EditText)findViewById(R.id.editTxtDecString);
btn_encrypt=(按钮)findviewbyd(R.id.btnEncrypt);
btn_encrypt.setOnClickListener(此);
btn_decrypt=(按钮)findviewbyd(R.id.btnDecrypt);
btn_decrypt.setOnClickListener(这个);
btn_clear=(按钮)findViewById(R.id.btnClear);
btn_clear.setOnClickListener(此);
///用于将editText注册到关联菜单
registerForContextMenu(编辑文本);
}
//用于使用基64对明文进行加密
公共静态字符串toBase64(字节[]字节){
返回Base64.encodeToString(字节,Base64.NO_WRAP);
}
Base64中的公共静态字节[](字节[]字节){
//返回Base64.encodeToString(字节,Base64.NO_WRAP);
返回Base64.decode(字节,Base64.DEFAULT);
}
@凌驾
public void onCreateContextMenu(ContextMenu,视图v,
上下文菜单信息(menuInfo){
//TODO自动生成的方法存根
int-groupId=0;
添加(groupId,1,1,“密码”);
添加(groupId,2,2,“加密”);
添加(groupId,3,3,“密码”);
添加(groupId,4,4,“算法”);
添加(groupId,5,5,“qwerty”);
super.onCreateContextMenu(menu,v,menuInfo);
}
@凌驾
公共布尔值onContextItemSelected(MenuItem项){
//TODO自动生成的方法存根
返回getText(item);
//返回super.onContextItemSelected(项目);
}
私有布尔getText(MenuItem项){
//TODO自动生成的方法存根
int menuItemId=item.getItemId();
如果(menuItemId==1){
编辑_txt_pass.setText(“密码”);
}
如果(menuItemId==2){
编辑_txt_pass.setText(“加密”);
}
如果(menuItemId==3){
编辑_txt_pass.setText(“密码”);
}
如果(menuItemId==4){
编辑_txt_pass.setText(“算法”);
}
如果(menuItemId==5){
编辑_txt_pass.setText(“qwerty”);
}
   package com.devleb.encdecapp;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import android.app.Activity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends Activity implements OnClickListener {
    // views for the layout
    Spinner spin;
    EditText edit_txt_pass;
    static EditText edit_txt_enc_string;
    EditText edit_txt_raw;
    static EditText edit_txt_dec_string;
    Button btn_encrypt, btn_decrypt, btn_clear;

    static String cyphertext = "";
    static String STReditTxtPass;
    String strPaddingencryption;
    static int iterations = 1000;
    private static final String[] items = { "Padding Key derivation",
            "SHA1PRNG key derivation", "PBKDF2 key derivation",
            "PKCS#12 key derivation" };

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final String[] Passwords = { "password", "cryptography",
            "cipher", "algorithm", "qwerty" };

    // mesage that will be binded with the key to generate the cypher text
    private static String PlainText = "this is the text that will be encrypted";

    // the list that will be used for the OnItemSelection method
    private static final int PADDING_ENC_IDX = 0;
    private static final int SHA1PRNG_ENC_IDX = 1;
    private static final int PBKDF2_ENC_IDX = 2;
    private static final int PKCS12_ENC_IDX = 3;

    byte[] salt = { (byte) 0x11, (byte) 0x9B, (byte) 0xC6, (byte) 0xFE,
            (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x77 };;

    static byte[] ivBytes = {0,0,0,0,0,0,0,0};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // creation of the spinner with setting Array adapter and
        // DropDownresourse
        spin = (Spinner) findViewById(R.id.spiner);
        spin.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, items);
        aa.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spin.setAdapter(aa);
        // end of the spinner code

        edit_txt_pass = (EditText) findViewById(R.id.editTxtPass);
        edit_txt_enc_string = (EditText) findViewById(R.id.editTxtEncString);
        edit_txt_raw = (EditText) findViewById(R.id.editTxtRawKey);
        edit_txt_dec_string = (EditText) findViewById(R.id.editTxtDecString);

        btn_encrypt = (Button) findViewById(R.id.btnEncrypt);
        btn_encrypt.setOnClickListener(this);

        btn_decrypt = (Button) findViewById(R.id.btnDecrypt);
        btn_decrypt.setOnClickListener(this);

        btn_clear = (Button) findViewById(R.id.btnClear);
        btn_clear.setOnClickListener(this);

        // / for registering the editText to the Context Menu
        registerForContextMenu(edit_txt_pass);

    }

    // for the ciphering of the plainText using the base 64
    public static String toBase64(byte[] bytes) {
        return Base64.encodeToString(bytes, Base64.NO_WRAP);
    }

    public static byte[] fromBase64(byte[] bytes) {
        // return Base64.encodeToString(bytes, Base64.NO_WRAP);
        return Base64.decode(bytes, Base64.DEFAULT);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub

        int groupId = 0;
        menu.add(groupId, 1, 1, "password");
        menu.add(groupId, 2, 2, "cryptography");
        menu.add(groupId, 3, 3, "cipher");
        menu.add(groupId, 4, 4, "algorithm");
        menu.add(groupId, 5, 5, "qwerty");

        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        return getText(item);

        // return super.onContextItemSelected(item);
    }

    private boolean getText(MenuItem item) {
        // TODO Auto-generated method stub

        int menuItemId = item.getItemId();

        if (menuItemId == 1) {
            edit_txt_pass.setText("password");
        }
        if (menuItemId == 2) {
            edit_txt_pass.setText("cryptography");
        }
        if (menuItemId == 3) {
            edit_txt_pass.setText("cipher");
        }
        if (menuItemId == 4) {
            edit_txt_pass.setText("algorithm");
        }
        if (menuItemId == 5) {
            edit_txt_pass.setText("qwerty");
        }
        STReditTxtPass = edit_txt_pass.getText().toString();

        Log.w("the String of the Password text", STReditTxtPass);
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if (v == btn_encrypt) {

            encryptPadding(PlainText, salt);
        } else if (v == btn_clear) {
            edit_txt_enc_string.setText("");
        } else if (v == btn_decrypt) {
            decryptPadding(cyphertext, salt);
        }
    }

    public static String encryptPadding(String plaintext, byte[] salt) {
        try {
            KeyGenerator kg = KeyGenerator.getInstance("DES");
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

            SecretKey SKey = kg.generateKey();

            cipher.init(Cipher.ENCRYPT_MODE, SKey);

            byte[] cipherText = cipher.doFinal(PlainText.getBytes("UTF-8"));

            cyphertext = String.format("%s%s%s", toBase64(salt), "]",
                    toBase64(cipherText));
            edit_txt_enc_string.setText(cyphertext);
            return cyphertext;
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String decryptPadding(String ctext, byte[] salt) {
        try {
            KeyGenerator kg = KeyGenerator.getInstance("DES");
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

            SecretKey SKey = kg.generateKey();

            IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
            cipher.init(Cipher.DECRYPT_MODE, SKey, ivSpec);

            byte[] plaintxt = cipher.doFinal(cyphertext.getBytes("UTF-8"));

            PlainText = String.format("%s%s%s", fromBase64(salt), "]",
                    fromBase64(plaintxt));
            edit_txt_dec_string.setText(PlainText);
            return PlainText;
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

}
SecretKey SKey = kg.generateKey();
public String encryptPadding(String plaintext, byte[] salt) {
    try {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        cipher.init(Cipher.ENCRYPT_MODE, SKey);

        byte[] cipherText = cipher.doFinal(PlainText.getBytes("UTF-8"));

        cyphertext = String.format("%s%s%s", toBase64(salt), "]",
                toBase64(cipherText));
        edit_txt_enc_string.setText(cyphertext);
        return cyphertext;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

public String decryptPadding(String ctext, byte[] salt) {
    try {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.DECRYPT_MODE, SKey, ivSpec);

        byte[] plaintxt = cipher.doFinal(cyphertext.getBytes("UTF-8"));

        PlainText = String.format("%s%s%s", fromBase64(salt), "]",
                fromBase64(plaintxt));
        edit_txt_dec_string.setText(PlainText);
        return PlainText;
    } catch (GeneralSecurityException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
private SecretKey SKey;
KeyGenerator kg = KeyGenerator.getInstance("DES");
SKey = kg.generateKey();