Esempio n. 1
0
        internal static int CheckIv(AlgorithmMode mode, byte[] iv, int defaulIvSize)
        {
            switch (mode)
            {
            case AlgorithmMode.CBC:
            case AlgorithmMode.CFB128:
            case AlgorithmMode.CFB8:
            case AlgorithmMode.CFB64:
            case AlgorithmMode.OFB128:
            case AlgorithmMode.OFB64:
                if (iv != null && iv.Length != defaulIvSize)
                {
                    throw new ArgumentException("IV must be " + defaulIvSize + " bytes long");
                }
                break;

            case AlgorithmMode.CTR:
                if (iv != null && iv.Length > defaulIvSize)
                {
                    throw new ArgumentException("CTR IV must be less than " + defaulIvSize + " bytes long");
                }
                break;
            }

            return(defaulIvSize);
        }
Esempio n. 2
0
        internal static IBufferedCipher CreateBufferedCipher(string name, AlgorithmMode algorithmMode, IParametersWithIV <IParameters <Algorithm>, Algorithm> parameters, bool forEncryption, IEngineProvider <Internal.IBlockCipher> cipherProvider)
        {
            Internal.IBlockCipher baseCipher = cipherProvider.CreateEngine(GetUsage(forEncryption, algorithmMode));
            Internal.IBlockCipher cipher;

            switch (algorithmMode)
            {
            case AlgorithmMode.CBC:
                cipher = new CbcBlockCipher(baseCipher);
                break;

            case AlgorithmMode.CS1:
                return(new NistCtsBlockCipher(NistCtsBlockCipher.CS1, baseCipher));

            case AlgorithmMode.CS2:
                return(new NistCtsBlockCipher(NistCtsBlockCipher.CS2, baseCipher));

            case AlgorithmMode.CS3:
                return(new NistCtsBlockCipher(NistCtsBlockCipher.CS3, baseCipher));

            case AlgorithmMode.CFB8:
                cipher = new CfbBlockCipher(baseCipher, 8);
                break;

            case AlgorithmMode.CFB64:
                cipher = new CfbBlockCipher(baseCipher, 64);
                break;

            case AlgorithmMode.CFB128:
                cipher = new CfbBlockCipher(baseCipher, 128);
                break;

            case AlgorithmMode.OpenPGPCFB:
                cipher = new OpenPgpCfbBlockCipher(baseCipher);
                break;

            case AlgorithmMode.OFB64:
                cipher = new OfbBlockCipher(baseCipher, 64);
                break;

            case AlgorithmMode.OFB128:
                cipher = new OfbBlockCipher(baseCipher, 128);
                break;

            case AlgorithmMode.CTR:
                cipher = new SicBlockCipher(baseCipher);
                break;

            default:
                throw new ArgumentException("Unknown algorithm mode passed to " + name + ".Provider: " + algorithmMode);
            }

            return(new BufferedBlockCipher(cipher));
        }
Esempio n. 3
0
        public bool CheckNeighbourhoods(int i, int j, AlgorithmMode algorithmMode, BoundaryConditionType boundaryConditionType = BoundaryConditionType.Periodic)
        {
            Dictionary <int, int> grainIds = new Dictionary <int, int>();
            int grainId = -1;

            switch (algorithmMode)
            {
            case AlgorithmMode.TheGameOfLife:
                grainId = this.CheckConditions(this.GetAlgorithmConditions(i, j, algorithmMode), this.GetCorrectGridElement(i, j), boundaryConditionType);

                if (this.CountNeighbourhoods(i, j, boundaryConditionType) == 3 || (this.GetCorrectValue(i, j, boundaryConditionType) && this.CountNeighbourhoods(i, j, boundaryConditionType) == 2))
                {
                    grainId = this.CheckConditions(this.GetAlgorithmConditions(i, j, algorithmMode), this.GetCorrectGridElement(i, j), boundaryConditionType);

                    if (grainId == -1)
                    {
                        return(false);
                    }

                    this.GetCorrectGridElement(i, j, boundaryConditionType).SetGrainId(grainId);

                    return(true);
                }

                return(false);

            case AlgorithmMode.GrainGrowthPegRandom:
                algorithmMode = new Random().Next(0, 2) == 1 ? AlgorithmMode.GrainGrowthPegLeft : AlgorithmMode.GrainGrowthPegRight;
                break;

            case AlgorithmMode.GrainGrowthHexRandom:
                algorithmMode = new Random().Next(0, 2) == 1 ? AlgorithmMode.GrainGrowthHexLeft : AlgorithmMode.GrainGrowthHexRight;
                break;
            }

            // all grain growth algorithms

            grainId = this.CheckConditions(this.GetAlgorithmConditions(i, j, algorithmMode), this.GetCorrectGridElement(i, j), boundaryConditionType);

            if (grainId == -1)
            {
                return(false);
            }

            this.GetCorrectGridElement(i, j, boundaryConditionType).SetGrainId(grainId);

            return(true);
        }
