我尝试将其转换为byte []并将其存储在bytea上,但它不起作用,我没有收到我存储的相同密钥。使用类型字符串不起作用
如果您对更多细节感兴趣,我的应用程序是使用AES加密和解密图片,这些是加密和解密的方法
public void crypt() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IOException{
Cipher cipher = Cipher.getInstance("AES");
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecretKey secKey = keyGen.generateKey();
byte[] encoded = secKey.getEncoded();
this.setCodeCrypt(encoded);
cipher.init(Cipher.ENCRYPT_MODE, secKey);
String cleartextFile = this.lien;
String ciphertextFile = "crypted img.jpg";
FileInputStream fis = new FileInputStream(cleartextFile);
FileOutputStream fos = new FileOutputStream(ciphertextFile);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int i;
while ((i = fis.read()) != -1) {
cos.write(i);
}
cos.close();
}
// Decrypt
public void decrypt() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IOException{
try {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/papiersadmin";
String user = "postgres";
String passwd = "postgresql";
java.sql.Connection conn = DriverManager.getConnection(url, user,passwd);
Statement state = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
// loading a picture knowing its path (lien)
ResultSet result = state.executeQuery("SELECT * FROM image WHERE lien = '"+this.lien+"'");
while(result.next()){
setCodeCrypt(result.getObject(6).toString().getBytes());}
state.close();
} catch (Exception e) {
e.printStackTrace();
}
Cipher cipher = Cipher.getInstance("AES");
SecretKey originalKey = new SecretKeySpec(codeCrypt, 0, codeCrypt.length, "AES");
cipher.init(Cipher.DECRYPT_MODE, originalKey);
String cleartextFile = "decrypted img.jpg";
String ciphertextFile = this.lien;
FileInputStream fis = new FileInputStream(ciphertextFile);
FileOutputStream fos = new FileOutputStream(cleartextFile);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int i;
while ((i = fis.read()) != -1) {
cos.write(i);
}
cos.close();
}
答案 0 :(得分:0)
使用byte[]
将toString()
转换为字符串,然后调用getBytes()
将无效。我建议从ResultSet.getBytes()
开始。