Пример #1
0
        public string GenerateRandomPassword()
        {
            var passwordBytes = this._platform.GenerateRandomBytes(32);

            char[] passwordChars = Base64Encoder.EncodeDataToBase64CharArray(passwordBytes);

            string passwordString = new string(passwordChars).Remove(43).Replace("/", "$");
            var    sb             = new StringBuilder();

            for (var i = 0; i != passwordString.Length; ++i)
            {
                sb.Append(passwordString[i]);
                var insertSpace   = (i + 1) % 5 == 0;
                var insertNewLine = (i + 1) % 25 == 0;
                if (insertNewLine)
                {
                    sb.Append(Environment.NewLine);
                }
                else if (insertSpace)
                {
                    sb.Append(" ");
                }
            }
            return(sb.ToString());
        }
Пример #2
0
        public static CipherV2 DissectVisualCryptText(string visualCryptText, LongRunningOperationContext context)
        {
            try
            {
                var visualCrypt = WhiteListVisualCryptCharacters(visualCryptText, context);

                if (!visualCrypt.StartsWith(VisualCryptSlashText, StringComparison.OrdinalIgnoreCase))
                {
                    throw CommonFormatException("The prefix '{0}' is missing.".FormatInvariant(VisualCryptSlashText));
                }

                var visualCryptTextV2Base64 = visualCrypt.Remove(0, VisualCryptSlashText.Length).Replace('$', '/');

                var visualCryptTextV2Bytes = Base64Encoder.DecodeBase64StringToBinary(visualCryptTextV2Base64);
                return(DissectBytesToCipherV2(visualCryptTextV2Bytes));
            }
            catch (Exception e)
            {
                if (e.Message.StartsWith(LocalizableStrings.MsgFormatError))
                {
                    throw;
                }
                throw CommonFormatException(e.Message);
            }
        }
Пример #3
0
        public static VisualCryptText CreateVisualCryptText(byte[] cipherV2, int length, int breakAfter = 74)
        {
            Guard.NotNull(cipherV2);
            int maxBytesNeeded;

            if (length == -1)
            {
                length         = Base64Encoder.CalculateBase64EncodedLengthInChars(cipherV2.Length);
                maxBytesNeeded = cipherV2.Length;
            }
            else
            {
                maxBytesNeeded = Base64Encoder.SafeEstimateBytesNeededForNBase64Chars(length);
            }
            char[] charsToProduce = new char[length + 4]; // safety margin
            Convert.ToBase64CharArray(cipherV2, 0, Math.Min(cipherV2.Length, maxBytesNeeded), charsToProduce, 0);


            var sb       = new StringBuilder();
            var ellipsis = " ...";

            length = length - ellipsis.Length;
            var charsInLine         = 0;
            var charactersProcessed = 0;

            foreach (var c in VisualCryptSlashText)
            {
                sb.Append(c);
                if (++charsInLine != breakAfter)
                {
                    continue;
                }
                sb.Append(new[] { '\r', '\n' });
                charsInLine = 0;
            }

            foreach (var c in charsToProduce)
            {
                if (length == charactersProcessed++)
                {
                    sb.Append(ellipsis);
                    break;
                }


                sb.Append(c == '/' ? '$' : c);
                if (++charsInLine != breakAfter)
                {
                    continue;
                }
                sb.Append(new[] { '\r', '\n' });
                charsInLine = 0;
            }

            return(new VisualCryptText(sb.ToString()));
        }
Пример #4
0
        public static VisualCryptText CreateVisualCryptText(CipherV2 cipherV2)
        {
            Guard.NotNull(cipherV2);

            var visualCryptTextV2Bytes = ByteArrays.Concatenate(
                // len			Sum(len)		Start Index
                new[] { CipherV2.Version },                // 1			1				0
                new[] { cipherV2.RoundsExponent.Value },   // 1			2				1
                new[] { cipherV2.PlaintextPadding.Value }, // 1			3				2
                cipherV2.IV16.GetBytes(),                  // 16			19				3
                cipherV2.MACCipher16.GetBytes(),           // 16			35				19
                cipherV2.RandomKeyCipher32.GetBytes(),     // 32			67				35
                cipherV2.MessageCipher.GetBytes()          // len			67 + len		67
                );

            if (visualCryptTextV2Bytes.Length < HeaderLenght)
            {
                throw new Exception("Data cannot be shorter than the required header.");
            }

            var visualCryptTextV2Base64 = Base64Encoder.EncodeDataToBase64CharArray(visualCryptTextV2Bytes);

            var       sb          = new StringBuilder();
            const int breakAfter  = 74;
            var       charsInLine = 0;

            foreach (var c in VisualCryptSlashText)
            {
                sb.Append(c);
                if (++charsInLine != breakAfter)
                {
                    continue;
                }
                sb.Append(new[] { '\r', '\n' });
                charsInLine = 0;
            }

            foreach (var c in visualCryptTextV2Base64)
            {
                sb.Append(c == '/' ? '$' : c);
                if (++charsInLine != breakAfter)
                {
                    continue;
                }
                sb.Append(new[] { '\r', '\n' });
                charsInLine = 0;
            }

            return(new VisualCryptText(sb.ToString()));
        }