From 961ca041b2d0e17f72e154803f88ea2d1ad5d268 Mon Sep 17 00:00:00 2001 From: Gang Zhuo Date: Wed, 25 Nov 2015 21:26:45 -0500 Subject: [PATCH] Fix bug --- shadowsocks-csharp/Encryption/IVEncryptor.cs | 24 ++++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/shadowsocks-csharp/Encryption/IVEncryptor.cs b/shadowsocks-csharp/Encryption/IVEncryptor.cs index 29b98abe..f1922ac0 100755 --- a/shadowsocks-csharp/Encryption/IVEncryptor.cs +++ b/shadowsocks-csharp/Encryption/IVEncryptor.cs @@ -62,18 +62,22 @@ namespace Shadowsocks.Encryption } keyLen = ciphers[_method][0]; ivLen = ciphers[_method][1]; - if (CachedKeys.ContainsKey(k)) + if (!CachedKeys.ContainsKey(k)) { - _key = CachedKeys[k]; - } - else - { - byte[] passbuf = Encoding.UTF8.GetBytes(password); - _key = new byte[32]; - byte[] iv = new byte[16]; - bytesToKey(passbuf, _key); - CachedKeys[k] = _key; + lock (CachedKeys) + { + if (!CachedKeys.ContainsKey(k)) + { + byte[] passbuf = Encoding.UTF8.GetBytes(password); + _key = new byte[32]; + byte[] iv = new byte[16]; + bytesToKey(passbuf, _key); + CachedKeys[k] = _key; + } + } } + if (_key == null) + _key = CachedKeys[k]; } protected void bytesToKey(byte[] password, byte[] key)