-1

AES暗号化と組み合わせて、Java IOにこの奇妙な問題があります。テキストの末尾に \n を追加して、暗号化されたテキストをバイナリ ファイルに書き込んでいます。ねえ、私の美しいひも...見て!私の美しい弦のもう一つ...

データを読み返すと、多くの余分なタブが付いた次のテキストが返されます。my beautiful string... 見てください! 私の美しい弦のもう一つ...

ここに私の自己完結型のコードがあります:

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

Ok!わかった。暗号化されたテキストをファイルに追加することはできません。これを解決する 1 つの方法は、ファイルから既存のテキストを抽出し、新しいテキストを追加し、暗号化してファイルに再度書き込むことです。これは良い解決策ではないようですが、私の場合、ファイルにテキストがほとんどない場合、これは機能します。

于 2014-05-12T05:32:04.137 に答える