-1

我在结合 AES 加密时遇到了 java IO 这个奇怪的问题。我正在将一些加密文本写入二进制文件,在文本末尾附加 \n。说,我美丽的琴弦……看!我的另一根美丽的弦...

当我读回数据时,我得到以下文本作为回报,并带有许多额外的标签:我美丽的字符串......看!我的另一根美丽的弦...

这是我的独立代码:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class SecurityUtil
{
    public static final String  ENCRYPTION_ALGORITHM    = "AES";
    public static final String  CHARACTER_SET           = "UTF-8";
    private static final int    BLOCKS                  = 128;
    private static final String KEY                     = "SomeSortOfEncryptionKey";

    public static void main (String[] args) throws IOException
    {
        String str = "my beautiful string...\n";
        byte[] encrypt = SecurityUtil.encrypt (str);
        File f = new File ("C:\\myfile.dat");
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f, true));
        bos.write(encrypt);
        bos.flush();
        bos.close();

        str = "look! Another of my beautiful strings...";
        encrypt = SecurityUtil.encrypt (str);
        bos = new BufferedOutputStream(new FileOutputStream(f, true));
        bos.write(encrypt);
        bos.flush();
        bos.close();

        byte[] buffer = new byte[(int) f.length ()];
        FileInputStream fis = new FileInputStream (f);
        fis.read (buffer);
        fis.close ();

        String decrypt = SecurityUtil.decrypt (buffer);
        System.out.println(decrypt);
    }

    public static byte[] encrypt (String text)
    {
        try
        {
            byte[] rawKey = getRawKey (KEY.getBytes (CHARACTER_SET));
            SecretKeySpec skeySpec = new SecretKeySpec (rawKey, ENCRYPTION_ALGORITHM);
            Cipher cipher = Cipher.getInstance (ENCRYPTION_ALGORITHM);
            cipher.init (Cipher.ENCRYPT_MODE, skeySpec);
            return cipher.doFinal (text.getBytes (CHARACTER_SET));
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace ();
        }
        catch (IllegalBlockSizeException e)
        {
            e.printStackTrace ();
        }
        catch (BadPaddingException e)
        {
            e.printStackTrace ();
        }
        catch (InvalidKeyException e)
        {
            e.printStackTrace ();
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace ();
        }
        catch (NoSuchPaddingException e)
        {
            e.printStackTrace ();
        }
        return null;
    }

    public static String decrypt (byte[] data)
    {
        try
        {
            byte[] rawKey = getRawKey (KEY.getBytes (CHARACTER_SET));
            SecretKeySpec skeySpec = new SecretKeySpec (rawKey, ENCRYPTION_ALGORITHM);
            Cipher cipher = Cipher.getInstance (ENCRYPTION_ALGORITHM);
            cipher.init (Cipher.DECRYPT_MODE, skeySpec);
            byte[] decrypted = cipher.doFinal (data);
            return new String (decrypted);
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace ();
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace ();
        }
        catch (NoSuchPaddingException e)
        {
            e.printStackTrace ();
        }
        catch (InvalidKeyException e)
        {
            e.printStackTrace ();
        }
        catch (IllegalBlockSizeException e)
        {
            e.printStackTrace ();
        }
        catch (BadPaddingException e)
        {
            e.printStackTrace ();
        }
        return null;
    }

    private static byte[] getRawKey (byte[] seed)
    {
        try
        {
            KeyGenerator kgen = KeyGenerator.getInstance (ENCRYPTION_ALGORITHM);
            SecureRandom sr = SecureRandom.getInstance ("SHA1PRNG");
            sr.setSeed (seed);
            kgen.init (BLOCKS, sr);
            SecretKey skey = kgen.generateKey ();
            byte[] raw = skey.getEncoded ();
            return raw;
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace ();
        }
        return null;
    }
}

这是控制台输出(带有额外的空格):

my beautiful string...
                                                                     look! Another of my beautiful strings...

我究竟做错了什么?

4

1 回答 1

0

好的!我知道了。您不能只将加密文本附加到文件中。解决此问题的一种方法是从文件中提取现有文本,附加新文本,加密并再次写入文件。这似乎不是一个好的解决方案,但在我的情况下,文件中的文本很少,这可以工作。

于 2014-05-12T05:32:04.137 回答