Esempio n. 4
0
        internal static IAeadBlockCipher CreateAeadCipher(string name, AlgorithmMode algorithmMode, IParametersWithIV <IParameters <Algorithm>, Algorithm> parameters, bool forEncryption, IEngineProvider <Internal.IBlockCipher> cipherProvider)
        {
            Internal.IBlockCipher baseCipher = cipherProvider.CreateEngine(GetUsage(forEncryption, algorithmMode));

            switch (algorithmMode)
            {
            case AlgorithmMode.CCM:
                return(new CcmBlockCipher(baseCipher));

            case AlgorithmMode.GCM:
                return(new GcmBlockCipher(baseCipher));

            default:
                throw new ArgumentException("Unknown algorithm mode passed to " + name + ".Provider: " + algorithmMode);
            }
        }
Esempio n. 5
0
        internal static IBufferedCipher CreateBufferedCipher(string name, AlgorithmMode algorithmMode, IParameters <Algorithm> parameters, Org.BouncyCastle.Crypto.Internal.IBlockCipher baseCipher)
        {
            Org.BouncyCastle.Crypto.Internal.IBlockCipher cipher;

            switch (algorithmMode)
            {
            case AlgorithmMode.ECB:
                cipher = baseCipher;
                break;

            default:
                throw new ArgumentException("Unknown algorithm mode passed to " + name + ".Provider: " + algorithmMode);
            }

            return(new BufferedBlockCipher(cipher));
        }
Esempio n. 6
0
        private static EngineUsage GetUsage(bool forEncryption, AlgorithmMode algorithmMode)
        {
            switch (algorithmMode)
            {
            case AlgorithmMode.OFB64:
            case AlgorithmMode.OFB128:
            case AlgorithmMode.CFB8:
            case AlgorithmMode.CFB64:
            case AlgorithmMode.CFB128:
            case AlgorithmMode.OpenPGPCFB:
            case AlgorithmMode.CTR:
            case AlgorithmMode.GCM:
            case AlgorithmMode.CCM:
                return(EngineUsage.ENCRYPTION);
            }

            return(forEncryption ? EngineUsage.ENCRYPTION : EngineUsage.DECRYPTION);
        }
 public AlgorithmControls(AlgorithmMode algorithmMode, Grid parametersGrid, Button processButton, Image originalImage, Image watermarkImage, Image watermarkedImage,
                          Action <System.Drawing.Bitmap> onUse, TabControl tabControl, TabItem resultTab, int resultTabIndex, Grid resultGrid, ScrollViewer resultScrollViewer, Button closeButton, Button cancelButton)
 {
     AlgorithmMode      = algorithmMode;
     ParametersGrid     = parametersGrid;
     ProcessButton      = processButton;
     OriginalImage      = originalImage;
     WatermarkImage     = watermarkImage;
     WatermarkedImage   = watermarkedImage;
     OnUse              = onUse;
     TabControl         = tabControl;
     ResultTab          = resultTab;
     ResultTabIndex     = resultTabIndex;
     ResultGrid         = resultGrid;
     ResultScrollViewer = resultScrollViewer;
     CloseButton        = closeButton;
     CancelButton       = cancelButton;
 }
