Skip to main content

Crate magic_crypt

Crate magic_crypt 

Source
Expand description

§MagicCrypt

MagicCrypt is a Java/PHP/NodeJS/Rust library to encrypt/decrypt strings, files, or data, using Data Encryption Standard(DES) or Advanced Encryption Standard(AES) algorithms. It supports CBC block cipher mode, PKCS7 padding and 64, 128, 192 or 256-bits key length.

§Security Notice

MagicCrypt uses an older encryption design and is not recommended for new projects.

For new applications, please use a modern library that supports authenticated encryption. Authenticated encryption can protect encrypted data from both reading and unauthorized changes.

§For Rust

§Migrating from 5.x to 6.0

Version 6.0 requires Rust 1.89 or later.

MagicCryptError::Base64Error and From<base64::DecodeError> now use base64 0.23. If your code uses base64::DecodeError with this library, update your direct base64 dependency to 0.23.1. Update matches from DecodeError::InvalidLastSymbol(_, _) to DecodeError::InvalidLastSymbol { .. }.

§Example

use magic_crypt::{new_magic_crypt, MagicCryptTrait};

let mc = new_magic_crypt!("magickey", 256);

let base64 = mc.encrypt_str_to_base64("http://magiclen.org");

assert_eq!("DS/2U8royDnJDiNY2ps3f6ZoTbpZo8ZtUGYLGEjwLDQ=", base64);

assert_eq!("http://magiclen.org", mc.decrypt_base64_to_string(&base64).unwrap());

§Change the Buffer Size

The default buffer size for the encrypt_reader_to_writer method and the decrypt_reader_to_writer method is 4096 bytes. If you want to change that, you can use the encrypt_reader_to_writer2 method or the decrypt_reader_to_writer2 method, and define a length explicitly.

For example, to change the buffer size to 256 bytes,

use std::io::Cursor;

use base64::Engine;
use magic_crypt::{new_magic_crypt, MagicCryptTrait};
use magic_crypt::array::typenum::U256;

let mc = new_magic_crypt!("magickey", 256);

let mut reader = Cursor::new("http://magiclen.org");
let mut writer = Vec::new();

mc.encrypt_reader_to_writer2::<U256>(&mut reader, &mut writer).unwrap();

let base64 = base64::engine::general_purpose::STANDARD.encode(&writer);

assert_eq!("DS/2U8royDnJDiNY2ps3f6ZoTbpZo8ZtUGYLGEjwLDQ=", base64);

assert_eq!("http://magiclen.org", mc.decrypt_base64_to_string(&base64).unwrap());

§No Std

Disable the default features to compile this crate without std.

[dependencies.magic-crypt]
version = "*"
default-features = false

§For Java

Refer to https://github.com/magiclen/MagicCrypt.

§For PHP

Refer to https://github.com/magiclen/MagicCrypt.

§For NodeJS

Refer to https://github.com/magiclen/node-magiccrypt.

Re-exports§

pub use cbc::cipher::array;

Macros§

new_magic_crypt
This macro provides a convenient way to create a MagicCrypt<bits> instance or a MagicCrypt instance.

Structs§

MagicCrypt
This struct can help you encrypt or decrypt data in a quick way.
MagicCrypt64
This struct can help you encrypt or decrypt data via DES in a quick way.
MagicCrypt128
This struct can help you encrypt or decrypt data via AES-128 in a quick way.
MagicCrypt192
This struct can help you encrypt or decrypt data via AES-192 in a quick way.
MagicCrypt256
This struct can help you encrypt or decrypt data via AES-256 in a quick way.

Enums§

MagicCryptError
Errors for MagicCrypt.
SecureBit
How secure does your encryption need to be?

Traits§

MagicCryptTrait
Methods for MagicCrypt and MagicCrypt<bits> structs.