0
이 코드의 모든 내용은 암호 해독을 제외하고 모두 작동합니다. 암호화 된 파일을 암호화합니다. 코드를 실행하고 프로젝트를 새로 고친 후 파일이 src에 표시됩니다. 그러나 해독 된 파일은 해독되지 않습니다. 아무도 나를 도울 수 있습니까, 나는 문제가 간단하다는 것을 알고 있습니다. 여기에 내가 여기까지 다음의 링크 중 일부입니다 : How to Encrypt or Decrypt a File in Java?가 Eclipse Java 암호 해독
코드는 작동하지만 출력 암호화되지 않은 파일은 (내가) (민간 및 공공하려면 openssl에서 내 키를 사용 명심) 해독되지 않았다. 나는 또한 이미 무제한 stength 자바 .jars를 추가했다.
코드 : 나는하려면 openssl에서 새 인증서 및 변경 디렉토리를 뜨거운 후
package CryptoFunction;
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
/**
* Utility class for encrypting/decrypting files.
* by: Michael Lones
* Minor Revisions by Glenn Hall
*/
public class CryptoFunction {
public static final int AES_Key_Size = 256;
Cipher pkCipher, aesCipher;
byte[] aesKey;
SecretKeySpec aeskeySpec;
/**
* Constructor: creates ciphers
*/
public CryptoFunction() throws GeneralSecurityException {
// create RSA public key cipher
pkCipher = Cipher.getInstance("RSA");
// create AES shared key cipher
aesCipher = Cipher.getInstance("AES");
}
/**
* Creates a new AES key
* A random AES key is generated to encrypt files.
* A key size (AES_Key_Size) of 256 bits is standard for AES
*/
public void makeKey() throws NoSuchAlgorithmException {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(AES_Key_Size);
SecretKey key = kgen.generateKey();
aesKey = key.getEncoded();
aeskeySpec = new SecretKeySpec(aesKey, "AES");
}
/**
* Decrypts an AES key from a file using an RSA private key
*/
@SuppressWarnings("resource")
public void loadKey(File in, File privateKeyFile) throws GeneralSecurityException, IOException {
// read private key to be used to decrypt the AES key
byte[] encodedKey = new byte[(int)privateKeyFile.length()]; new FileInputStream(privateKeyFile).read(encodedKey);
// create private key
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey pk = kf.generatePrivate(privateKeySpec);
// read AES key
pkCipher.init(Cipher.DECRYPT_MODE, pk);
aesKey = new byte[AES_Key_Size/8];
CipherInputStream is = new CipherInputStream(new FileInputStream(in), pkCipher);
is.read(aesKey);
aeskeySpec = new SecretKeySpec(aesKey, "AES");
}
/**
* Encrypts the AES key to a file using an RSA public key
*/
@SuppressWarnings("resource")
public void saveKey(File out, File publicKeyFile) throws IOException, GeneralSecurityException {
// read public key to be used to encrypt the AES key
byte[] encodedKey = new byte[(int)publicKeyFile.length()];
new FileInputStream(publicKeyFile).read(encodedKey);
// create public key
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pk = kf.generatePublic(publicKeySpec);
// write AES key
pkCipher.init(Cipher.ENCRYPT_MODE, pk);
CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), pkCipher);
os.write(aesKey);
os.close();
}
/**
* Encrypts and then copies the contents of a given file.
*/
public void encrypt(File in, File out) throws IOException, InvalidKeyException {
aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpec);
FileInputStream is = new FileInputStream(in);
CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), aesCipher);
System.out.println(out.getAbsolutePath());
copy(is, os);
os.close();
}
/**
* Decrypts and then copies the contents of a given file.
*/
public void decrypt(File in, File out) throws IOException, InvalidKeyException {
aesCipher.init(Cipher.DECRYPT_MODE, aeskeySpec);
CipherInputStream is = new CipherInputStream(new FileInputStream(in), aesCipher);
FileOutputStream os = new FileOutputStream(out);
System.out.println(out.getAbsolutePath());
copy(is, os);
is.close();
os.close();
}
/**
* Copies a stream.
*/
private void copy(InputStream is, OutputStream os) throws IOException {
int i;
byte[] b = new byte[1024];
while((i=is.read(b))!=-1) {
os.write(b, 0, i);
}
}
public static void main(String[] args) throws GeneralSecurityException, IOException {
CryptoFunction secure = new CryptoFunction();
// Encrypt code
System.out.println("Begin Encyption!");
File encryptFile = new File("encrypt.data");
File publicKeyData = new File("src/publickey.der");
File originalFile = new File("src/stufftoencrypt.txt");
File secureFile = new File("secure.data");
// create AES key
secure.makeKey();
// save AES key using public key
secure.saveKey(encryptFile, publicKeyData);
// save original file securely
secure.encrypt(originalFile, secureFile);
System.out.println("End Encryption!");
System.out.println("Begin decryption!");
// Decrypt code
//File encryptFile1 = new File("encrypt.data");
File privateKeyFile = new File("src/privatekey.der");
File secureFile1 = new File("secure.data");
File unencryptedFile = new File("unencryptedFile.txt");
// load AES key
secure.loadKey(encryptFile, privateKeyFile);
// decrypt file
secure.decrypt(secureFile1, unencryptedFile);
System.out.println("End decryption!"); // Display the string.
}
}
"코드가 작동하지만 암호화되지 않은 출력 파일이 해독되지 않았 음"이란 의미를 설명 할 수 있습니까? 'unencryptedFile.txt'는 존재하지 않지만 다른 것들은 존재하지 않습니까? 또한'aesKey'가'secure.loadKey'의 앞뒤에 정확히 같은 바이트를 포함하는지 확인 했습니까? – zapl
파일을 소속되지 않은 장소에 두지 않고이 파일을 시험해 볼 수 있습니까? 새로 고칠 때까지 Eclipse에 항상 업데이트가 표시되는 것은 아닙니다. 소스 폴더 대신'data' 폴더를 사용해보십시오. –
코드가 작동합니다. –