mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-03-29 15:31:37 +00:00
Upgraded cipher
This commit is contained in:
parent
cf6226546e
commit
a03c4fc06a
@ -26,7 +26,6 @@ rocket = { version = "0.5.1", features = ["json", "tls"] }
|
|||||||
rand = "0.9.1"
|
rand = "0.9.1"
|
||||||
rand_chacha = "0.9"
|
rand_chacha = "0.9"
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
cipher = { version = "0.4.4", features = ["std"] }
|
|
||||||
aes = "0.8.4"
|
aes = "0.8.4"
|
||||||
cbc = "0.1.2"
|
cbc = "0.1.2"
|
||||||
pbkdf2 = "0.12.2"
|
pbkdf2 = "0.12.2"
|
||||||
|
|||||||
@ -98,9 +98,14 @@ impl Encryption {
|
|||||||
/// Encrypts the given data.
|
/// Encrypts the given data.
|
||||||
pub fn encrypt(&self, data: &str) -> Result<EncryptedText, String> {
|
pub fn encrypt(&self, data: &str) -> Result<EncryptedText, String> {
|
||||||
let cipher = Aes256CbcEnc::new(&self.key.into(), &self.iv.into());
|
let cipher = Aes256CbcEnc::new(&self.key.into(), &self.iv.into());
|
||||||
let encrypted = cipher.encrypt_padded_vec_mut::<Pkcs7>(data.as_bytes());
|
let data = data.as_bytes();
|
||||||
|
let mut buffer = vec![0u8; data.len() + 16];
|
||||||
|
buffer[..data.len()].copy_from_slice(data);
|
||||||
|
let encrypted = cipher
|
||||||
|
.encrypt_padded_mut::<Pkcs7>(&mut buffer, data.len())
|
||||||
|
.map_err(|e| format!("Error encrypting data: {e}"))?;
|
||||||
let mut result = BASE64_STANDARD.encode(self.secret_key_salt);
|
let mut result = BASE64_STANDARD.encode(self.secret_key_salt);
|
||||||
result.push_str(&BASE64_STANDARD.encode(&encrypted));
|
result.push_str(&BASE64_STANDARD.encode(encrypted));
|
||||||
Ok(EncryptedText::new(result))
|
Ok(EncryptedText::new(result))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,9 +123,12 @@ impl Encryption {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let cipher = Aes256CbcDec::new(&self.key.into(), &self.iv.into());
|
let cipher = Aes256CbcDec::new(&self.key.into(), &self.iv.into());
|
||||||
let decrypted = cipher.decrypt_padded_vec_mut::<Pkcs7>(encrypted).map_err(|e| format!("Error decrypting data: {e}"))?;
|
let mut buffer = encrypted.to_vec();
|
||||||
|
let decrypted = cipher
|
||||||
|
.decrypt_padded_mut::<Pkcs7>(&mut buffer)
|
||||||
|
.map_err(|e| format!("Error decrypting data: {e}"))?;
|
||||||
|
|
||||||
String::from_utf8(decrypted).map_err(|e| format!("Error converting decrypted data to string: {}", e))
|
String::from_utf8(decrypted.to_vec()).map_err(|e| format!("Error converting decrypted data to string: {}", e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user