private IMode ChooseMode(string mode, byte[] key, DesEngine engine) { IMode chosenMode; switch (mode) { case "ECB": chosenMode = new ECB(engine); break; case "CBC": chosenMode = new CBC(engine, key.ToArray()); break; case "CFB": chosenMode = new CFB(engine, key.ToArray()); break; case "OFB": chosenMode = new OFB(engine, key.ToArray()); break; default: throw new Exception("Неправильный режим"); } return(chosenMode); }
private Version(int versionNumber, int symbolSizeRows, int symbolSizeColumns, int dataRegionSizeRows, int dataRegionSizeColumns, ECBlocks ecBlocks) { this.versionNumber = versionNumber; this.symbolSizeRows = symbolSizeRows; this.symbolSizeColumns = symbolSizeColumns; this.dataRegionSizeRows = dataRegionSizeRows; this.dataRegionSizeColumns = dataRegionSizeColumns; this.ecBlocks = ecBlocks; // Calculate the total number of codewords int total = 0; int ecCodewords = ecBlocks.getECCodewords(); ECB[] ecbArray = ecBlocks.getECBlocks(); for (int i = 0; i < ecbArray.Length; i++) { ECB ecBlock = ecbArray[i]; total += ecBlock.getCount() * (ecBlock.getDataCodewords() + ecCodewords); } this.totalCodewords = total; }
private void Mapping(TableEntrie[] Data) { PanelSetting = new Point(0, 0); foreach (TableEntrie item in Data) { switch (item.Key) { case "SentinelPort": SentinelPort = UInt16.Parse(item.Value); break; case "NodeAPI.IPAddress": API.IPAddress = ECB.Decrypt(item.Value, KeyEncrypt); break; case "NodeAPI.Port": API.Port = ECB.Decrypt(item.Value, KeyEncrypt); break; case "MSSQL.Name": MSSQL.Name = ECB.Decrypt(item.Value, KeyEncrypt); break; case "MSSQL.ServerName": MSSQL.ServerName = ECB.Decrypt(item.Value, KeyEncrypt); break; case "MSSQL.Username": MSSQL.Username = ECB.Decrypt(item.Value, KeyEncrypt); break; case "MSSQL.Password": MSSQL.Password = ECB.Decrypt(item.Value, KeyEncrypt); break; case "PanelConfig.X": PanelSetting.X = Double.Parse(ECB.Decrypt(item.Value, KeyEncrypt)); break; case "PanelConfig.Y": PanelSetting.Y = Double.Parse(ECB.Decrypt(item.Value, KeyEncrypt)); break; } } }
public void MovedTo(ref UnityEcsEntityComponent entityComponent, ExclusiveGroupStruct previousGroup, EGID egid) { #if USE_ENTITY_MANAGER EntityManager.SetSharedComponentData(entityComponent.uecsEntity, new UECSSveltoGroupID(egid.groupID)); #else ECB.SetSharedComponent(entityComponent.uecsEntity, new UECSSveltoGroupID(egid.groupID)); #endif }
private Package ProcessMessage(Package pck) { // Console.WriteLine("#Get Message Client: "); // pck.Print(); Package response = new Package(); RSA certRSA = new RSA(certificateKey); string[] respCertf = certRSA.decrypt(pck.GetHeader("Certificate")).Split(';'); BigInteger n = BigInteger.Parse(respCertf[2]); BigInteger e = BigInteger.Parse(respCertf[3]); int idUser = GetUserByPublicKey(n, e); switch (pck.GetHeader("Command")) { case "Connect": UserData user = new UserData(n, e, pck.GetHeader("Username")); users.Add(user); response.SetHeader("Certificate", certificate); break; case "Disconnect": users.RemoveAt(idUser); break; case "DES": string desEnc = pck.GetHeader("DES Key"); users[idUser].desKey = rsa.decrypt(desEnc); RSA resRSA = new RSA(users[idUser].key); response.SetHeader("DES Key", resRSA.encrypt(ecbKey)); break; case "Message": Message msg = new Message(users[idUser], ECB.decrypt(pck.GetContent(), users[idUser].desKey)); bufferMessage.Add(msg); break; case "New Message": string lastIndex = ECB.decrypt(pck.GetContent(), users[idUser].desKey); int index = int.Parse(lastIndex); if (index < bufferMessage.Count) { response.SetContent(bufferMessage[index].user.username + " = " + bufferMessage[index].message); } break; } if (response.GetContent() != "") { response.SetContent(ECB.encrypt(response.GetContent(), ecbKey)); } return(response); }
/* executes class program */ public static void Main(String[] args) { ECB ecb = new ECB(); Console.Write("\n===== ECB (Electronic Code Book) =====\n"); Console.Write("\n\n== START PROGRAM ==\n\n"); ecb.driver(); Console.Write("\n\n== END PROGRAM ==\n\n"); Console.ReadLine(); }
public static void Main(String[] args) /* executes class program */ { ECB ecb = new ECB(); Console.Write("\n===== ECB (Electronic Code Book) =====\n"); Console.Write("\n\n== START PROGRAM ==\n\n"); ecb.driver(); Console.Write("\n\n== END PROGRAM ==\n\n"); Console.ReadLine(); }
public void Add(ref SpawnPointEntityComponent entityComponent, EGID egid) { Entity uecsEntity = ECB.Instantiate(entityComponent.prefabEntity); //SharedComponentData can be used to group the UECS entities exactly like the Svelto ones ECB.AddSharedComponent(uecsEntity, new UECSSveltoGroupID(egid.groupID)); ECB.AddComponent(uecsEntity, new UpdateUECSEntityAfterSubmission(egid)); ECB.SetComponent(uecsEntity, new Translation { Value = new float3(entityComponent.spawnPosition.x, entityComponent.spawnPosition.y , entityComponent.spawnPosition.z) }); }
private Version(int versionNumber, ECBlocks ecBlocks1, ECBlocks ecBlocks2, ECBlocks ecBlocks3, ECBlocks ecBlocks4) { this.versionNumber = versionNumber; this.ecBlocks = new ECBlocks[] { ecBlocks1, ecBlocks2, ecBlocks3, ecBlocks4 }; int total = 0; int ecCodewords = ecBlocks1.ECCodewordsPerBlock; ECB[] ecbArray = ecBlocks1.getECBlocks(); for (int i = 0; i < ecbArray.Length; i++) { ECB ecBlock = ecbArray[i]; //ECBlocks(7, new ECB(1, 19) 19+7=26 total += ecBlock.Count * (ecBlock.DataCodewords + ecCodewords); } this.totalCodewords = total; }
private static void ProcessECB(DataExporterFlags flags, Type t) { foreach (var file in flags.Paths) { var ecb = new ECB(File.ReadAllBytes(file)); var ft = Path.ChangeExtension(file, ".json"); var Entries = ecb.Cast(t); File.WriteAllText(ft, JsonConvert.SerializeObject(new { t.FullName, Entries }, Formatting.Indented, new StringEnumConverter())); Logger.Info("ECB", ft); } }
public void Decrypt_Decrypts10Bytes_Decrypted() { byte[] bytes = { 6, 6, 6, 6, 6, 6, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; var blockEncryptorMock = MockRepository.GenerateMock <ICipher>(); blockEncryptorMock.Expect(m => m.BlockSize).Return(16); blockEncryptorMock.Expect(m => m.Decrypt(bytes)).Return(bytes.Reverse().ToArray()); //blockEncryptorMock.Expect(m => m.DecryptBlock(padding)).Return(padding); ECB encryptor = new ECB(blockEncryptorMock, new PKCS7Padding()); MemoryStream input = new MemoryStream(bytes); MemoryStream output = new MemoryStream(); encryptor.Decrypt(input, output); CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, output.GetBuffer().SubArray(0, 10)); }
private Version(int versionNumber, int[] alignmentPatternCenters, ECBlocks ecBlocks1, ECBlocks ecBlocks2, ECBlocks ecBlocks3, ECBlocks ecBlocks4) { this.versionNumber = versionNumber; this.alignmentPatternCenters = alignmentPatternCenters; this.ecBlocks = new ECBlocks[] { ecBlocks1, ecBlocks2, ecBlocks3, ecBlocks4 }; int total = 0; int ecCodewords = ecBlocks1.ECCodewordsPerBlock; ECB[] ecbArray = ecBlocks1.getECBlocks(); for (int i = 0; i < ecbArray.Length; i++) { ECB ecBlock = ecbArray[i]; total += ecBlock.Count * (ecBlock.DataCodewords + ecCodewords); } this.totalCodewords = total; }
private void ECBTest(byte[] Key, byte[, ][] Input, byte[, ][] Output) { byte[] outBytes = new byte[16]; int index = 0; if (Key.Length == 24) { index = 2; } else if (Key.Length == 32) { index = 4; } using (ECB mode = new ECB(new RHX())) { mode.Initialize(true, new KeyParams(Key)); for (int i = 0; i < 4; i++) { mode.Transform(Input[index, i], outBytes); if (Evaluate.AreEqual(outBytes, Output[index, i]) == false) { throw new Exception("ECB Mode: Encrypted arrays are not equal!"); } } } index++; using (ECB mode = new ECB(new RHX())) { mode.Initialize(false, new KeyParams(Key)); for (int i = 0; i < 4; i++) { mode.Transform(Input[index, i], outBytes); if (Evaluate.AreEqual(outBytes, _output[index, i]) == false) { throw new Exception("ECB Mode: Decrypted arrays are not equal!"); } } } }
public void Encrypt_EncryptsBlock_Encrypted() { byte[] bytes = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; byte[] padding = { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 }; var blockEncryptorMock = MockRepository.GenerateMock <ICipher>(); blockEncryptorMock.Expect(m => m.BlockSize).Return(16); blockEncryptorMock.Expect(m => m.Encrypt(bytes)).Return(bytes.Reverse().ToArray()); blockEncryptorMock.Expect(m => m.Encrypt(padding)).Return(padding); ECB encryptor = new ECB(blockEncryptorMock, new PKCS7Padding()); MemoryStream input = new MemoryStream(bytes); MemoryStream output = new MemoryStream(); encryptor.Encrypt(input, output); CollectionAssert.AreEqual(new byte[] { 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 }, output.GetBuffer().SubArray(0, 32)); }
public void Save() { TypeSFO FileConfig = new TypeSFO(); FileConfig.Param("SentinelPort", SentinelPort); FileConfig.Param("NodeAPI.IPAddress", ECB.Encrypt(API.IPAddress, KeyEncrypt)); FileConfig.Param("NodeAPI.Port", ECB.Encrypt(API.Port, KeyEncrypt)); FileConfig.Param("MSSQL.Name", ECB.Encrypt(MSSQL.Name, KeyEncrypt)); FileConfig.Param("MSSQL.ServerName", ECB.Encrypt(MSSQL.ServerName, KeyEncrypt)); FileConfig.Param("MSSQL.Username", ECB.Encrypt(MSSQL.Username, KeyEncrypt)); FileConfig.Param("MSSQL.Password", ECB.Encrypt(MSSQL.Password, KeyEncrypt)); FileConfig.Param("PanelConfig.X", ECB.Encrypt(PanelSetting.X.ToString(), KeyEncrypt)); FileConfig.Param("PanelConfig.Y", ECB.Encrypt(PanelSetting.Y.ToString(), KeyEncrypt)); FileConfig.SaveAs(Module.TravoxSentinel + Module.File_Config); }
private Package SendCommand(Package pck, int port) { TcpClient client = new TcpClient(); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); client.Connect(serverEndPoint); NetworkStream clientStream = client.GetStream(); ASCIIEncoding encoder = new ASCIIEncoding(); if (pck.GetContent() != "") { pck.SetContent(ECB.encrypt(pck.GetContent(), ecbKey)); } String s = pck.GetString(); byte[] buffer = encoder.GetBytes(s); clientStream.Write(buffer, 0, buffer.Length); clientStream.Flush(); byte[] bufferResponse = new byte[4096]; clientStream.Read(bufferResponse, 0, 4096); String response = encoder.GetString(bufferResponse).Replace("\0", ""); client.Close(); Package resp = new Package(); resp.SetByString(response); //Console.WriteLine("#Response From Server: "); //resp.Print(); if (port == 1234 && resp.GetContent() != "") { resp.SetContent(ECB.decrypt(resp.GetContent(), publicDes)); } return(resp); }
public ServerChat() { listening = false; bufferMessage.Clear(); rsa.GenerateKey(); ecbKey = ECB.GenerateKey(); Package verf = new Package(); verf.SetHeader("Command", "Register"); verf.SetHeader("Public Key n", rsa.Key.n.ToString()); verf.SetHeader("Public Key e", rsa.Key.e.ToString()); Package certf = SendCommand(verf, 2745); BigInteger n = BigInteger.Parse(certf.GetHeader("Public Key n")); BigInteger e = BigInteger.Parse(certf.GetHeader("Public Key e")); certificateKey = new RSAKey(); certificateKey.d = e; certificateKey.n = n; certificate = certf.GetContent(); }
public Chat() { InitializeComponent(); rsa.GenerateKey(); ecbKey = ECB.GenerateKey(); Package verf = new Package(); verf.SetHeader("Command", "Register"); verf.SetHeader("Public Key n", rsa.Key.n.ToString()); verf.SetHeader("Public Key e", rsa.Key.e.ToString()); Package certf = SendCommand(verf, 2745); BigInteger n = BigInteger.Parse(certf.GetHeader("Public Key n")); BigInteger e = BigInteger.Parse(certf.GetHeader("Public Key e")); certificateKey = new RSAKey(); certificateKey.d = e; certificateKey.n = n; certificate = certf.GetContent(); Console.WriteLine("Register Certificate: " + certificate); //certf.Print(); }
internal ECBlocks(int ecCodewords, ECB ecBlocks1, ECB ecBlocks2) { this.ecCodewords = ecCodewords; this.ecBlocks = new ECB[] { ecBlocks1, ecBlocks2 }; }
private async void buttonEncode_Click(object sender, EventArgs e) { if ((textBoxKey.Text == "" && byteKey == null) || (textBoxC0.Text == "" && bytec0 == null && !radioButtonECB.Checked)) { MessageBox.Show("Не введены данные"); return; } if (openFileDialog.ShowDialog() == DialogResult.Cancel) { return; } var filePath = openFileDialog.FileName; Task read = Task.Run(() => { try { text = File.ReadAllBytes(filePath); } catch (OutOfMemoryException ex) { MessageBox.Show(ex.Message); return; } }); read.Wait(); if (text == null) { MessageBox.Show("Нет текста"); return; } ICoder modeWork = null; if (byteKey == null) { byteKey = Encoding.Default.GetBytes(textBoxKey.Text); } var addByte = new List <byte>(); for (int i = byteKey.Length; i < blockKeySize; i++) { addByte.Add(byteKey[i % byteKey.Length]); } byteKey = byteKey.Concat(addByte.ToArray()).ToArray(); if (!radioButtonECB.Checked) { if (bytec0 == null) { bytec0 = Encoding.Default.GetBytes(textBoxC0.Text); } addByte = new List <byte>(); for (int i = bytec0.Length; i < blockSize; i++) { addByte.Add(bytec0[i % bytec0.Length]); } bytec0 = bytec0.Concat(addByte.ToArray()).ToArray(); } if (text.Length % blockSize != 0) { addByte = new List <byte>(); for (int i = 0; i < blockSize - text.Length % blockSize; i++) { addByte.Add(0); } text = text.Concat(addByte.ToArray()).ToArray(); } uint[] T = new uint[14]; for (int i = 0; i < byteKey.Length; i += subblockSize) { T[i / subblockSize] = BitConverter.ToUInt32(byteKey.Skip(i).Take(subblockSize).ToArray(), 0); } var fileName = Path.GetFileName(openFileDialog.FileName); FormFile wind = new FormFile(fileName, true); var mars = new Mars(T); if (radioButtonECB.Checked) { modeWork = new ECB(mars, wind); } else if (radioButtonCBC.Checked) { modeWork = new CBC(mars, bytec0, wind); } else if (radioButtonCFB.Checked) { modeWork = new CFB(mars, bytec0, wind); } else if (radioButtonOFB.Checked) { modeWork = new OFB(mars, bytec0, wind); } wind.Show(); byte[] result = new byte[] { }; if (radioButtonECB.Checked) { int processorCount = 2; Task <byte[]>[] allTasks = new Task <byte[]> [processorCount]; for (int i = 0; i < processorCount; i++) { int indexI = i; // т.к. значение i может поменяться при EndInvoke allTasks[indexI] = Task.Run(() => modeWork.Encode(text.Skip(text.Length / processorCount * i).Take(text.Length / processorCount).ToArray())); Thread.Sleep(100); } await Task.WhenAll(allTasks); for (int i = 0; i < processorCount; i++) { result = result.Concat(allTasks[i].Result).ToArray(); } } else { result = await modeWork.Encode(text); } wind.progressBar.Value = 100; using (var fs = System.IO.File.Create(Application.StartupPath + "\\(MARS)" + fileName)) { await fs.WriteAsync(result, 0, result.Count()); fs.Flush(); } }
internal ECBlocks(int ecCodewords, ECB ecBlocks) { this.ecCodewords = ecCodewords; this._ecBlocksValue = new ECB[] { ecBlocks }; }
public void testECBDates() { // Testing ECB dates List <Date> knownDates = ECB.knownDates(); if (knownDates.empty()) { QAssert.Fail("Empty EBC date vector"); } int n = ECB.nextDates(Date.minDate()).Count; if (n != knownDates.Count) { QAssert.Fail("NextDates(minDate) returns " + n + " instead of " + knownDates.Count + " dates"); } Date previousEcbDate = Date.minDate(), currentEcbDate, ecbDateMinusOne; for (int i = 0; i < knownDates.Count; ++i) { currentEcbDate = knownDates[i]; if (!ECB.isECBdate(currentEcbDate)) { QAssert.Fail(currentEcbDate + " fails isECBdate check"); } ecbDateMinusOne = currentEcbDate - 1; if (ECB.isECBdate(ecbDateMinusOne)) { QAssert.Fail(ecbDateMinusOne + " fails isECBdate check"); } if (ECB.nextDate(ecbDateMinusOne) != currentEcbDate) { QAssert.Fail("Next EBC date following " + ecbDateMinusOne + " must be " + currentEcbDate); } if (ECB.nextDate(previousEcbDate) != currentEcbDate) { QAssert.Fail("Next EBC date following " + previousEcbDate + " must be " + currentEcbDate); } previousEcbDate = currentEcbDate; } Date knownDate = knownDates.First(); ECB.removeDate(knownDate); if (ECB.isECBdate(knownDate)) { QAssert.Fail("Unable to remove an EBC date"); } ECB.addDate(knownDate); if (!ECB.isECBdate(knownDate)) { QAssert.Fail("Unable to add an EBC date"); } }
internal ECBlocks(int ecCodewordsPerBlock, ECB ecBlocks) { this.ecCodewordsPerBlock = ecCodewordsPerBlock; this.ecBlocks = new[] {ecBlocks}; }
internal ECBlocks(int ecCodewordsPerBlock, ECB ecBlocks1, ECB ecBlocks2) { this.ecCodewordsPerBlock = ecCodewordsPerBlock; this.ecBlocks = new ECB[]{ecBlocks1, ecBlocks2}; }
internal ECBlocks(int ecCodewords, ECB ecBlocks1, ECB ecBlocks2) { this.ecCodewords = ecCodewords; this._ecBlocksValue = new ECB[] { ecBlocks1, ecBlocks2 }; }
public ECBlocks(int ecCodewords, ECB ecBlocks) { this.ecCodewords = ecCodewords; this.ecBlocks = new ECB[] { ecBlocks }; }
internal ECBlocks(int ecCodewords, ECB ecBlocks) { this.ecCodewords = ecCodewords; this.ecBlocks = new ECB[]{ecBlocks}; }
internal ECBlocks(int ecCodewords, ECB ecBlocks1, ECB ecBlocks2) { this.ecCodewords = ecCodewords; ecBlocks = new[] {ecBlocks1, ecBlocks2}; }
internal ECBlocks(int ecCodewordsPerBlock, ECB ecBlocks1, ECB ecBlocks2) { this.ecCodewordsPerBlock = ecCodewordsPerBlock; this.ecBlocks = new ECB[] { ecBlocks1, ecBlocks2 }; }
public ECBlocks(int ecCodewordsPerBlock, ECB ecBlocks) { this.ecCodewordsPerBlock = ecCodewordsPerBlock; this.ecBlocks = new ECB[] { ecBlocks }; }
public async Task <ActionResult <string> > Post(IFormFile message, KACD kacd) { byte[] byteMessage = null; if (message == null) { ModelState.AddModelError("Error", "Некорректные параметры"); return(BadRequest(ModelState)); } ICoder modeWork = null; try { if (kacd.key == null || kacd.mode == null) { throw new FormatException(); } var byteKey = Encoding.Default.GetBytes(kacd.key); byte[] bytec0 = null; if (kacd.mode != "ecb") { if (kacd.c0 == null) { throw new FormatException(); } bytec0 = Encoding.Default.GetBytes(kacd.c0); if (bytec0.Length != 8) { throw new FormatException(); } } if (byteKey.Length != 8) { throw new FormatException(); } using (var ms = new MemoryStream()) { message.CopyTo(ms); byteMessage = ms.ToArray(); } if (byteMessage.Length % 8 != 0) { var addByte = new List <byte>(); for (int i = 0; i < 8 - byteMessage.Length % 8; i++) { addByte.Add(0); } byteMessage = byteMessage.Concat(addByte.ToArray()).ToArray(); } var des = new DES(BitConverter.ToUInt64(byteKey)); switch (kacd.mode) { case "ecb": modeWork = new ECB(des); break; case "cbc": modeWork = new CBC(des, bytec0); break; case "cfb": modeWork = new CFB(des, bytec0); break; case "ofb": modeWork = new OFB(des, bytec0); break; } } catch (FormatException) { ModelState.AddModelError("Error", "Некорректные параметры"); return(BadRequest(ModelState)); } var result = kacd.decode != null?modeWork.Decode(byteMessage) : modeWork.Encode(byteMessage); var path = "(DES)" + message.FileName; using (var fs = System.IO.File.Create(_environment.WebRootPath + "/" + path)) { fs.Write(result, 0, result.Count()); fs.Flush(); } return(path); }
public void Remove(ref DOTSEntityComponent entityComponent, EGID egid) { ECB.DestroyEntity(entityComponent.dotsEntity); }
internal ECBlocks(int ecCodewordsPerBlock, ECB ecBlocks1, ECB ecBlocks2) { ECCodewordsPerBlock = ecCodewordsPerBlock; GetECBlocks = new[] { ecBlocks1, ecBlocks2 }; }