【发布时间】:2014-11-12 06:41:11
【问题描述】:
我正在加密文件作为执行其他操作的更大应用程序的一部分。该文件正在使用 AES 加密(随机密钥)进行加密,并使用 RSA 公钥加密进行包装。这样做的原因是加密文件将由适当的人仅使用匹配的私钥发送/访问。
我加密文件的核心功能如下。我把keyBlock写在加密文件的前面,然后是加密的数据。
我有几个问题:
1) 从架构和安全的角度来看,这种加密文件的方法是否是真正加密文件的安全方法? ——还是有点傻?如果很傻,还有什么其他选择可能是合理的;和
2) 从安全角度来看,将 keyBlock 附加到加密文件的前面好吗?
提前感谢您的任何想法或评论。
Java 函数:
// File in = plain input file
// File out = encrypted output file
// Key pubKey = public Key (that wraps a random AES key)
public static void encryptFile(File in, File out, Key pubKey) throws Exception {
FileInputStream fin;
FileOutputStream fout;
int nread = 0;
byte[] inbuf = new byte[1024];
fout = new FileOutputStream(out);
fin = new FileInputStream(in);
SecureRandom random = new SecureRandom();
// symmetric wrapping
Key sKey = createKeyForAES(Config.SYM_CRYPTO_STR, random);
IvParameterSpec sIvSpec = createCtrIvForAES(0, random);
// encrypt symmetric key with RSA/pub key
Cipher xCipher = Cipher.getInstance(Config.RSA_INSTANCE);
xCipher.init(Cipher.ENCRYPT_MODE, pubKey, random);
byte[] keyBlock = xCipher.doFinal(packKeyAndIv(sKey, sIvSpec));
fout.write(keyBlock);
System.out.println("keyblock size = " + keyBlock.length);
// encrypt data with symmetric key
Cipher sCipher = Cipher.getInstance(Config.AES_INSTANCE);
sCipher.init(Cipher.ENCRYPT_MODE, sKey, sIvSpec);
// Now read our file and encrypt it.
while((nread = fin.read(inbuf)) >0) {
byte[] trimbuf = new byte[nread];
for(int i=0;i<nread;i++)
trimbuf[i] = inbuf[i];
byte[] newtmp = sCipher.update(trimbuf);
if(newtmp != null)
fout.write(newtmp);
}
byte[] finalbuf = sCipher.doFinal();
if(finalbuf !=null)
fout.write(finalbuf);
fout.flush();
fin.close();
fout.close();
}
【问题讨论】:
标签: java encryption