-2

I have a question...kind of basic but I thought I can take some help from you guys

I am encrypting a file and the information I encrypt is

LoginTxtBox.Text + "/" + PwdTxtBox.Text + "/" + InstNameTextBox.Text + "/" + DBNameTxtBox.Text;

When I decrypt it ... I am doing:

StringBuilder sClearText = new StringBuilder();
encryptor.Decrypt(sPrivateKeyFile, sDataFile, sClearText);

//username/password
string s = sClearText.ToString();
string[] split = s.Split(new Char[] { '/' });
if (split.Length == 4)
{
    split0 = split[0];
    split1 = split[1];
    split2 = split[1];
    split3 = split[1];

Now the requirement I got is I need to count the delimiters in the decrypted format of string and if there are more than 2 delimiter then its not a new application. If there is only one delimiter then its a never used application. I don't know how to to count the delimiters from the decrypt string...Help me plzz

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
user1410658
  • 551
  • 3
  • 10
  • 20
  • 2
    The code you've shown implicitly knows the number of delimiters, doesn't it? (`split.Length - 1`) ;) – Dan J Jul 31 '12 at 17:54
  • 3
    Side note: using pretty much any printable character to separate user-entered strings is bad idea. Please consider proper serialization to avoid all sorts of injection issues (i.e. password "a/b" would be awesome for your system). – Alexei Levenkov Jul 31 '12 at 18:00
  • @Dan Yes!! Searching for more ways to do that Regex.Matches( s, "/" ).Count .This worked perfectly. Thank you for your time :) – user1410658 Jul 31 '12 at 18:11
  • 1
    @user1410658: Please also read the answers to [this previous question about storing encrypted passwords](http://stackoverflow.com/questions/1607075/storing-encrypted-passwords). Basically, even if you're encrypting passwords, unless you're using a cryptographically strong one-way hash algorithm, you're doing it wrong. – Daniel Pryden Jul 31 '12 at 18:34
  • @DanielPryden probably, but it *does* depend on the use case. It would be wrong in web user authentication, but for e.g. a password keeper application (to name an obvious example)... – Maarten Bodewes Jul 31 '12 at 22:57
  • @DanielPryden I am new to this site.... Thank you for your Suggestions :) – user1410658 Aug 02 '12 at 14:21

3 Answers3

5

try with this code

Regex.Matches( s,  "/" ).Count
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
2

Some more ways:

int delimiters = input.Count(x => x == '/');

-or-

int delimiters = input.split('/').Length - 1;
naspinski
  • 34,020
  • 36
  • 111
  • 167
0

Couldn't you split the string on the character delimeter and the resulting array should contain one more than the number of delimeters?

mreyeros
  • 4,359
  • 20
  • 24
  • Yes!! i thought of trying that but then thought of knowning diff ways of doing this stuff Regex.Matches( s, "/" ).Count) worked perfectly.Thank you for your time :) – user1410658 Jul 31 '12 at 18:15