|
1 | | -using Org.BouncyCastle.Crypto.Parameters; |
2 | | -using Org.BouncyCastle.Crypto; |
| 1 | +using Org.BouncyCastle.Crypto.Digests; |
| 2 | +using Org.BouncyCastle.Crypto.Generators; |
| 3 | +using Org.BouncyCastle.Crypto.Parameters; |
3 | 4 | using Org.BouncyCastle.Security; |
4 | | -using System; |
5 | | -using System.Collections.Generic; |
6 | | -using System.Linq; |
7 | 5 | using System.Text; |
8 | | -using System.Threading.Tasks; |
9 | 6 |
|
10 | | -namespace BouncyCastleCryptography.SymmetricEncryption |
| 7 | +namespace BouncyCastleCryptography.SymmetricEncryption; |
| 8 | + |
| 9 | +public static class BlowfishEncryptor |
11 | 10 | { |
12 | | - public class BlowfishEncryptor |
| 11 | + private const int Iterations = 600_000; |
| 12 | + |
| 13 | + public static byte[] BlowfishEncrypt(string input, string password, out byte[] salt, out byte[] iv) |
13 | 14 | { |
14 | | - public static byte[] BlowfishEncrypt(string input, string password, out byte[] iv) |
15 | | - { |
16 | | - var inputBytes = Encoding.UTF8.GetBytes(input); |
| 15 | + var inputBytes = Encoding.UTF8.GetBytes(input); |
| 16 | + |
| 17 | + var random = new SecureRandom(); |
| 18 | + salt = new byte[16]; |
| 19 | + random.NextBytes(salt); |
| 20 | + iv = new byte[8]; // Blowfish uses an 8-byte (64-bit) IV |
| 21 | + random.NextBytes(iv); |
| 22 | + |
| 23 | + var cipher = CipherUtilities.GetCipher("Blowfish/CBC/PKCS7Padding"); |
17 | 24 |
|
18 | | - var random = new SecureRandom(); |
19 | | - iv = new byte[8]; // Blowfish uses an 8-byte (64-bit) IV |
20 | | - random.NextBytes(iv); |
| 25 | + var keyParam = DeriveKey(password, salt); |
21 | 26 |
|
22 | | - var cipher = CipherUtilities.GetCipher("Blowfish/CBC/PKCS7Padding"); |
| 27 | + cipher.Init(true, new ParametersWithIV(keyParam, iv)); |
23 | 28 |
|
24 | | - var keyBytes = Encoding.UTF8.GetBytes(password); |
25 | | - var keyParam = new KeyParameter(keyBytes); |
| 29 | + return cipher.DoFinal(inputBytes); |
| 30 | + } |
26 | 31 |
|
27 | | - cipher.Init(true, new ParametersWithIV(keyParam, iv)); |
| 32 | + public static string BlowfishDecrypt(byte[] encryptedBytes, string password, byte[] salt, byte[] iv) |
| 33 | + { |
| 34 | + var cipher = CipherUtilities.GetCipher("Blowfish/CBC/PKCS7Padding"); |
28 | 35 |
|
29 | | - return cipher.DoFinal(inputBytes); |
30 | | - } |
| 36 | + var keyParam = DeriveKey(password, salt); |
31 | 37 |
|
32 | | - public static string BlowfishDecrypt(byte[] encryptedBytes, string password, byte[] iv) |
33 | | - { |
34 | | - var cipher = CipherUtilities.GetCipher("Blowfish/CBC/PKCS7Padding"); |
| 38 | + cipher.Init(false, new ParametersWithIV(keyParam, iv)); |
35 | 39 |
|
36 | | - var keyBytes = Encoding.UTF8.GetBytes(password); |
37 | | - var keyParam = new KeyParameter(keyBytes); |
| 40 | + var plainBytes = cipher.DoFinal(encryptedBytes); |
38 | 41 |
|
39 | | - cipher.Init(false, new ParametersWithIV(keyParam, iv)); |
| 42 | + return Encoding.UTF8.GetString(plainBytes); |
| 43 | + } |
40 | 44 |
|
41 | | - var plainBytes = cipher.DoFinal(encryptedBytes); |
| 45 | + private static KeyParameter DeriveKey(string password, byte[] salt) |
| 46 | + { |
| 47 | + // PBKDF2 with HMAC-SHA256 stretches the password and salt into a 128-bit key |
| 48 | + var generator = new Pkcs5S2ParametersGenerator(new Sha256Digest()); |
| 49 | + generator.Init(Encoding.UTF8.GetBytes(password), salt, Iterations); |
42 | 50 |
|
43 | | - return Encoding.UTF8.GetString(plainBytes); |
44 | | - } |
| 51 | + return (KeyParameter)generator.GenerateDerivedParameters("Blowfish", 128); |
45 | 52 | } |
46 | 53 | } |
0 commit comments