57 lines
2.2 KiB
Rust
57 lines
2.2 KiB
Rust
use crate::NetEaseCryptoError;
|
|
use aes::cipher::generic_array::GenericArray;
|
|
use aes::cipher::KeyInit;
|
|
use aes::Aes128Dec;
|
|
use cipher::block_padding::Pkcs7;
|
|
use cipher::BlockDecryptMut;
|
|
use itertools::Itertools;
|
|
|
|
const CONTENT_KEY: [u8; 0x10] = *b"hzHRAmso5kInbaxW";
|
|
|
|
/// Decrypt content key
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `key`: Encrypted content_key
|
|
///
|
|
/// returns: Result<Vec<u8, Global>, NetEaseCryptoError>
|
|
///
|
|
pub fn decrypt<T>(key: T) -> Result<Vec<u8>, NetEaseCryptoError>
|
|
where
|
|
T: AsRef<[u8]>,
|
|
{
|
|
let mut data = key.as_ref().iter().map(|&b| b ^ 0x64).collect_vec();
|
|
|
|
let aes = Aes128Dec::new(&GenericArray::from(CONTENT_KEY));
|
|
let content_key = aes
|
|
.decrypt_padded_mut::<Pkcs7>(&mut data[..])
|
|
.map_err(NetEaseCryptoError::ContentKeyDecryptError)?;
|
|
|
|
let key = match content_key.strip_prefix(b"neteasecloudmusic") {
|
|
None => Err(NetEaseCryptoError::ContentKeyWrongPrefix(
|
|
String::from_utf8_lossy(content_key).into(),
|
|
))?,
|
|
Some(key) => key,
|
|
};
|
|
|
|
Ok(Vec::from(key))
|
|
}
|
|
|
|
#[test]
|
|
fn test_decrypt_content_key() {
|
|
let enciphered_key = [
|
|
0x2C, 0xCE, 0xD5, 0xEB, 0x69, 0xEA, 0xFB, 0x14, 0x55, 0x0D, 0x45, 0xBF, 0x61, 0xDD, 0x17,
|
|
0x1D, 0x93, 0x71, 0x47, 0x1E, 0xE1, 0xDD, 0xDA, 0xF4, 0xD5, 0xE8, 0x4F, 0x1C, 0xBA, 0x00,
|
|
0x20, 0xC3, 0x02, 0xE9, 0xFE, 0x29, 0x92, 0xE1, 0x81, 0x45, 0x6F, 0x18, 0xC7, 0x2D, 0x11,
|
|
0xF2, 0xBC, 0x5B, 0xBC, 0xDC, 0x22, 0x33, 0xF9, 0x68, 0xB4, 0xB0, 0x28, 0x38, 0x3F, 0x63,
|
|
0x6C, 0x88, 0x66, 0x35, 0xF9, 0xE7, 0xB1, 0x70, 0x0E, 0xEE, 0x55, 0xAC, 0xB8, 0xED, 0x8B,
|
|
0x48, 0x17, 0x25, 0x3A, 0xE6, 0x5E, 0xB5, 0x80, 0x78, 0x8A, 0xCD, 0xDC, 0xE1, 0xEF, 0x3D,
|
|
0x30, 0xEC, 0x9C, 0x2A, 0xC6, 0xC7, 0x51, 0xAE, 0x3D, 0x11, 0xB5, 0x64, 0x88, 0x9E, 0xD6,
|
|
0x77, 0x66, 0xF6, 0x2B, 0x52, 0x9E, 0xFA, 0xF9, 0x63, 0xF6, 0xDE, 0x27, 0x10, 0x45, 0x82,
|
|
0xAC, 0x2D, 0x20, 0x84, 0x95, 0x4C, 0x0F, 0x7A, 0xAE, 0x8B, 0x91, 0x6D, 0x10, 0x2E, 0x63,
|
|
0x1C, 0xEA, 0xCA, 0xF9, 0x14, 0x97, 0xD8, 0xB3, 0xE8,
|
|
];
|
|
let key = decrypt(enciphered_key).expect("Failed to decrypt content");
|
|
assert_eq!(key, b"174279197715752960061821572626E7fT49x7dof9OKCgg9cdvhEuezy3iZCL1nFvBFd1T4uSktAJKmwZXsijPbijliionVUXXg9plTbXEclAE9Lb");
|
|
}
|