Esempio n. 8
0
        private void Neighboorhood_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (neighboorhood.Text)
            {
            case "thegameoflife":
                this.algorithmMode = AlgorithmMode.TheGameOfLife;
                break;

            case "neumann":
                this.algorithmMode = AlgorithmMode.GrainGrowthNeumann;
                break;

            case "moore":
                this.algorithmMode = AlgorithmMode.GrainGrowthMoore;
                break;

            case "hexleft":
                this.algorithmMode = AlgorithmMode.GrainGrowthHexLeft;
                break;

            case "hexright":
                this.algorithmMode = AlgorithmMode.GrainGrowthHexRight;
                break;

            case "hexrandom":
                this.algorithmMode = AlgorithmMode.GrainGrowthHexRandom;
                break;

            case "pexleft":
                this.algorithmMode = AlgorithmMode.GrainGrowthPegLeft;
                break;

            case "pexright":
                this.algorithmMode = AlgorithmMode.GrainGrowthPegRight;
                break;

            case "pexrandom":
                this.algorithmMode = AlgorithmMode.GrainGrowthPegRandom;
                break;
            }
        }
Esempio n. 9
0
        public SymmetricKeyProvider(AlgorithmName name = AlgorithmName.Aes, AlgorithmMode mode = AlgorithmMode.Cbc, AlgorithmPadding padding = AlgorithmPadding.Pkcs7, byte[] salt = null)
        {
            Name    = name;
            Mode    = mode;
            Padding = padding;
            Salt    = salt ?? DefaultSalt;
            switch (Name)
            {
            case AlgorithmName.Aes:
                _alg = Aes.Create();
                break;

            case AlgorithmName.TripleDes:
                _alg = TripleDES.Create();
                break;
            }
            switch (Mode)
            {
            case AlgorithmMode.Cbc:
                _alg.Mode = CipherMode.CBC;
                break;

            case AlgorithmMode.Ecb:
                _alg.Mode = CipherMode.ECB;
                break;
            }
            switch (Padding)
            {
            case AlgorithmPadding.None:
                _alg.Padding = PaddingMode.None;
                break;

            case AlgorithmPadding.Pkcs7:
                _alg.Padding = PaddingMode.PKCS7;
                break;

            case AlgorithmPadding.Zeros:
                _alg.Padding = PaddingMode.Zeros;
                break;
            }
        }
Esempio n. 10
0
        internal static IWrapper CreateWrapper(string name, AlgorithmMode algorithmMode, bool useInverse, bool forWrapping, IEngineProvider <Internal.IBlockCipher> baseCipherProvider)
        {
            Internal.IBlockCipher baseCipher = baseCipherProvider.CreateEngine(GetWrapUsage(useInverse, forWrapping));
            IWrapper cipher;

            switch (algorithmMode)
            {
            case AlgorithmMode.WRAP:
                cipher = new SP80038FWrapEngine(baseCipher, useInverse);
                break;

            case AlgorithmMode.WRAPPAD:
                cipher = new SP80038FWrapWithPaddingEngine(baseCipher, useInverse);
                break;

            default:
                throw new ArgumentException("Unknown wrapper algorithm passed to " + name + ".Provider: " + algorithmMode);
            }

            cipher.Init(forWrapping, null);

            return(cipher);
        }
Esempio n. 11
0
 internal FipsAlgorithm(FipsAlgorithm algorithm, AlgorithmMode mode) : base(algorithm.Name, mode)
 {
 }
Esempio n. 12
0
 internal FipsAlgorithm(string name, AlgorithmMode mode) : base(name, mode)
 {
 }
Esempio n. 13
0
 internal DigestAlgorithm(string name, AlgorithmMode mode) : base(name, mode)
 {
 }
 internal static AlgorithmModeDetails GetDetails(AlgorithmMode mode)
 {
     return((AlgorithmModeDetails)detailsTable[mode]);
 }
 private AlgorithmModeDetails(AlgorithmMode mode, string code, bool expectsIV)
 {
     this.mode      = mode;
     this.code      = code;
     this.expectsIV = expectsIV;
 }
