| Problem trying to create an Encryption Library |
|
| Author |
Message |
Heathermack

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
I have followed the example for creating a C# Encryption Libary, but receive numerous errors with regard to no overload method ' ' requires 2 arguments, I have verified that my code is exactly as the example provided by microsoft and found no difference. Has anyone ever tried this code and had problems. Is there any other code to create an encryptoin library in C#.
I would appreciate any and all assistance. I am responsible for figuring out how to encrypt the connection string for our web config files in Asp.net page. (we dont' have the .net 2.o framework, we are using Visual Studio 2003 with 1.x framework
Thanks
Heather
Visual FoxPro2
|
| |
|
| |
 |
giuln

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
Can you post example or where it could be found
|
| |
|
| |
 |
Heathermack

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
Sure
msdn.microsoft.com/library/en-us/dnnetsec/html/secnetht10.asp
where I am having a problem is with the overload method for the encrypt.cs and decrypt.cs. won't compile says I need two arguments.
Thanks Heather
|
| |
|
| |
 |
giuln

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
Hi Heather.
In Encryptor.Encrypt e Decryptor.Decrypt, replace this row
ICryptoTransform transform = transformer.GetCryptoServiceProvider(bytesKey);
with
ICryptoTransform transform = transformer.GetCryptoServiceProvider(bytesKey, initVec);
GetCryptoServiceProvider method of both EncryptTransformer and DecryptTransformer classes accepts two parameters...
A little bug to fix... ;-))
|
| |
|
| |
 |
giuln

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
In Console application Main method, you have to replace
cipherText = enc.Encrypt(plainText, key);
and
byte[] plainText = dec.Decrypt(cipherText, key);
rows with
cipherText = enc.Encrypt(plainText, key, IV);
and
byte[] plainText = dec.Decrypt(cipherText, key, IV);
Another fix...
|
| |
|
| |
 |
Heathermack

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
I have made the changes as you instructed and now receive the following errors:
No overload for method TOBase64CharArray takes 1 arguments
Nooverload for method Decrypt takes 3 arguments.
Thanks for all your assistance
Heather
|
| |
|
| |
 |
giuln

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
Are you sure you have not made other changes
I haven't method ToBase64CharArray... Where is it
My Decrypt method takes 3 arguments...
I build both class library and test console app solutions and all works fine...
Can you post (or send via e-mail) your code
|
| |
|
| |
 |
Heathermack

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
Can't thank you enough. You were right I had changed two things, there was no chararray - should have been string and I had added IV to the decrypt method of the Decryptor.
I appreciate all your help.
|
| |
|
| |
 |
Heathermack

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
I now have another problem.
I am trying to use the assembly I created following the instructions on Creating an Encryption Library which with your help was able to do. Now I receive the same errors when tyring to use this assembly to store the encrypted string in the registry and create a c# windows project. I had fixed these errors adding the IV as an argument, but if I add this to the C# windows project it gives me another error.
No overload for method 'Encrypt' takes '2' arguments No overload for method 'Decrypt' takes '2' arguments
the example I am following is at http://msdn.microsoft.com/library/en-us/dnnetsec/html/SecNetHT11.asp
Thanks
Heather
|
| |
|
| |
 |
giuln

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
It's the same error...
Probably you could remove the third parameter in encrypt/decrypt class methods...
However now try with
byte[] cipherText = enc.Encrypt(plainText, key, Encoding.ASCII.GetBytes("init vec"));
and
byte[] plainText = dec.Decrypt(Convert.FromBase64String(txtEncryptedString.Text), key, Encoding.ASCII.GetBytes("init vec"));
|
| |
|
| |
 |
giuln

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
However it could be a final fix to define Encrypt and Decrypt methods with 2 parameters as follow
public byte[] Encrypt(byte[] bytesData, byte[] bytesKey) { return Encrypt(bytesData, bytesKey, Encoding.ASCII.GetBytes("init vec")); }
public byte[] Decrypt(byte[] bytesData, byte[] bytesKey) { return Decrypt(bytesData, bytesKey, Encoding.ASCII.GetBytes("init vec")); }
in Encryptor and Decryptor classes... ;-))
|
| |
|
| |
 |
