ncr/
lib.rs

1//! # No Chat Reports (NCR) Chat Encryption
2//!
3//! This crate implements the [No Chat Reports](https://github.com/Aizistral-Studios/No-Chat-Reports)'s custom chat encryption.
4//! More specifically this implements a fork of [No Chat Reports](https://github.com/HKS-HNS/No-Chat-Reports).
5//!
6//! Currently all functionalities of the custom chat encryption are implemented.
7//! You can still use this crate normally if you are using the original [No Chat Reports](https://github.com/Aizistral-Studios/No-Chat-Reports).
8//!
9//! - Caesar, Ecb, Cfb8 and Gcm encryption
10//! - Base64 (old), Base64r, [Sus16](https://github.com/HKS-HNS/No-Chat-Reports) and [Mc256](https://github.com/HKS-HNS/No-Chat-Reports) encoding
11//! - Passphrase
12//!
13//! # Examples
14//!
15//! ## Encrypting
16//!
17//! ```rust
18//! use ncr::{
19//!     encoding::Base64rEncoding,
20//!     encryption::{Cfb8Encryption, Encryption},
21//!     utils::prepend_header,
22//!     AesKey,
23//! };
24//!
25//! let key = AesKey::gen_from_passphrase(b"secret");
26//!
27//! let plaintext = prepend_header("I love Minecraft!");
28//! let ciphertext = Cfb8Encryption::<Base64rEncoding>::encrypt(&plaintext, &key).unwrap();
29//!
30//! println!("{}", ciphertext);
31//! ```
32//!
33//! ## Decrypting
34//!
35//! ```rust
36//! use ncr::{
37//!     encoding::Base64rEncoding,
38//!     encryption::{Cfb8Encryption, Encryption},
39//!     utils::trim_header,
40//!     AesKey,
41//! };
42//!
43//! let key = AesKey::gen_from_passphrase(b"secret");
44//!
45//! let ciphertext = r#"%[2_0»³"!7).«?;!.$¥`¶:8~667ª¸[¬)¢+¤^"#;
46//! let plaintext = Cfb8Encryption::<Base64rEncoding>::decrypt(ciphertext, &key).unwrap();
47//!
48//! let plaintext = trim_header(&plaintext).unwrap();
49//!
50//! assert_eq!(plaintext, "I love Minecraft!");
51//! ```
52//!
53//! # Features
54//!
55//! Current there are 4 feature flags.
56//!
57//!  - `passphrase` (default): Enable key generation from passphrase.
58//!  - `cfb8`: Enable aes/cfb8 encryption.
59//!  - `ecb`: Enable aes/ecb encryption.
60//!  - `gcm`: Enable aes/gcm encryption.
61//!
62//! # How NCR encrypt chat messages
63//! 1. Two characters `#%` will be prepended to every message.
64//! 2. The string is fed into one of the encryption algorithms.
65//! 3. The encrypted bytes are fed into one of the encoding algorithms to turn them into sendable Minecraft messages.
66//! 
67//! The reversal is done on decryption.
68
69#![cfg_attr(docsrs, feature(doc_auto_cfg))]
70
71mod aes_key;
72pub mod encoding;
73pub mod encryption;
74pub mod utils;
75
76use std::fmt;
77
78pub use aes_key::AesKey;
79
80/// This represents all errors that can happen in this crate.
81#[derive(Debug, Clone, PartialEq, Eq)]
82pub enum NcrError {
83    EncryptError,
84    DecryptError,
85    DecodeError,
86    HeaderError,
87}
88
89impl fmt::Display for NcrError {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        write!(
92            f,
93            "{}",
94            match self {
95                NcrError::EncryptError => "Encrypt error",
96                NcrError::DecryptError => "Decrypt error",
97                NcrError::DecodeError => "Decode error",
98                NcrError::HeaderError => "Header error",
99            }
100        )
101    }
102}