rust-rsa-tool 0.1.1

A sophisticated encryption utility employing a hybrid cryptographic system. It integrates RSA and AES algorithms, supports both CBC and ECB operational modes, and utilizes Base64-encoded keys for streamlined key management.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use aes::cipher::{block_padding::Pkcs7, BlockDecryptMut, BlockEncryptMut, KeyIvInit, KeyInit};
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
use rand::rngs::OsRng;
use rand::RngCore;
use rsa::pkcs8::{DecodePrivateKey, DecodePublicKey, EncodePrivateKey, EncodePublicKey};
use rsa::{Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey};
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use thiserror::Error;

type Aes128CbcEnc = cbc::Encryptor<aes::Aes128>;
type Aes128CbcDec = cbc::Decryptor<aes::Aes128>;
type Aes128EcbEnc = ecb::Encryptor<aes::Aes128>;
type Aes128EcbDec = ecb::Decryptor<aes::Aes128>;

/// RSA最大加密明文大小 (for 2048-bit key with PKCS#1 v1.5 padding)
const MAX_ENCRYPT_BLOCK: usize = 117;

/// Custom error types
#[derive(Error, Debug)]
pub enum RsaUtilsError {
    #[error("RSA encryption error: {0}")]
    RsaError(#[from] rsa::Error),

    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    #[error("Base64 decode error: {0}")]
    Base64Error(#[from] base64::DecodeError),

    #[error("PKCS8 error: {0}")]
    Pkcs8Error(String),

    #[error("Decryption error: {0}")]
    DecryptionError(String),

    #[error("Cipher error: {0}")]
    CipherError(String),
}

/// RSA key pair container
pub struct KeyPair {
    pub public_key: RsaPublicKey,
    pub private_key: RsaPrivateKey,
}

/// Initialize and generate RSA key pair (2048-bit)
pub fn init_key() -> Result<KeyPair, RsaUtilsError> {
    let mut rng = OsRng;
    let bits = 2048;
    let private_key = RsaPrivateKey::new(&mut rng, bits)?;
    let public_key = RsaPublicKey::from(&private_key);

    Ok(KeyPair {
        public_key,
        private_key,
    })
}

/// Get public key from base64 encoded string (X.509/SPKI format)
pub fn get_public_key(key_str: &str) -> Result<RsaPublicKey, RsaUtilsError> {
    let key_bytes = BASE64.decode(key_str)?;

    // Try to parse as SPKI (X.509) format
    let public_key = RsaPublicKey::from_public_key_der(&key_bytes)
        .map_err(|e| RsaUtilsError::Pkcs8Error(format!("Failed to parse public key: {}", e)))?;

    Ok(public_key)
}

/// Get private key from base64 encoded string (PKCS#8 format)
pub fn get_private_key(key_str: &str) -> Result<RsaPrivateKey, RsaUtilsError> {
    let key_bytes = BASE64.decode(key_str)?;

    // Try to parse as PKCS#8 format
    let private_key = RsaPrivateKey::from_pkcs8_der(&key_bytes)
        .map_err(|e| RsaUtilsError::Pkcs8Error(format!("Failed to parse private key: {}", e)))?;

    Ok(private_key)
}

/// Encode public key to base64 string (X.509/SPKI format)
pub fn encode_public_key(public_key: &RsaPublicKey) -> Result<String, RsaUtilsError> {
    let der = public_key
        .to_public_key_der()
        .map_err(|e| RsaUtilsError::Pkcs8Error(format!("Failed to encode public key: {}", e)))?;
    Ok(BASE64.encode(der.as_bytes()))
}

/// Encode private key to base64 string (PKCS#8 format)
pub fn encode_private_key(private_key: &RsaPrivateKey) -> Result<String, RsaUtilsError> {
    let der = private_key
        .to_pkcs8_der()
        .map_err(|e| RsaUtilsError::Pkcs8Error(format!("Failed to encode private key: {}", e)))?;
    Ok(BASE64.encode(der.as_bytes()))
}

/// Encrypt data with RSA public key (supports data larger than key size via chunking)
pub fn encrypt(plain_text: &[u8], public_key_str: &str) -> Result<Vec<u8>, RsaUtilsError> {
    let public_key = get_public_key(public_key_str)?;
    let mut rng = OsRng;

    let mut result = Vec::new();
    let mut offset = 0;
    let input_len = plain_text.len();

    while offset < input_len {
        let chunk_size = std::cmp::min(MAX_ENCRYPT_BLOCK, input_len - offset);
        let chunk = &plain_text[offset..offset + chunk_size];

        let encrypted_chunk = public_key.encrypt(&mut rng, Pkcs1v15Encrypt, chunk)?;
        result.extend_from_slice(&encrypted_chunk);

        offset += chunk_size;
    }

    Ok(result)
}

/// Encrypt file using hybrid encryption (AES for file content, RSA for AES key)
/// This matches the Java implementation's approach
pub fn encrypt_file<P: AsRef<Path>>(
    input_path: P,
    output_path: P,
    public_key_str: &str,
) -> Result<(), RsaUtilsError> {
    // Generate random AES key (128-bit)
    let mut aes_key = [0u8; 16];
    let mut iv = [0u8; 16];
    OsRng.fill_bytes(&mut aes_key);
    OsRng.fill_bytes(&mut iv);

    // Get RSA public key
    let public_key = get_public_key(public_key_str)?;
    let mut rng = OsRng;

    // Wrap (encrypt) the AES key with RSA
    let mut key_to_wrap = Vec::new();
    key_to_wrap.extend_from_slice(&aes_key);
    key_to_wrap.extend_from_slice(&iv);

    let wrapped_key = public_key.encrypt(&mut rng, Pkcs1v15Encrypt, &key_to_wrap)?;

    // Open input and output files
    let mut input_file = File::open(input_path)?;
    let mut output_file = File::create(output_path)?;

    // Write wrapped key length and wrapped key
    output_file.write_all(&(wrapped_key.len() as u32).to_be_bytes())?;
    output_file.write_all(&wrapped_key)?;

    // Encrypt file content with AES
    let cipher = Aes128CbcEnc::new(&aes_key.into(), &iv.into());
    encrypt_stream(&mut input_file, &mut output_file, cipher)?;

    Ok(())
}

/// Encrypt file using Java-compatible format (AES/ECB mode)
/// This generates files that can be decrypted by Java's default Cipher.getInstance("AES")
pub fn encrypt_file_java_ecb<P: AsRef<Path>>(
    input_path: P,
    output_path: P,
    public_key_str: &str,
) -> Result<(), RsaUtilsError> {
    // Generate random AES key (128-bit)
    // Note: ECB mode doesn't use IV
    let mut aes_key = [0u8; 16];
    OsRng.fill_bytes(&mut aes_key);

    // Get RSA public key
    let public_key = get_public_key(public_key_str)?;
    let mut rng = OsRng;

    // Wrap (encrypt) only the AES key with RSA (no IV for ECB mode)
    let wrapped_key = public_key.encrypt(&mut rng, Pkcs1v15Encrypt, &aes_key)?;

    // Open input and output files
    let mut input_file = File::open(input_path)?;
    let mut output_file = File::create(output_path)?;

    // Write wrapped key length and wrapped key
    output_file.write_all(&(wrapped_key.len() as u32).to_be_bytes())?;
    output_file.write_all(&wrapped_key)?;

    // Encrypt file content with AES-ECB
    let cipher = Aes128EcbEnc::new(&aes_key.into());
    encrypt_stream_ecb(&mut input_file, &mut output_file, cipher)?;

    Ok(())
}

/// Decrypt file using hybrid decryption (RSA for AES key, AES for file content)
pub fn decrypt_file<P: AsRef<Path>>(
    input_path: P,
    output_path: P,
    private_key_str: &str,
) -> Result<(), RsaUtilsError> {
    // Get RSA private key
    let private_key = get_private_key(private_key_str)?;

    // Open input and output files
    let mut input_file = File::open(input_path)?;
    let mut output_file = File::create(output_path)?;

    // Read wrapped key length
    let mut length_bytes = [0u8; 4];
    input_file.read_exact(&mut length_bytes)?;
    let wrapped_key_len = u32::from_be_bytes(length_bytes) as usize;

    // Read wrapped key
    let mut wrapped_key = vec![0u8; wrapped_key_len];
    input_file.read_exact(&mut wrapped_key)?;

    // Unwrap (decrypt) the AES key with RSA
    let unwrapped = private_key
        .decrypt(Pkcs1v15Encrypt, &wrapped_key)
        .map_err(|e| RsaUtilsError::DecryptionError(format!("Failed to unwrap key: {}", e)))?;

    if unwrapped.len() != 32 {
        return Err(RsaUtilsError::DecryptionError(
            "Invalid unwrapped key size".to_string(),
        ));
    }

    let mut aes_key = [0u8; 16];
    let mut iv = [0u8; 16];
    aes_key.copy_from_slice(&unwrapped[0..16]);
    iv.copy_from_slice(&unwrapped[16..32]);

    // Decrypt file content with AES
    let cipher = Aes128CbcDec::new(&aes_key.into(), &iv.into());
    decrypt_stream(&mut input_file, &mut output_file, cipher)?;

    Ok(())
}

/// Decrypt file encrypted by Java (using AES/ECB mode)
/// Java's default Cipher.getInstance("AES") uses ECB mode without IV
pub fn decrypt_file_java_ecb<P: AsRef<Path>>(
    input_path: P,
    output_path: P,
    private_key_str: &str,
) -> Result<(), RsaUtilsError> {
    // Get RSA private key
    let private_key = get_private_key(private_key_str)?;

    // Open input and output files
    let mut input_file = File::open(input_path)?;
    let mut output_file = File::create(output_path)?;

    // Read wrapped key length
    let mut length_bytes = [0u8; 4];
    input_file.read_exact(&mut length_bytes)?;
    let wrapped_key_len = u32::from_be_bytes(length_bytes) as usize;

    // Read wrapped key
    let mut wrapped_key = vec![0u8; wrapped_key_len];
    input_file.read_exact(&mut wrapped_key)?;

    // Unwrap (decrypt) the AES key with RSA
    let aes_key_bytes = private_key
        .decrypt(Pkcs1v15Encrypt, &wrapped_key)
        .map_err(|e| RsaUtilsError::DecryptionError(format!("Failed to unwrap key: {}", e)))?;

    if aes_key_bytes.len() != 16 {
        return Err(RsaUtilsError::DecryptionError(
            format!("Invalid unwrapped key size: expected 16 bytes, got {}", aes_key_bytes.len()),
        ));
    }

    let mut aes_key = [0u8; 16];
    aes_key.copy_from_slice(&aes_key_bytes[0..16]);

    // Decrypt file content with AES-ECB
    let cipher = Aes128EcbDec::new(&aes_key.into());
    decrypt_stream_ecb(&mut input_file, &mut output_file, cipher)?;

    Ok(())
}

/// Encrypt data stream with AES cipher (CBC mode)
fn encrypt_stream<R: Read, W: Write>(
    input: &mut R,
    output: &mut W,
    cipher: Aes128CbcEnc,
) -> Result<(), RsaUtilsError> {
    let mut buffer = Vec::new();
    input.read_to_end(&mut buffer)?;

    // Pad and encrypt
    let ciphertext = cipher.encrypt_padded_vec_mut::<Pkcs7>(&buffer);

    output.write_all(&ciphertext)?;
    Ok(())
}

/// Encrypt data stream with AES cipher (ECB mode - for Java compatibility)
fn encrypt_stream_ecb<R: Read, W: Write>(
    input: &mut R,
    output: &mut W,
    cipher: Aes128EcbEnc,
) -> Result<(), RsaUtilsError> {
    let mut buffer = Vec::new();
    input.read_to_end(&mut buffer)?;

    // Pad and encrypt
    let ciphertext = cipher.encrypt_padded_vec_mut::<Pkcs7>(&buffer);

    output.write_all(&ciphertext)?;
    Ok(())
}

/// Decrypt data stream with AES cipher (CBC mode)
fn decrypt_stream<R: Read, W: Write>(
    input: &mut R,
    output: &mut W,
    cipher: Aes128CbcDec,
) -> Result<(), RsaUtilsError> {
    let mut buffer = Vec::new();
    input.read_to_end(&mut buffer)?;

    // Decrypt and unpad
    let plaintext = cipher
        .decrypt_padded_vec_mut::<Pkcs7>(&buffer)
        .map_err(|e| RsaUtilsError::DecryptionError(format!("Decryption failed: {}", e)))?;

    output.write_all(&plaintext)?;
    Ok(())
}

/// Decrypt data stream with AES cipher (ECB mode - for Java compatibility)
fn decrypt_stream_ecb<R: Read, W: Write>(
    input: &mut R,
    output: &mut W,
    cipher: Aes128EcbDec,
) -> Result<(), RsaUtilsError> {
    let mut buffer = Vec::new();
    input.read_to_end(&mut buffer)?;

    // Decrypt and unpad
    let plaintext = cipher
        .decrypt_padded_vec_mut::<Pkcs7>(&buffer)
        .map_err(|e| RsaUtilsError::DecryptionError(format!("Decryption failed: {}", e)))?;

    output.write_all(&plaintext)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use rsa::traits::PublicKeyParts;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_key_generation() {
        let key_pair = init_key().unwrap();
        assert_eq!(key_pair.private_key.size(), 256); // 2048 bits = 256 bytes
    }

    #[test]
    fn test_key_encoding_decoding() {
        let key_pair = init_key().unwrap();

        // Test public key
        let pub_key_str = encode_public_key(&key_pair.public_key).unwrap();
        let decoded_pub = get_public_key(&pub_key_str).unwrap();
        assert_eq!(key_pair.public_key.n(), decoded_pub.n());

        // Test private key
        let priv_key_str = encode_private_key(&key_pair.private_key).unwrap();
        let decoded_priv = get_private_key(&priv_key_str).unwrap();
        assert_eq!(key_pair.private_key.n(), decoded_priv.n());
    }

    #[test]
    fn test_small_data_encryption() {
        let key_pair = init_key().unwrap();
        let pub_key_str = encode_public_key(&key_pair.public_key).unwrap();

        let plain_text = b"Hello, RSA!";
        let encrypted = encrypt(plain_text, &pub_key_str).unwrap();

        // Decrypt to verify
        let decrypted = key_pair
            .private_key
            .decrypt(Pkcs1v15Encrypt, &encrypted)
            .unwrap();
        assert_eq!(plain_text, &decrypted[..]);
    }

    #[test]
    fn test_file_encryption_decryption() {
        let key_pair = init_key().unwrap();
        let pub_key_str = encode_public_key(&key_pair.public_key).unwrap();
        let priv_key_str = encode_private_key(&key_pair.private_key).unwrap();

        // Create test file
        let mut input_file = NamedTempFile::new().unwrap();
        let test_data = b"This is a test file for RSA encryption!\nIt has multiple lines.\nAnd some more content to make it interesting.";
        input_file.write_all(test_data).unwrap();
        input_file.flush().unwrap();

        // Create temp files for encrypted and decrypted output
        let encrypted_file = NamedTempFile::new().unwrap();
        let decrypted_file = NamedTempFile::new().unwrap();

        // Encrypt
        encrypt_file(
            input_file.path(),
            encrypted_file.path(),
            &pub_key_str,
        )
            .unwrap();

        // Decrypt
        decrypt_file(
            encrypted_file.path(),
            decrypted_file.path(),
            &priv_key_str,
        )
            .unwrap();

        // Verify
        let mut decrypted_content = Vec::new();
        File::open(decrypted_file.path())
            .unwrap()
            .read_to_end(&mut decrypted_content)
            .unwrap();

        assert_eq!(test_data, &decrypted_content[..]);
    }
}