Heathermack

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
This did work, but now when I run the EncryptionTest Project I receive the following error at this point.
Console.WriteLine("InitializationVector....
{value cannot be null...
Heather
|
| |
|
| |
 |
giuln

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
- What have you modified in EncryptionTest or Encryption Classes
- Have you these rows in EncryptionTest
enc.IV = IV;
// Perform the encryption.
cipherText = enc.Encrypt(plainText, key, IV);
- Does Connect String Encryption work
|
| |
|
| |
 |
Heathermack

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
Yes if have enc.IV = IV; in my Encryption Class.
With the exception of yoru post outlining a final fix, all the changes you gave have been done.
I did have cipherText = enc.Encrypt(plainText, key, IV); but would receive with the IV.
That was changed by your post of 7/21/2006
to byte[]cipherText = enc.Encrypt(plainText, key, Encoding.ASCII.GetBytes("init vec"));
this compiles fine.
Where i try and run the application i get the error
Exception Encrypting. Array can not be null
Parameter name: bytes
I am including the code for my form in the Encryption Test Harness;
The problem line is in bold print
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Encryption;
using System.Text;
using Microsoft.Win32;
namespace EncryptionTestApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txtInitializationVector;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtEncryptedString;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox txtDecryptedString;
private System.Windows.Forms.Button btnEncrypt;
private System.Windows.Forms.Button btnDecrypt;
private System.Windows.Forms.Button btnWriteRegistryData;
private System.Windows.Forms.TextBox txtConnectionstring;
private System.Windows.Forms.TextBox txtKey;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.txtConnectionstring = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtKey = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtInitializationVector = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.txtEncryptedString = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.txtDecryptedString = new System.Windows.Forms.TextBox();
this.btnEncrypt = new System.Windows.Forms.Button();
this.btnDecrypt = new System.Windows.Forms.Button();
this.btnWriteRegistryData = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(112, 24);
this.label1.TabIndex = 0;
this.label1.Text = "Connection String:";
//
// txtConnectionstring
//
this.txtConnectionstring.Location = new System.Drawing.Point(120, 8);
this.txtConnectionstring.Name = "txtConnectionstring";
this.txtConnectionstring.Size = new System.Drawing.Size(144, 20);
this.txtConnectionstring.TabIndex = 1;
this.txtConnectionstring.Text = "Server=local; database = pubs; uid=bob; pwd=Password ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(104, 16);
this.label2.TabIndex = 2;
this.label2.Text = "Key:";
//
// txtKey
//
this.txtKey.Location = new System.Drawing.Point(120, 40);
this.txtKey.Name = "txtKey";
this.txtKey.Size = new System.Drawing.Size(144, 20);
this.txtKey.TabIndex = 3;
this.txtKey.Text = "0123456789012345";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 88);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 24);
this.label3.TabIndex = 4;
this.label3.Text = "Initialization Vector:";
//
// txtInitializationVector
//
this.txtInitializationVector.Location = new System.Drawing.Point(120, 80);
this.txtInitializationVector.Name = "txtInitializationVector";
this.txtInitializationVector.Size = new System.Drawing.Size(144, 20);
this.txtInitializationVector.TabIndex = 5;
this.txtInitializationVector.Text = "textBox1";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 128);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(96, 24);
this.label4.TabIndex = 6;
this.label4.Text = "Encrypted String";
//
// txtEncryptedString
//
this.txtEncryptedString.Location = new System.Drawing.Point(120, 128);
this.txtEncryptedString.Name = "txtEncryptedString";
this.txtEncryptedString.Size = new System.Drawing.Size(144, 20);
this.txtEncryptedString.TabIndex = 7;
this.txtEncryptedString.Text = "textBox1";
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 168);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(96, 16);
this.label5.TabIndex = 8;
this.label5.Text = "Decrypted String";
//
// txtDecryptedString
//
this.txtDecryptedString.Location = new System.Drawing.Point(120, 168);
this.txtDecryptedString.Name = "txtDecryptedString";
this.txtDecryptedString.Size = new System.Drawing.Size(152, 20);
this.txtDecryptedString.TabIndex = 9;
this.txtDecryptedString.Text = "textBox1";
//
// btnEncrypt
//
this.btnEncrypt.Location = new System.Drawing.Point(24, 216);
this.btnEncrypt.Name = "btnEncrypt";
this.btnEncrypt.Size = new System.Drawing.Size(80, 24);
this.btnEncrypt.TabIndex = 10;
this.btnEncrypt.Text = "Encrypt";
this.btnEncrypt.Click += new System.EventHandler(this.btnEncrypt_Click);
//
// btnDecrypt
//
this.btnDecrypt.Location = new System.Drawing.Point(136, 216);
this.btnDecrypt.Name = "btnDecrypt";
this.btnDecrypt.Size = new System.Drawing.Size(80, 24);
this.btnDecrypt.TabIndex = 11;
this.btnDecrypt.Text = "Decrypt";
this.btnDecrypt.Click += new System.EventHandler(this.btnDecrypt_Click);
//
// btnWriteRegistryData
//
this.btnWriteRegistryData.Location = new System.Drawing.Point(240, 216);
this.btnWriteRegistryData.Name = "btnWriteRegistryData";
this.btnWriteRegistryData.Size = new System.Drawing.Size(120, 24);
this.btnWriteRegistryData.TabIndex = 12;
this.btnWriteRegistryData.Text = "Write Registry Data";
this.btnWriteRegistryData.Click += new System.EventHandler(this.btnWriteRegistryData_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(384, 273);
this.Controls.Add(this.btnWriteRegistryData);
this.Controls.Add(this.btnDecrypt);
this.Controls.Add(this.btnEncrypt);
this.Controls.Add(this.txtDecryptedString);
this.Controls.Add(this.label5);
this.Controls.Add(this.txtEncryptedString);
this.Controls.Add(this.label4);
this.Controls.Add(this.txtInitializationVector);
this.Controls.Add(this.label3);
this.Controls.Add(this.txtKey);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtConnectionstring);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Encryption Test Harness";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run( new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void btnEncrypt_Click(object sender, System.EventArgs e)
{
try
{
Encryptor enc = new Encryptor(EncryptionAlgorithm.TripleDes);
byte[] plainText =
Encoding.ASCII.GetBytes(txtConnectionstring.Text);
byte[] key =
Encoding.ASCII.GetBytes(txtKey.Text);
byte[] cipherText = enc.Encrypt(plainText,key, Encoding.ASCII.GetBytes("init vec"));
txtInitializationVector.Text = Encoding.ASCII.GetString(enc.IV);
txtEncryptedString.Text = Convert.ToBase64String(cipherText);
}
catch(Exception ex)
{
MessageBox.Show("Exception encrypting: " + ex.Message, "encryption ");
}
}
private void btnDecrypt_Click(object sender, System.EventArgs e)
{
try
{
Decryptor dec = new Decryptor(EncryptionAlgorithm.TripleDes);
dec.IV = Encoding.ASCII.GetBytes(txtInitializationVector.Text);
byte[] key = Encoding.ASCII.GetBytes(txtKey.Text);
byte[] plainText = dec.Decrypt(Convert.FromBase64String(txtEncryptedString.Text),key,Encoding.ASCII.GetBytes("initVec"));
txtDecryptedString.Text = Encoding.ASCII.GetString(plainText);
}
catch(Exception ex)
{
MessageBox.Show("Exception decrypting. " + ex.Message,
"Encryption Test Harness");
}
}
private void btnWriteRegistryData_Click(object sender, System.EventArgs e)
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software", true);
rk = rk.CreateSubKey("TestApplication");
rk.SetValue("connectionString",txtEncryptedString.Text);
rk.SetValue("init vec",
Encoding.ASCII.GetBytes(txtInitializationVector.Text));
rk.SetValue("key", Encoding.ASCII.GetBytes(txtKey.Text));
MessageBox.Show("the data has been successfully written to the registry");
}
}
}
|
| |
|
| |
 |
