【发布时间】:2015-08-31 14:16:19
【问题描述】:
我遇到了一个问题,如果使用 CipherInputStream,则针对由 FileInputStream 支持的 InputStream 的代码不起作用。
示例如下:
// skipCount is same as n in a FileInputStream
FileInputStream fis;
...
skipCount = fis.skip(n)
如果使用 CipherInputStream 则获得不同的行为
// skipCount is always 0
CipherInputStream cis;
...
skipCount = cis.skip(n)
经过进一步调试后,skip 仅在与 read() 调用结合使用时才有效(即返回值 > 0)。
有没有比滚动我自己的依赖于调用 read 的“skip”方法更好的方法让 skip 与 CipherInputStream 一起工作?
另外,有没有办法告诉 CipherInputStream 自动执行“读取”作为调用跳过调用的一部分?否则,在 CipherInputStream 中,skip API 看起来很不稳定。
MCVE
public class TestSkip {
public static final String ALGO = "AES/CBC/PKCS5Padding";
public static final String CONTENT = "Strive not to be a success, but rather to be of value";
private static int BlockSizeBytes = 16;
private static SecureRandom random = null;
static {
try {
random = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Could not initialize AES encryption", e);
}
}
static byte[] getKeyBytes() throws NoSuchAlgorithmException, UnsupportedEncodingException
{
byte[] key = "Not a secure string!".getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16); // use only first 128 bit
return key;
}
static KeySpec getKeySpec() throws GeneralSecurityException, UnsupportedEncodingException
{
return new SecretKeySpec(getKeyBytes(), "AES");
}
static byte[] getIv ()
{
byte[] iv = new byte[BlockSizeBytes];
random.nextBytes(iv);
return iv;
}
static Cipher initCipher (int mode, byte[] iv) throws GeneralSecurityException, UnsupportedEncodingException
{
KeySpec spec = getKeySpec();
Cipher cipher = Cipher.getInstance(ALGO);
cipher.init(mode, (SecretKey) spec, new IvParameterSpec(iv));
return cipher;
}
static void encrypt(String fileName) throws
GeneralSecurityException,
IOException
{
FileOutputStream fos = new FileOutputStream(fileName);
byte[] iv = getIv();
fos.write(iv);
Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, iv);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
PrintWriter pw = new PrintWriter(cos);
pw.println(CONTENT);
pw.close();
}
static void skipAndCheck(String fileName) throws
GeneralSecurityException,
IOException
{
FileInputStream fis = new FileInputStream(fileName);
byte[] iv = new byte[BlockSizeBytes];
if (fis.read(iv) != BlockSizeBytes) {
throw new GeneralSecurityException("Could not retrieve IV from AES encrypted stream");
}
Cipher cipher = initCipher(Cipher.DECRYPT_MODE, iv);
CipherInputStream cis = new CipherInputStream(fis, cipher);
// This does not skip
long count = cis.skip(32);
System.out.println("Bytes skipped: " + count);
// Read a line
InputStreamReader is = new InputStreamReader(cis);
BufferedReader br = new BufferedReader(is);
String read = br.readLine();
System.out.println("Content after skipping 32 bytes is: " + read);
br.close();
}
static InputStream getWrapper(CipherInputStream cis) {
return new SkipInputStream(cis);
}
public static void main(String[] args) throws
IOException,
GeneralSecurityException
{
String fileName = "EncryptedSample.txt";
encrypt(fileName);
skipAndCheck(fileName);
}
}
【问题讨论】:
-
问题可能取决于实际使用的密码(流密码或分组密码操作模式)。您能否添加更多细节,或者更好地创建一个MCVE?
-
我也遇到过,CipherInputStream.skip(n) 返回 0,但 read() 可以读取下一个字节,然后 next skip(n) 返回 >0。我认为 InputStream.skip() 定义松散,实际上允许这种丑陋的行为。我的解决方法是制作包装器并返回 (read()!=-1)?1:0 如果跳过返回零。这样它会跳过至少 1 个字节,然后在下次调用时会跳过更多。
标签: java file-io cryptography inputstream