数组只写入,从不读取

时间:2017-03-20 16:45:46

标签: java encryption

我尝试测试此代码以进行加密和解密。使用dst = new String(baos.toByteArray()); return dst; 我无法解密密文。但是当我使用byte[] encryptedBytes = DatatypeConverter.parseHexBinary(src);时 我没有设法运行该程序。我该如何解决这个问题?

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);
        byte[] buf = new byte[1024];
        int read;
        while((read=cin.read(buf))>=0)  //reading encrypted data!
        {
            baos.write(buf,0,read);     //writing decrypted data!
        }

        // closing streams
        cin.close();
        byte[] encryptedBytes = DatatypeConverter.parseHexBinary(src);
        return dst;
    }
}

1 个答案:

答案 0 :(得分:1)

示例代码中有三个错误:

  1. 由于byte[] encryptedBytes = DatatypeConverter.parseHexBinary(src);方法结束时decryptString而无法编译
  2. 因为您已将加密的字符串打印为十六进制表示,所以在尝试解密之前必须将其转换回正常的字节数组。在decryptString,您应该致电byte[] encryptedBytes = DatatypeConverter.parseHexBinary(src);。这将转换回byte[]而不是十六进制表示。
  3. 您需要一行在baos结尾处将String转换为decrpytString。这就像dst = baos.toString();

    一样简单

    通过这三项更改,我能够加密和解密字符串。