コード例 #1
0
        public string DecryptedName()
        {
            int len   = 'z' - 'a' + 1;
            int shift = SectorId % len;

            return(string.Concat(EncryptedName.Select(c => c == '-' ? ' ' : (char)((c - 'a' + shift) % len + 'a'))));
        }
コード例 #2
0
ファイル: Day4.cs プロジェクト: kjellskogsrud/AoC_2016
            private bool Validate()
            {
                // this thing from: https://github.com/Pyrobolser/AdventOfCode2016/blob/master/AdventOfCode2016/Days/Day4.cs
                // thank you to pyrobolser, Today I learned about new fansy things to do with string, using LINQ. :)
                string calculatedCheckSum = string.Join("", EncryptedName.GroupBy(c => c).OrderByDescending(c => c.Count()).ThenBy(c => c.Key).Take(5).Select(c => c.Key).ToList());

                // return the result of this evaluation.
                return((calculatedCheckSum == this.Checksum) ? true : false);
            }
コード例 #3
0
        public bool MatchesChecksum()
        {
            var cleaned = EncryptedName.Replace("-", "");
            var ordered = String.Concat(cleaned.OrderBy(c => c));
            var commons = ordered.GroupBy(x => x).OrderByDescending(x => x.Count()).Take(5);

            var calculated = string.Format("[{0}]", string.Concat(commons.Select(x => x.Key)));

            return(calculated == Checksum);
        }
コード例 #4
0
 public bool IsReal()
 {
     return(Checksum == string.Concat(EncryptedName
                                      .Replace("-", "")
                                      .GroupBy(c => c)
                                      .OrderByDescending(g => g.Count())
                                      .ThenBy(g => g.Key)
                                      .Select(g => g.Key)
                                      .Take(5)));
 }
コード例 #5
0
ファイル: Room.cs プロジェクト: tobper/AdventOfCode2016
        public bool IsReal()
        {
            var chars = EncryptedName.
                        Replace("-", "").
                        GroupBy(c => c).
                        OrderByDescending(g => g.Count()).
                        ThenBy(g => g.Key).
                        Select(g => g.Key).
                        Take(5).
                        ToArray();

            var actualChecksum = new string(chars);
            var roomIsReal     = actualChecksum == Checksum;

            return(roomIsReal);
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: PanosKousidis/AOC-2016
        private string GenerateHash()
        {
            var dictionary = new Dictionary <char, int>();

            foreach (var c in EncryptedName.Replace("-", ""))
            {
                if (dictionary.ContainsKey(c))
                {
                    dictionary[c]++;
                }
                else
                {
                    dictionary.Add(c, 1);
                }
            }
            dictionary = dictionary.OrderByDescending(x => x.Value).ThenBy(x => x.Key)
                         .ToDictionary(x => x.Key, x => x.Value);
            var ret = string.Join("", dictionary.Keys).Substring(0, 5);

            return(ret);
        }
コード例 #7
0
            public RoomRecord(string rawText)
            {
                FullText = rawText.Trim();

                var parts = FullText.Split('-');

                EncryptedName = string.Join('-', parts.Take(parts.Length - 1));

                var suffix      = parts.Last();
                var suffixParts = suffix.Split('[');

                RawSectorId = suffixParts[0];
                Checksum    = suffixParts[1].TrimEnd(']');
                SectorId    = int.Parse(RawSectorId);

                var letters      = EncryptedName.Replace("-", "");
                var letterCounts = new Dictionary <char, int>();

                foreach (var l in letters)
                {
                    if (!letterCounts.ContainsKey(l))
                    {
                        letterCounts[l] = 0;
                    }
                    letterCounts[l] = 1 + letterCounts[l];
                }

                RealChecksum = new string(
                    letterCounts
                    .OrderByDescending(x => x.Value)
                    .ThenBy(x => x.Key)
                    .Select(x => x.Key)
                    .ToArray()
                    )
                               .Substring(0, 5);
                IsReal = RealChecksum == Checksum;

                RealName = IsReal ? Decrypt() : null;
            }
コード例 #8
0
ファイル: Room.cs プロジェクト: tobper/AdventOfCode2016
        public string GetDecryptedName()
        {
            var chars         = EncryptedName.ToCharArray();
            var shifts        = SectorId % 26;
            var decryptedName = "";

            foreach (var letter in EncryptedName)
            {
                if (letter == '-')
                {
                    decryptedName += " ";
                }
                else
                {
                    int  alphabetIndex        = letter - 'a';
                    int  shiftedAlphabetIndex = (alphabetIndex + shifts) % 26;
                    char shiftedCharacter     = (char)(shiftedAlphabetIndex + 'a');

                    decryptedName += shiftedCharacter;
                }
            }

            return(decryptedName);
        }
コード例 #9
0
ファイル: EfsFile.cs プロジェクト: AleksaMCode/Enigma
        /// <summary>
        /// Decrypts the filename and initializes <see cref="Name"/> field value.
        /// </summary>
        /// <param name="file">Encrpted file in raw form.</param>
        /// <param name="userId">Unique identifier of the logged-in user.</param>
        /// <param name="userPrivateKey">Users private RSA key.</param>
        private void DecryptName(byte[] file, int userId, RSAParameters userPrivateKey)
        {
            var offset = 44; // we are skipping StandardInformation header
            var securityDescriptorHeader = new SecurityDescriptor();

            securityDescriptorHeader.ParseSecurityDescriptor(file, ref offset);
            OwnerId = securityDescriptorHeader.OwnerId;

            try
            {
                var fileKey = securityDescriptorHeader.GetKey(userId, userPrivateKey); // if user isn't authorised to access the file, exception will be thrown
                Name = Encoding.ASCII.GetString(new AesAlgorithm(fileKey, securityDescriptorHeader.IV, "OFB").Decrypt(Convert.FromBase64String(EncryptedName.Split('.')[0].Replace('$', '/'))));
            }
            // When reading files in Shared folder, users who don't have approved access will be able to see files in folder but won't be able to read them or see their real names.
            catch (Exception)
            {
                //Name = EncryptedName;
            }
        }