From ca8d87e9cdf1ae70d1f2889f6e939d6be7afaf53 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sat, 4 Mar 2023 18:01:41 +0700 Subject: Refactoring in CipherFactory --- crypto/src/crypto/util/CipherFactory.cs | 102 +++++++++++++++++--------------- 1 file changed, 53 insertions(+), 49 deletions(-) (limited to 'crypto') diff --git a/crypto/src/crypto/util/CipherFactory.cs b/crypto/src/crypto/util/CipherFactory.cs index 56f57c545..725159bfd 100644 --- a/crypto/src/crypto/util/CipherFactory.cs +++ b/crypto/src/crypto/util/CipherFactory.cs @@ -15,13 +15,14 @@ using Org.BouncyCastle.Crypto.Parameters; namespace Org.BouncyCastle.Crypto.Utilities { + // TODO[api] Make static public class CipherFactory { private CipherFactory() { } - private static readonly short[] rc2Ekb = + private static readonly byte[] RC2Ekb = { 0x5d, 0xbe, 0x9b, 0x8b, 0x11, 0x99, 0x6e, 0x4d, 0x59, 0xf3, 0x85, 0xa6, 0x3f, 0xb7, 0x83, 0xc5, 0xe4, 0x73, 0x6b, 0x3a, 0x68, 0x5a, 0xc0, 0x47, 0xa0, 0x64, 0x34, 0x0c, 0xf1, 0xd0, 0x52, 0xa5, @@ -48,64 +49,67 @@ namespace Org.BouncyCastle.Crypto.Utilities if (encAlg.Equals(PkcsObjectIdentifiers.rc4)) { - IStreamCipher cipher = new RC4Engine(); - cipher.Init(forEncryption, encKey); - return cipher; + var rc4Engine = new RC4Engine(); + rc4Engine.Init(forEncryption, encKey); + return rc4Engine; } - else - { - BufferedBlockCipher cipher = CreateCipher(encryptionAlgID.Algorithm); - Asn1Object sParams = encryptionAlgID.Parameters.ToAsn1Object(); - if (sParams != null && !(sParams is DerNull)) + BufferedBlockCipher cipher = CreateCipher(encryptionAlgID.Algorithm); + Asn1Object sParams = encryptionAlgID.Parameters.ToAsn1Object(); + + if (sParams != null && !(sParams is DerNull)) + { + if (encAlg.Equals(PkcsObjectIdentifiers.DesEde3Cbc) + || encAlg.Equals(AlgorithmIdentifierFactory.IDEA_CBC) + || encAlg.Equals(NistObjectIdentifiers.IdAes128Cbc) + || encAlg.Equals(NistObjectIdentifiers.IdAes192Cbc) + || encAlg.Equals(NistObjectIdentifiers.IdAes256Cbc) + || encAlg.Equals(NttObjectIdentifiers.IdCamellia128Cbc) + || encAlg.Equals(NttObjectIdentifiers.IdCamellia192Cbc) + || encAlg.Equals(NttObjectIdentifiers.IdCamellia256Cbc) + || encAlg.Equals(KisaObjectIdentifiers.IdSeedCbc) + || encAlg.Equals(OiwObjectIdentifiers.DesCbc)) { - if (encAlg.Equals(PkcsObjectIdentifiers.DesEde3Cbc) - || encAlg.Equals(AlgorithmIdentifierFactory.IDEA_CBC) - || encAlg.Equals(NistObjectIdentifiers.IdAes128Cbc) - || encAlg.Equals(NistObjectIdentifiers.IdAes192Cbc) - || encAlg.Equals(NistObjectIdentifiers.IdAes256Cbc) - || encAlg.Equals(NttObjectIdentifiers.IdCamellia128Cbc) - || encAlg.Equals(NttObjectIdentifiers.IdCamellia192Cbc) - || encAlg.Equals(NttObjectIdentifiers.IdCamellia256Cbc) - || encAlg.Equals(KisaObjectIdentifiers.IdSeedCbc) - || encAlg.Equals(OiwObjectIdentifiers.DesCbc)) - { - cipher.Init(forEncryption, new ParametersWithIV(encKey, - Asn1OctetString.GetInstance(sParams).GetOctets())); - } - else if (encAlg.Equals(AlgorithmIdentifierFactory.CAST5_CBC)) - { - Cast5CbcParameters cbcParams = Cast5CbcParameters.GetInstance(sParams); + cipher.Init(forEncryption, new ParametersWithIV(encKey, + Asn1OctetString.GetInstance(sParams).GetOctets())); + } + else if (encAlg.Equals(AlgorithmIdentifierFactory.CAST5_CBC)) + { + Cast5CbcParameters cbcParams = Cast5CbcParameters.GetInstance(sParams); - cipher.Init(forEncryption, new ParametersWithIV(encKey, cbcParams.GetIV())); - } - else if (encAlg.Equals(PkcsObjectIdentifiers.RC2Cbc)) - { - RC2CbcParameter cbcParams = RC2CbcParameter.GetInstance(sParams); + cipher.Init(forEncryption, new ParametersWithIV(encKey, cbcParams.GetIV())); + } + else if (encAlg.Equals(PkcsObjectIdentifiers.RC2Cbc)) + { + RC2CbcParameter cbcParams = RC2CbcParameter.GetInstance(sParams); - cipher.Init(forEncryption, new ParametersWithIV(new RC2Parameters(((KeyParameter)encKey).GetKey(), rc2Ekb[cbcParams.RC2ParameterVersion.IntValue]), cbcParams.GetIV())); - } - else - { - throw new InvalidOperationException("cannot match parameters"); - } + cipher.Init(forEncryption, + new ParametersWithIV( + new RC2Parameters( + ((KeyParameter)encKey).GetKey(), + RC2Ekb[cbcParams.RC2ParameterVersion.IntValue]), + cbcParams.GetIV())); } else { - if (encAlg.Equals(PkcsObjectIdentifiers.DesEde3Cbc) - || encAlg.Equals(AlgorithmIdentifierFactory.IDEA_CBC) - || encAlg.Equals(AlgorithmIdentifierFactory.CAST5_CBC)) - { - cipher.Init(forEncryption, new ParametersWithIV(encKey, new byte[8])); - } - else - { - cipher.Init(forEncryption, encKey); - } + throw new InvalidOperationException("cannot match parameters"); } - - return cipher; } + else + { + if (encAlg.Equals(PkcsObjectIdentifiers.DesEde3Cbc) + || encAlg.Equals(AlgorithmIdentifierFactory.IDEA_CBC) + || encAlg.Equals(AlgorithmIdentifierFactory.CAST5_CBC)) + { + cipher.Init(forEncryption, new ParametersWithIV(encKey, new byte[8])); + } + else + { + cipher.Init(forEncryption, encKey); + } + } + + return cipher; } private static BufferedBlockCipher CreateCipher(DerObjectIdentifier algorithm) -- cgit 1.4.1