public void Encode() { crypter = DelastellaCypherFactory.Create(Key.UnifyKey(), SelectedCypherType); MatrixValues = crypter.MatrixGenerator.PobliusMatrix; Result = crypter.Encode(TextToEncodeDecode); MatrixVisible = Visibility.Visible; }
public AccountController( IHostingEnvironment environment, IIdentityServerInteractionService interaction, UserManager userManager, SignInManager signInManager, MessagingService messagingService, ILoggerFactory loggerFactory, ICypher cypher, IHttpContextAccessor httpContextAccessor, AegisTenantResolver aegisTenantResolver, DirectoryManager directoryManager ) { _logger = loggerFactory.CreateLogger <AccountController>(); _environment = environment; _interaction = interaction; _userManager = userManager; _signInManager = signInManager; _messaging = messagingService; _cypher = cypher; _httpContextAccessor = httpContextAccessor; _aegisTenantResolver = aegisTenantResolver; _directoryManager = directoryManager; }
public static void Decrypt( this ICypher self, Stream cypher, IRng key, Stream outPlain) { if (!cypher.CanRead) { throw new InvalidOperationException($"Input stream does not allow read"); } if (!outPlain.CanWrite) { throw new InvalidOperationException($"Output stream does not allow write"); } byte[] buff = new byte[1024]; int read; while ((read = cypher.Read(buff, 0, 1024)) != 0) { byte[] bytesRead = buff.Take(read).ToArray(); byte[] bytesWrite = self.Decrypt(bytesRead, key); outPlain.Write(bytesWrite, 0, bytesWrite.Length); } }
private static void EncryptDecryptPlainText(Options ops, ICypher cypher) { string originalText = File.ReadAllText(ops.SourceFilePath); EndOfLine endOfLine; if (!AlphabeticCypher.IsTextPlain(originalText, out endOfLine)) { throw new InvalidOperationException("Text is not considered plain text"); } cypher.Alphabet = AlphabeticCypher.GetPlainTextAlphabet(endOfLine); if (ops.Action == Action.Encrypt) { string plainText = originalText; string cryptoText = cypher.Encrypt(plainText, ops.EncryptionKey); File.WriteAllText(ops.TargetFilePath, cryptoText); } else { string cryptoText = originalText; string plainText = cypher.Decrypt(cryptoText, ops.EncryptionKey); File.WriteAllText(ops.TargetFilePath, plainText); } }
private static void InitTest(ICypher encrypt, out byte[] plain, out byte[] key, out byte[] originalCypher) { _RNG = new Random(RSeed); plain = RandomText(PlainSize, Type); key = RandomText(KeySize); originalCypher = encrypt.Encrypt(plain, key); }
public UpdateWebsiteHandler(IWebsiteRepository repository, IUnitOfWork unitOfWork, ICypher cypher) { _repository = repository; _unitOfWork = unitOfWork; _cypher = cypher; }
public EncryptTest(ICypher e, string name, char[] allowedAlpha, TestType types) { Enc = e; Name = name; Alpha = allowedAlpha; AllowedTypes = types; Keys = null; Texts = null; }
public SaltedCypher(ICypher inner, int saltLength = 20) { if (inner == null) { throw new ArgumentNullException(nameof(inner)); } _cypher = inner; _saltLength = saltLength; Shaker = new Shaker(); }
/// <summary> /// Contructs basic data for file en-/decryption. This includes a temporary file to save data. /// </summary> /// <param name="filePath">File path to the file, which will be en-/decrypted.</param> /// <param name="key">The key for the Cypher</param> /// <param name="method">The method of en-/decryption</param> public SecureFile(string key, Cypher method = Cypher.AES) { switch (method) { case Cypher.AES: _cypher = new AES(ToByte(key)); _cryptType = ToByte(_secureFileType + "AES"); break; default: throw new NotImplementedException(); } }
private static void EncryptDecryptBinary(Options ops, ICypher cypher) { byte[] originalText = File.ReadAllBytes(ops.SourceFilePath); if (ops.Action == Action.Encrypt) { byte[] plainText = originalText; byte[] cryptoText = cypher.Encrypt( plainText, ops.EncryptionKey.ToByteArray()); File.WriteAllBytes(ops.TargetFilePath, cryptoText); } else { byte[] cryptoText = originalText; byte[] plainText = cypher.Decrypt( cryptoText, ops.EncryptionKey.ToByteArray()); File.WriteAllBytes(ops.TargetFilePath, plainText); } }
/// <summary> /// Diffusion is a measure of how each bit of the plain text affects the bits of the cypher text. /// Since we flip one bit of the plain text, the minimum is 1 / text-length (in bits) /// </summary> public static float TestForDifussion(ICypher encrypt, int testCount) { byte[] plain; byte[] key; byte[] originalCypher; InitTest(encrypt, out plain, out key, out originalCypher); List <byte[]> plainFlips = CreateFlips(plain, testCount); List <byte[]> cypherFlips = plainFlips.Select(pf => encrypt.Encrypt(pf, key)).ToList(); var scores = new List <float>(); foreach (var cypherFlip in cypherFlips) { scores.Add(PercentFlips(originalCypher, cypherFlip)); } float ave = scores.Average(); Debug.Assert(ave >= 0f && ave <= 1f, "Percentage is not within 0 and 1!"); //The further from 50%, the worse. return((0.5f - Math.Abs(0.5f - ave)) / 0.5f); }
private void ChangeCurrentCipher() { try { var selectedTab = tabControl1.SelectedTab; switch (selectedTab.Text) { case @"Шифр Цезаря": cypher = new CaesarCypher(GetM()); break; case @"Шифр Виженера": cypher = new VigenerCypher(GetKeyWord()); break; default: throw new Exception("Не найден текущий вид шифрования"); } } catch (Exception e) { MessageBox.Show(e.Message); } }
/// <summary> /// The distribution should be as spread as possible /// </summary> public static float TestForDistribution(ICypher enc) { byte[] plain; byte[] key; byte[] cypher; float[] scores = new float[TryCount]; float[] plainChis = new float[TryCount]; float[] cypherChis = new float[TryCount]; for (int ii = 0; ii < TryCount; ++ii) { plain = RandomText(PlainSize, Type); key = RandomText(KeySize); cypher = enc.Encrypt(plain, key); double plainChi = Math.Sqrt(Stats.ChiSquared(GetBlockCount(plain))); double cypherChi = Math.Sqrt(Stats.ChiSquared(GetBlockCount(cypher))); plainChis[ii] = (float)plainChi; cypherChis[ii] = (float)cypherChi; // Prevent division by zero when both plain and cypher have uniform distribution if (plainChi == 0) { plainChi = float.MinValue; if (cypherChi == 0) { cypherChi = float.MinValue; } } //if cypherChi == 0, score is 100% (uniform distribution, little statistical data) //if cypherChi == plainChi, score is 0% (same distribution as plain-text, not good at all) //if cypherChi > plainChi, score is negative (cypher has worse distribution than plain, probably inconclusive) scores[ii] = (float)((plainChi - cypherChi) / plainChi); } return(scores.Average()); }
private bool StreamTester(ICypher cypher, string plain, string key) { int capacity = plain.Length * 2 + 256; //2 bytes per char using (Stream plainStream = GenerateStreamFromString(plain)) using (MemoryStream cryptoStream = new MemoryStream(capacity)) using (MemoryStream backPlainStream = new MemoryStream(capacity)) { byte[] keyArr = key.ToByteArray(); plainStream.Position = 0; cryptoStream.Position = 0; backPlainStream.Position = 0; cypher.Encrypt(plainStream, keyArr, cryptoStream); plainStream.Position = 0; cryptoStream.Position = 0; backPlainStream.Position = 0; cypher.Decrypt(cryptoStream, keyArr, backPlainStream); plainStream.Position = 0; cryptoStream.Position = 0; backPlainStream.Position = 0; int origByte, newByte; do { origByte = plainStream.ReadByte(); newByte = backPlainStream.ReadByte(); if (origByte != newByte) { return(false); } } while (origByte != -1 && newByte != -1); return(true); } }
public static string Decrypt(this ICypher self, string cypher, string key) { byte[] cypherByte = cypher.ToByteArray(); byte[] keyByte = key.ToByteArray(); return(self.Decrypt(cypherByte, keyByte).ToTextString()); }
public CypherMachine() { cypher = this; }
public AuthenticationService(StudentSystemAuthDbContext studentSystemAuthDbContext, ICypher cypher, IMediator mediator) { _studentSystemAuthDbContext = studentSystemAuthDbContext; _cypher = cypher; _mediator = mediator; }
public EncryptTest(ICypher e, string name, char[] allowedAlpha, string[] texts) : this(e, name, allowedAlpha) { Texts = texts; }
public EncryptTest(ICypher e, string name, TestType types) : this(e, name, null, types) { }
public EncryptTest(ICypher e, string name, char[] allowedAlpha) : this(e, name, allowedAlpha, TestType.All) { }
public EncryptTest(ICypher e, string name) : this(e, name, null) { }
public SecurityManager(IDataPipe pipe, IPasswordValidator validator, ICypher cypher) { _pipe = pipe; _validator = validator; _cypher = cypher; }
public CypherQueryBegin(ICypher cypher) { _cypher = cypher; }
public GetWebsiteHandler(IWebsiteRepository repository, ICypher cypher) { _repository = repository; _cypher = cypher; }
public TestCase(ICypher b, string a, float?c) { Name = a; Cypher = b; ExpectedVal = c; }
public static string Encrypt(this ICypher self, string plain, string key) { byte[] plainByte = plain.ToByteArray(); byte[] keyByte = key.ToByteArray(); return(self.Encrypt(plainByte, keyByte).ToTextString()); }