0

我正在运行以下代码,我得到一个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;
}
4

1 回答 1

0

你本想分cipher成两半,但你做错了。我怀疑这条线:

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

应该改为

else if((i>=cipher.length/2)&&(i<cipher.length)){
于 2013-06-08T10:56:47.093 回答