Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Encryption Of The Data In C#

c# encryption

  • Please log in to reply
2 replies to this topic

#1 shravya

shravya

    CC Newcomer

  • Member
  • PipPip
  • 13 posts

Posted 13 April 2012 - 10:14 PM

hi! guys i just came through this code on Google


thank you in advance.

public string Encrypt(string plainText)
{
if (plainText == null) throw new ArgumentNullException("plainText");
//encrypt data
var data = Encoding.Unicode.GetBytes(plainText);
byte[] encrypted = ProtectedData.Protect(data, null, Scope);

//return as base64 string
return Convert.ToBase64String(encrypted);
}



and tried to implement it since i had some difficulty in understanding the code .But the problem is i'm unable to run and check how it works because when i try to run i get the 2 errors they are as below:
The name 'ProtectedData' does not exist in the current context
The name 'Scope' does not exist in the current context
idont know what to do
can some body help me out in this
i mean what should i do to make the above piece of code work or can somebody help me out iby explaining the whole program with its working and librires it uses
here are the system namespaces which i used:

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;


  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 13 April 2012 - 10:20 PM

First you need to add a reference to your project to the system.security.dll library. To do follow the steps...
  • Right click on your project node -->Add Reference
  • From the Add Reference window --> .NET tab, find System.Security.dll library and click on Ok.
And it will work now. Another thing is that you have to put a value for score. Following is the working code...
public string Encrypt(string plainText)
{
	if (plainText == null) throw new ArgumentNullException("plainText");

	//encrypt data
	var data = Encoding.Unicode.GetBytes(plainText);
	byte[] encrypted = ProtectedData.Protect(data, null, DataProtectionScope.LocalMachine);

	//return as base64 string
	return Convert.ToBase64String(encrypted);
}

  • 1

#3 shravya

shravya

    CC Newcomer

  • Member
  • PipPip
  • 13 posts

Posted 13 April 2012 - 10:44 PM

thank you kernel coder for your help it really helped me a lot
  • 0





Also tagged with one or more of these keywords: c#, encryption

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download