Compare commits
2 Commits
31d5129a8a
...
8cbb7f5269
Author | SHA1 | Date | |
---|---|---|---|
|
8cbb7f5269 | ||
|
a19dfd1e1b |
|
@ -12,9 +12,11 @@ crate-type = ["dylib", "rlib"]
|
||||||
aes = "0.8.3"
|
aes = "0.8.3"
|
||||||
base64 = "0.21.2"
|
base64 = "0.21.2"
|
||||||
blockhash = "0.5.0"
|
blockhash = "0.5.0"
|
||||||
cbc = "0.1.2"
|
cbc = { version = "0.1.2", features = ["std"] }
|
||||||
|
cipher = "0.4.4"
|
||||||
des = "0.8.1"
|
des = "0.8.1"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
|
hmac-sha256 = "1.1.7"
|
||||||
hmac-sha512 = "1.1.5"
|
hmac-sha512 = "1.1.5"
|
||||||
image = "0.24.6"
|
image = "0.24.6"
|
||||||
md-5 = "0.10.5"
|
md-5 = "0.10.5"
|
||||||
|
|
|
@ -4,10 +4,12 @@ Rust 中可以使用的常用辅助功能工具箱。主要配备以下功能:
|
||||||
|
|
||||||
- 加解密算法
|
- 加解密算法
|
||||||
- [ ] 随机密钥自解密算法
|
- [ ] 随机密钥自解密算法
|
||||||
- [ ] AES-CBC 便捷加解密算法
|
- [x] AES-CBC 便捷加解密算法
|
||||||
- [ ] ZerosPadding
|
- [x] No Padding
|
||||||
- [ ] Pkcs7Padding
|
- [x] ZerosPadding
|
||||||
|
- [x] Pkcs7Padding
|
||||||
- [ ] DES-CBC 便捷加解密算法
|
- [ ] DES-CBC 便捷加解密算法
|
||||||
|
- [ ] No Padding
|
||||||
- [ ] ZerosPadding
|
- [ ] ZerosPadding
|
||||||
- [ ] Pkcs7Padding
|
- [ ] Pkcs7Padding
|
||||||
- [ ] 3DES 便捷加解密算法
|
- [ ] 3DES 便捷加解密算法
|
||||||
|
|
133
src/encryption/aes.rs
Normal file
133
src/encryption/aes.rs
Normal file
|
@ -0,0 +1,133 @@
|
||||||
|
use aes::Aes256;
|
||||||
|
use cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit};
|
||||||
|
|
||||||
|
type AesEncryptor = cbc::Encryptor<Aes256>;
|
||||||
|
type AesDecryptor = cbc::Decryptor<Aes256>;
|
||||||
|
|
||||||
|
/// 利用Sha256生成32字节的密钥
|
||||||
|
///
|
||||||
|
/// - `key` 原始密钥
|
||||||
|
fn generate_key<T: AsRef<[u8]>>(key: T) -> [u8; 32] {
|
||||||
|
let mut hasher = hmac_sha256::Hash::new();
|
||||||
|
hasher.update(key);
|
||||||
|
let result = hasher.finalize();
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 利用给定的密钥生成16字节的初始向量
|
||||||
|
fn generate_iv(key: &[u8; 32]) -> [u8; 16] {
|
||||||
|
let mut result = [0u8; 16];
|
||||||
|
for i in 0..16 {
|
||||||
|
result[i] = key[i] ^ key[i + 16];
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 使用指定的密钥和填充方式对数据进行加密。
|
||||||
|
/// 如果需要字符串形式的密文,可以配合使用`hex`或者`base64`等函数。
|
||||||
|
///
|
||||||
|
/// - `key` 密钥
|
||||||
|
/// - `padding` 填充方式
|
||||||
|
/// - `plain_data` 明文数据
|
||||||
|
pub fn encrypt<T: AsRef<[u8]>, D: AsRef<[u8]>>(
|
||||||
|
key: T,
|
||||||
|
padding: super::Padding,
|
||||||
|
plain_data: D,
|
||||||
|
) -> Vec<u8> {
|
||||||
|
let key = generate_key(key);
|
||||||
|
let iv = generate_iv(&key);
|
||||||
|
let encryptor = AesEncryptor::new(&key.into(), &iv.into());
|
||||||
|
let result = match padding {
|
||||||
|
super::Padding::NoPadding => encryptor
|
||||||
|
.encrypt_padded_vec_mut::<cipher::block_padding::NoPadding>(plain_data.as_ref()),
|
||||||
|
super::Padding::ZeroPadding => encryptor
|
||||||
|
.encrypt_padded_vec_mut::<cipher::block_padding::ZeroPadding>(plain_data.as_ref()),
|
||||||
|
super::Padding::Pkcs7Padding => {
|
||||||
|
encryptor.encrypt_padded_vec_mut::<cipher::block_padding::Pkcs7>(plain_data.as_ref())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 使用指定的密钥和填充方式对数据进行解密。
|
||||||
|
///
|
||||||
|
/// - `key` 密钥
|
||||||
|
/// - `padding` 填充方式
|
||||||
|
/// - `cipher_data` 密文数据
|
||||||
|
pub fn decrypt<T: AsRef<[u8]>, D: AsRef<[u8]>>(
|
||||||
|
key: T,
|
||||||
|
padding: super::Padding,
|
||||||
|
cipher_data: D,
|
||||||
|
) -> Result<Vec<u8>, super::DecryptFailedError> {
|
||||||
|
let key = generate_key(key);
|
||||||
|
let iv = generate_iv(&key);
|
||||||
|
let decryptor = AesDecryptor::new(&key.into(), &iv.into());
|
||||||
|
match padding {
|
||||||
|
super::Padding::NoPadding => decryptor
|
||||||
|
.decrypt_padded_vec_mut::<cipher::block_padding::NoPadding>(cipher_data.as_ref())
|
||||||
|
.map_err(|_| super::DecryptFailedError {}),
|
||||||
|
super::Padding::ZeroPadding => decryptor
|
||||||
|
.decrypt_padded_vec_mut::<cipher::block_padding::ZeroPadding>(cipher_data.as_ref())
|
||||||
|
.map_err(|_| super::DecryptFailedError {}),
|
||||||
|
super::Padding::Pkcs7Padding => decryptor
|
||||||
|
.decrypt_padded_vec_mut::<cipher::block_padding::Pkcs7>(cipher_data.as_ref())
|
||||||
|
.map_err(|_| super::DecryptFailedError {}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 快捷无填充加密函数
|
||||||
|
///
|
||||||
|
/// - `key` 密钥
|
||||||
|
/// - `plain_data` 明文数据
|
||||||
|
pub fn encrypt_no_padding<T: AsRef<[u8]>, D: AsRef<[u8]>>(key: T, plain_data: D) -> Vec<u8> {
|
||||||
|
encrypt(key, super::Padding::NoPadding, plain_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 快捷无填充解密函数
|
||||||
|
///
|
||||||
|
/// - `key` 密钥
|
||||||
|
/// - `cipher_data` 密文数据
|
||||||
|
pub fn decrypt_no_padding<T: AsRef<[u8]>, D: AsRef<[u8]>>(
|
||||||
|
key: T,
|
||||||
|
cipher_data: D,
|
||||||
|
) -> Result<Vec<u8>, super::DecryptFailedError> {
|
||||||
|
decrypt(key, super::Padding::NoPadding, cipher_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 快捷零填充加密函数
|
||||||
|
///
|
||||||
|
/// - `key` 密钥
|
||||||
|
/// - `plain_data` 明文数据
|
||||||
|
pub fn encrypt_zero_padding<T: AsRef<[u8]>, D: AsRef<[u8]>>(key: T, plain_data: D) -> Vec<u8> {
|
||||||
|
encrypt(key, super::Padding::ZeroPadding, plain_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 快捷零填充解密函数
|
||||||
|
///
|
||||||
|
/// - `key` 密钥
|
||||||
|
/// - `cipher_data` 密文数据
|
||||||
|
pub fn decrypt_zero_padding<T: AsRef<[u8]>, D: AsRef<[u8]>>(
|
||||||
|
key: T,
|
||||||
|
cipher_data: D,
|
||||||
|
) -> Result<Vec<u8>, super::DecryptFailedError> {
|
||||||
|
decrypt(key, super::Padding::ZeroPadding, cipher_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 快捷Pkcs7填充加密函数
|
||||||
|
///
|
||||||
|
/// - `key` 密钥
|
||||||
|
/// - `plain_data` 明文数据
|
||||||
|
pub fn encrypt_pkcs7_padding<T: AsRef<[u8]>, D: AsRef<[u8]>>(key: T, plain_data: D) -> Vec<u8> {
|
||||||
|
encrypt(key, super::Padding::Pkcs7Padding, plain_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 快捷Pkcs7填充解密函数
|
||||||
|
///
|
||||||
|
/// - `key` 密钥
|
||||||
|
/// - `cipher_data` 密文数据
|
||||||
|
pub fn decrypt_pkcs7_padding<T: AsRef<[u8]>, D: AsRef<[u8]>>(
|
||||||
|
key: T,
|
||||||
|
cipher_data: D,
|
||||||
|
) -> Result<Vec<u8>, super::DecryptFailedError> {
|
||||||
|
decrypt(key, super::Padding::Pkcs7Padding, cipher_data)
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
pub mod aes;
|
||||||
|
|
||||||
|
pub enum Padding {
|
||||||
|
NoPadding,
|
||||||
|
ZeroPadding,
|
||||||
|
Pkcs7Padding,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
#[error("Decrypt failed")]
|
||||||
|
pub struct DecryptFailedError {}
|
25
tests/aes_cryption.rs
Normal file
25
tests/aes_cryption.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#[cfg(test)]
|
||||||
|
mod aes_test {
|
||||||
|
#[test]
|
||||||
|
fn encrypt_zero_padding() {
|
||||||
|
let cipher_data =
|
||||||
|
rs_toolbox::encryption::aes::encrypt_zero_padding("123456", "Hello world.");
|
||||||
|
let cipher_hex = rs_toolbox::serialize::to_hex(cipher_data);
|
||||||
|
assert_eq!(cipher_hex, "722e10838a6c770d631701ee7c37c334");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn decrypt_zero_padding() {
|
||||||
|
let plain_data = rs_toolbox::encryption::aes::decrypt_zero_padding(
|
||||||
|
"123456",
|
||||||
|
rs_toolbox::serialize::from_hex("722e10838a6c770d631701ee7c37c334").unwrap(),
|
||||||
|
);
|
||||||
|
if plain_data.is_err() {
|
||||||
|
panic!("Decrypt failed");
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
String::from_utf8(plain_data.unwrap()).unwrap(),
|
||||||
|
"Hello world."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user