Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解密des算法方法错误填充_Java_Encryption - Fatal编程技术网

Java 解密des算法方法错误填充

Java 解密des算法方法错误填充,java,encryption,Java,Encryption,我尝试使用des算法进行加密和解密。对于enryption,我没有问题,成功地将明文转换为字节数组。但是我无法将密文解密为普通明文。我认为我的解密方法有一些问题。这是我的代码 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; impo

我尝试使用des算法进行加密和解密。对于enryption,我没有问题,成功地将明文转换为字节数组。但是我无法将密文解密为普通明文。我认为我的解密方法有一些问题。这是我的代码

import java.io.ByteArrayInputStream;    
import java.io.ByteArrayOutputStream;    
import javax.crypto.Cipher;    
import javax.crypto.CipherInputStream;    
import javax.crypto.CipherOutputStream;    
import javax.crypto.spec.IvParameterSpec;    
import javax.crypto.spec.SecretKeySpec;    
import javax.xml.bind.DatatypeConverter;

public class FileEncryption {

    //Initial Vector
    public static final byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };      

    //EncryptAndDecrypt String -> Input : PlainText + Return : CipherText+DecipherText
    public static String encryptString(String src) throws Exception
    {
        String dst="";
        //Not Input!
        if(src == null || src.length()==0)
            return "";

        //Encryption Setting
        byte[] k="Multimediaproces".getBytes();
        SecretKeySpec Key = new SecretKeySpec(k,"AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        encryptCipher.init(Cipher.ENCRYPT_MODE,Key,ivspec);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CipherOutputStream cout = new CipherOutputStream(baos,encryptCipher);
        cout.write(src.getBytes());
        cout.flush();               //ByteOutputStream -> Write Encryption Text
        cout.close();           
        dst = DatatypeConverter.printHexBinary(baos.toByteArray());
        return dst;
    }   
    //String src -> EncryptedData
    public static String decryptString(String src) throws Exception 
    {
        //src value is Encrypted Value!
        //So, src value -> Not Byte!
        //String dst="";
        byte[] encryptedBytes = src.getBytes();         
        //Not Input!
        if(src == null || src.length()==0)
            return "";          
        //Decryption Setting
        IvParameterSpec ivspec = new IvParameterSpec(iv);
        byte[] k="Multimediaproces".getBytes();
        SecretKeySpec Key = new SecretKeySpec(k,"AES");
        Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        decryptCipher.init(Cipher.DECRYPT_MODE,Key,ivspec); 

        //ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayInputStream bais = new ByteArrayInputStream(encryptedBytes);
        CipherInputStream cin = new CipherInputStream(bais,decryptCipher);
        //CipherOutputStream cout = new CipherOutputStream(baos,decryptCipher);
        //cout.write(src.getBytes());
        byte[] buf = new byte[1024];
        int read;
        while((read=cin.read(buf))>=0)  //reading encrypted data!
        {
            cin.read(buf,0,read);     //writing decrypted data!
            //read = cin.read(buf);
        }
        // closing streams
        //baos.write(decryptCipher.doFinal());
        //return baos.toString();
        //cin.close(); 
        return bais.toString();
    }
}
运行代码时声明的错误 在encryptString方法中,您正在将字节转换为十六进制字符串:

dst = DatatypeConverter.printHexBinary(baos.toByteArray());
因此,为了解密,必须将此十六进制字符串转换回字节:

// Instead of this: byte[] encryptedBytes = src.getBytes();
// Do this:
byte[] encryptedBytes = DatatypeConverter.parseHexBinary(src);
返回ByteArrayInputStream的toString表示,但实际上需要的是buf变量,因为它将包含解密的数据。 因此,decryptString方法应返回:

// Instead of this: return bais.toString();
// do this:
return new String(buf);
注意:当您将字符串转换为字节,并将字节转换回字符串时,您可能会遇到一些编码问题。为了避免这种情况,建议对两种转换始终使用相同的编码。例如,在本例中,假设您想要使用UTF-8,您可以执行以下操作

在加密字符串中:

在字符串中:


这适用于任何编码,只要两种方法使用相同的编码。

不要在新代码中使用DES,它不安全,已被AES取代。另外,不要使用静态IV,使用随机IV并将其前置到加密数据,以便在解密期间使用。使用getBytes但不指定编码,因此可能会获得UTF-16或UTF-8,请指定编码。而是使用getBytesUTF-8。那么我应该如何修复它呢?你能给我一些代码吗这里有很多代码示例。由于未提供加密数据,因此几乎无法进行检查。好的,谢谢您的帮助。我将尝试修复并找到解决方案。谢谢兄弟:是的,我终于明白了!非常感谢。。谢谢你,伙计
cout.write(src.getBytes("UTF-8")); // instead of cout.write(src.getBytes());
return new String(buf, "UTF-8"); // instead of return new String(buf);