user505689
user505689

Reputation: 81

How to Decrypt using Javascript

I have a captcha text,which I am storing in hidden variable and retreiving the value in code behind(c#.net).

Now I need to Encrypt that captcha text and store in hidden variable and decrypt the same in c#.net

I need an algorithm that works both in javascript and c# .i.e encrypt in javascript and decrypt in c#.net.

eg : text : "dfg563hj"

Thanks for your help in advance. Ramesh.

Upvotes: 2

Views: 1578

Answers (2)

jamiegs
jamiegs

Reputation: 1791

I also do not think this is a good idea because they'd be able to see how to do the decryption.. Here's javascript for doing a DES decryption http://www.teslacore.it/sssup1/des.js

you just need to call

encryptedText = des(password, unHex(contents), 0);

Here's c# for doing DES encyption

public static string Encrypt(string originalString)
{
    if (String.IsNullOrEmpty(originalString))
    {
        throw new ArgumentNullException
               ("The string which needs to be encrypted can not be null.");
    } 
    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    MemoryStream memoryStream = new MemoryStream();
    CryptoStream cryptoStream = new CryptoStream(memoryStream, 
        cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
    StreamWriter writer = new StreamWriter(cryptoStream);
    writer.Write(originalString);
    writer.Flush();
    cryptoStream.FlushFinalBlock();
    writer.Flush();
    return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}

Upvotes: 2

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22104

Whether such an encryption exists or not, it would be a bad idea to have Captcha text in Client side and modified using JavaScript.

Upvotes: 0

Related Questions