giuln

|
Posted: Visual FoxPro General, Problem trying to create an Encryption Library |
Top |
Ok.
In Encrypt method you have to add this row
IV = transformer.IV;
in this point
encKey = transformer.Key;
// ADD ROW HERE
encStream.FlushFinalBlock();
encStream.Close();
Now modify btnDecrypt_Click method because Encryption uses "init vec" while Decryption uses "initvec" and this is incorrect... So use the value stored in txtInitializationVector.Text by btnEncrypt_Click...
private void btnDecrypt_Click(object sender, System.EventArgs e)
{
try
{
Decryptor dec = new Decryptor(EncryptionAlgorithm.TripleDes);
dec.IV = Encoding.ASCII.GetBytes(txtInitializationVector.Text);
byte[] key = Encoding.ASCII.GetBytes(txtKey.Text);
byte[] plainText = dec.Decrypt(Convert.FromBase64String(txtEncryptedString.Text), key, Encoding.ASCII.GetBytes(txtInitializationVector.Text));
txtDecryptedString.Text = Encoding.ASCII.GetString(plainText);
}
catch (Exception ex)
{
MessageBox.Show("Exception decrypting. " + ex.Message, "Encryption Test Harness");
}
}
I think I'll refact this example asap... ;-)
Bye,
Giulio
|
| |
|
| |
 |
| |
|