Static Method

G

Guest

Hi I have the following code

private string GetUser()
{
Decryption decrypt = new Decryption();
return (string) decrypt.DecodeText("agyL70eOPb=", key);
}

which calls this static method

public class Decryption
{
public static string DecodeText(string encrypted, string source)
{
string decrypted;
TripleDESCryptoServiceProvider des;
MD5CryptoServiceProvider hashmd5;
byte[] pwdhash, buff;
hashmd5 = new MD5CryptoServiceProvider();
pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(source));
hashmd5 = null;
des = new TripleDESCryptoServiceProvider();
des.Key = pwdhash;
des.Mode = CipherMode.ECB;
buff = Convert.FromBase64String(encrypted);
decrypted = ASCIIEncoding.ASCII.GetString(des.CreateDecryptor
().TransformFinalBlock(buff, 0, buff.Length));
des = null;
return decrypted;
}
}


Is there a way to get rid of the 'new' line from the calling code

ie

private string GetUser()
{
return (string) DecodeText("agyL70eOPb=", key);
}

does not seem to work.
 
R

Remigiusz Dybka

how about:

return (string) Decryption.DecodeText("agyL70eOPb=", key);

This will work ONLY if DecodeText is declared static (which it is in
your example).
 
R

Remigiusz Dybka

Remigiusz Dybka napisał(a):
how about:

return (string) Decryption.DecodeText("agyL70eOPb=", key);

This will work ONLY if DecodeText is declared static (which it is in
your example).

....and of course the cast to string is unneeded :) - sorry, forgot that.
 
G

Guest

used

return Library.Decryption.DecodeText("agyL70eOPb=", key);

but I get the compile error

An object reference is required for the nonstatic field, method, or property
'Library.Decryption.DecodeText(string, string)'

can't see why as the DecodeText is static?
 
R

Richard Blewett [DevelopMentor]

This code will not compile in C# as it is illegal to call a static method through an object reference. You absolutely don't need the "new" line but you have to call through the type name, i.e.

private string GetUser()
{
return Decryption.DecodeText("agyL70eOPb=", key);
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi I have the following code

private string GetUser()
{
Decryption decrypt = new Decryption();
return (string) decrypt.DecodeText("agyL70eOPb=", key);
}

which calls this static method

public class Decryption
{
public static string DecodeText(string encrypted, string source)
{
string decrypted;
TripleDESCryptoServiceProvider des;
MD5CryptoServiceProvider hashmd5;
byte[] pwdhash, buff;
hashmd5 = new MD5CryptoServiceProvider();
pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(source));
hashmd5 = null;
des = new TripleDESCryptoServiceProvider();
des.Key = pwdhash;
des.Mode = CipherMode.ECB;
buff = Convert.FromBase64String(encrypted);
decrypted = ASCIIEncoding.ASCII.GetString(des.CreateDecryptor
().TransformFinalBlock(buff, 0, buff.Length));
des = null;
return decrypted;
}
}


Is there a way to get rid of the 'new' line from the calling code

ie

private string GetUser()
{
return (string) DecodeText("agyL70eOPb=", key);
}

does not seem to work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top