저는 암호를 처음 사용하지만 나중에 일부 응용 프로그램에서 사용할 계획입니다.Java CipherOutputStream이 모든 바이트를 반환하지 않습니다.
내가 만든 짧은 데모 프로그램에서 누락 된 구성 요소가 있는지 알고 싶습니다. 내가 알고 싶습니다 배열의 크기를 추측 주위에 얻을 수있는 방법이 있는지
나는
import java.io.*;
import java.security.GeneralSecurityException;
import java.security.spec.KeySpec;
import java.util.Arrays;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
public class CipherStreamDemo {
private static final byte[] salt={
(byte)0xC9, (byte)0xEF, (byte)0x7D, (byte)0xFA,
(byte)0xBA, (byte)0xDD, (byte)0x24, (byte)0xA9
};
private Cipher cipher;
private final SecretKey key;
public CipherStreamDemo() throws GeneralSecurityException, IOException{
SecretKeyFactory kf=SecretKeyFactory.getInstance("DES");
KeySpec spec=new DESKeySpec(salt);
key=kf.generateSecret(spec);
cipher=Cipher.getInstance("DES");
}
public void encrypt(byte[] buf) throws IOException, GeneralSecurityException{
cipher.init(Cipher.ENCRYPT_MODE,key);
OutputStream out=new CipherOutputStream(new FileOutputStream("crypt.dat"), cipher);
out.write(buf);
out.close();
}
public byte[] decrypt() throws IOException, GeneralSecurityException{
cipher.init(Cipher.DECRYPT_MODE, key);
InputStream in=new CipherInputStream(new FileInputStream("crypt.dat"), cipher);
byte[] buf=new byte[300];
int bytes=in.read(buf);
buf=Arrays.copyOf(buf, bytes);
in.close();
return buf;
}
public static void main(String[] args) {
try{
CipherStreamDemo csd=new CipherStreamDemo();
String pass="thisisasecretpassword";
csd.encrypt(pass.getBytes());
System.out.println(new String(csd.decrypt()));
}catch(Exception e){
e.printStackTrace();
}
}
}
//Output: thisisasecretpass