Esempio n. 16
0
        // Neighbourhoods

        private int[,] GetAlgorithmConditions(int i, int j, AlgorithmMode algorithmMode)
        {
            switch (algorithmMode)
            {
            case AlgorithmMode.TheGameOfLife: {
                int[,] conditions =
                {
                    { i - 1, j - 1 },
                    { i - 1, j     },
                    { i - 1, j + 1 },
                    { i,     j - 1 },
                    { i,     j + 1 },
                    { i + 1, j - 1 },
                    { i + 1, j     },
                    { i + 1, j + 1 }
                };

                return(conditions);
            }

            case AlgorithmMode.GrainGrowthNeumann: {
                int[,] conditions =
                {
                    { i - 1, j     },
                    { i,     j - 1 },
                    { i,     j + 1 },
                    { i + 1, j     }
                };

                return(conditions);
            }

            case AlgorithmMode.GrainGrowthMoore: {
                int[,] conditions =
                {
                    { i - 1, j - 1 },
                    { i - 1, j     },
                    { i - 1, j + 1 },
                    { i,     j - 1 },
                    { i,     j + 1 },
                    { i + 1, j - 1 },
                    { i + 1, j     },
                    { i + 1, j + 1 }
                };

                return(conditions);
            }

            case AlgorithmMode.GrainGrowthHexLeft: {
                int[,] conditions =
                {
                    { i - 1, j - 1 },
                    { i - 1, j     },
                    { i,     j - 1 },
                    { i,     j + 1 },
                    { i + 1, j     },
                    { i + 1, j + 1 }
                };

                return(conditions);
            }

            case AlgorithmMode.GrainGrowthHexRight: {
                int[,] conditions =
                {
                    { i - 1, j     },
                    { i - 1, j + 1 },
                    { i,     j - 1 },
                    { i,     j + 1 },
                    { i + 1, j - 1 },
                    { i + 1, j     }
                };

                return(conditions);
            }

            case AlgorithmMode.GrainGrowthPegLeft: {
                int[,] conditions =
                {
                    { i - 1, j - 1 },
                    { i - 1, j     },
                    { i - 1, j + 1 },
                    { i,     j - 1 },
                    { i,     j + 1 }
                };

                return(conditions);
            }

            case AlgorithmMode.GrainGrowthPegRight: {
                int[,] conditions =
                {
                    { i,     j - 1 },
                    { i,     j + 1 },
                    { i + 1, j - 1 },
                    { i + 1, j     },
                    { i + 1, j + 1 }
                };

                return(conditions);
            }
            }

            return(null);
        }
Esempio n. 17
0
 internal Algorithm(string name, AlgorithmMode mode)
 {
     this.name = name;
     this.mode = mode;
 }
Esempio n. 18
0
 internal Algorithm(Algorithm algorithm, AlgorithmMode mode) : this(algorithm.Name, mode)
 {
 }
Esempio n. 19
0
 internal GeneralAlgorithm(Algorithm algorithm, AlgorithmMode mode) : base(algorithm.Name, mode)
 {
 }
Esempio n. 20
0
        internal static bool isBlockCipherMode(Algorithm algorithm)
        {
            AlgorithmMode mode = algorithm.Mode;

            return(mode == AlgorithmMode.ECB || mode == AlgorithmMode.CBC);
        }
Esempio n. 21
0
        // Cells operations

        public void RecalculateCells(BoundaryConditionType boundaryConditionType = BoundaryConditionType.Periodic, AlgorithmMode algorithMode = AlgorithmMode.TheGameOfLife)
        {
            int[] gridSize = this.GetGridSize();
            Grid  newGrid  = new Grid(gridSize[0], gridSize[1]);

            for (int i = 0; i < gridSize[0]; i++)
            {
                for (int j = 0; j < gridSize[1]; j++)
                {
                    newGrid.SetCorrectGridElement(i, j, this.CheckNeighbourhoods(i, j, algorithMode, boundaryConditionType));
                    newGrid.GetCorrectGridElement(i, j, boundaryConditionType).SetGrainId(this.GetCorrectGridElement(i, j, boundaryConditionType).GetGrainId());
                }
            }

            this.cells = newGrid.cells;
        }