Java:RSA中的Bad Padding异常

时间:2013-06-07 15:33:11

标签: java encryption rsa private-key public-key

我正在运行以下代码我得到Bad Padding Exception - Data must start with zero。有什么想法吗?

这是代码

public class test {
    public static void main(String[] args) throws NoSuchAlgorithmException,
           NoSuchProviderException, InvalidKeyException,
           SignatureException, NoSuchPaddingException,
           IllegalBlockSizeException, BadPaddingException {
        KeyPair k=Utilities.keys(2048);
        PublicKey public_key=Utilities.public_key(k);
        PrivateKey private_key=Utilities.private_key(k);
        String hex1=Lib.stringToHex("test1/test2/test3");
        byte[] plaintext=Lib.toByteArray(hex1);
        byte [] cipher=Utilities.Encrypt_RSA(plaintext, public_key); 
        System.out.println(cipher.length); 
          byte[] newArray = new byte[128];
          byte[] newArray2 = new byte[128];
         // System.arraycopy(cipher, 0, newArray, 0, 512);
          for (int i=0;i<cipher.length;i++){
                    if(i<cipher.length/2){

                        newArray[i]=cipher[i];
                    }
                    else if((i>cipher.length/2)&&(i<cipher.length)){
                        newArray2[i-128]=cipher[i];
                    }
           }

         // System.arraycopy(cipher, 512, newArray2, 0, 512);

        byte[] cipher2=Utilities.Encrypt_RSA_Pr(newArray, private_key);
        byte[] cipher3=Utilities.Encrypt_RSA_Pr(newArray2, private_key);

        System.out.println(cipher2.length+" cipher2");
        System.out.println(cipher3.length+" cipher3");
        byte [] plain1=Utilities.Decrypt_RSA_Pub(cipher2,public_key);
        byte [] plain2=Utilities.Decrypt_RSA_Pub(cipher3,public_key);
        System.out.println(plain1.length);
        System.out.println(plain2.length);
        byte[] finald=new byte[256];
        for(int i=0;i<256;i++){
            if(i<128){
                finald[i]=plain1[i];
            }
            else{
                finald[i]=plain2[i-128];
            }
        }

        for(int i=0;i<plain1.length;i++){
            if(i>=64){
                plain1[i]='\0';
            }
        }

        System.out.println(plain1.length);
      System.out.println(finald.length;
     byte[] plainfinal=Utilities.Decrypt_RSA(finald, private_key);

以下是我用于加密/解密的方法:

 public static byte[] Encrypt_RSA(byte[] plaintext,PublicKey key) throws
        InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException,NoSuchProviderException{
   Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    c.init(Cipher.ENCRYPT_MODE,key);
    byte [] ciphertext = c.doFinal(plaintext);
    return ciphertext;
}

public static byte[] Decrypt_RSA(byte []ciphertext,PrivateKey key) throws
       NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
       IllegalBlockSizeException, BadPaddingException,NoSuchProviderException{
    Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    c.init(Cipher.DECRYPT_MODE,key);
    byte [] plaintext=c.doFinal(ciphertext);
    return plaintext;
}

public static byte[] Encrypt_RSA_Pr(byte[] plaintext,PrivateKey key) throws
       NoSuchAlgorithmException, NoSuchPaddingException,
       IllegalBlockSizeException, BadPaddingException, InvalidKeyException{
    Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    c.init(Cipher.ENCRYPT_MODE,key);
    byte [] ciphertext = c.doFinal(plaintext);
    return ciphertext;
}

 public static byte[] Decrypt_RSA_Pub(byte []ciphertext,PublicKey key) throws
        NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        IllegalBlockSizeException,BadPaddingException,NoSuchProviderException{
    Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    c.init(Cipher.DECRYPT_MODE,key);
    byte [] plaintext=c.doFinal(ciphertext);
    return plaintext;
}

1 个答案:

答案 0 :(得分:0)

你的意思是将cipher分成两半,但你做错了。我怀疑这一行:

else if((i>cipher.length/2)&&(i<cipher.length)){

应该是

else if((i>=cipher.length/2)&&(i<cipher.length)){