Skip to content

Commit 0aac749

Browse files
authored
Merge pull request #234 from michaelcheers/patch-1
fix: use cryptographically secure RNG for IV generation
2 parents b3ab870 + d28575c commit 0aac749

File tree

1 file changed

+6
-20
lines changed

1 file changed

+6
-20
lines changed
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
1+
using System.Security.Cryptography;
2+
13
namespace iTextSharp.text.pdf.crypto;
24

35
/// <summary>
4-
/// An initialization vector generator for a CBC block encryption. It's a random generator based on RC4.
6+
/// An initialization vector generator for a CBC block encryption.
7+
/// Uses cryptographically secure random number generation.
58
/// @author Paulo Soares ([email protected])
69
/// </summary>
710
public static class IvGenerator
811
{
9-
private static readonly ArcfourEncryption _rc4;
10-
11-
static IvGenerator()
12-
{
13-
_rc4 = new ArcfourEncryption();
14-
var longBytes = new byte[8];
15-
var val = DateTime.Now.Ticks;
16-
for (var i = 0; i != 8; i++)
17-
{
18-
longBytes[i] = (byte)val;
19-
val = (long)((ulong)val >> 8);
20-
}
21-
22-
_rc4.PrepareArcfourKey(longBytes);
23-
}
24-
2512
/// <summary>
2613
/// Gets a 16 byte random initialization vector.
2714
/// </summary>
@@ -36,11 +23,10 @@ static IvGenerator()
3623
public static byte[] GetIv(int len)
3724
{
3825
var b = new byte[len];
39-
lock (_rc4)
26+
using (var rng = RandomNumberGenerator.Create())
4027
{
41-
_rc4.EncryptArcfour(b);
28+
rng.GetBytes(b);
4229
}
43-
4430
return b;
4531
}
4632
}

0 commit comments

Comments
 (0)