Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
sorry :), i'm new in C#. I used AES ,Here's the code

C#
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.IO;
using System.Data.OleDb;
using System.Security.Cryptography;

namespace EnDecrytion_tryout
{
publicpartialclassReadExcel : Form
{
	privatestaticstring key = "asdfghjklzxcasdfghjklzxcasdfghjk";
	privatestaticstring IV = "qwertyuiopasdfgh";
	public ReadExcel()
        {
            InitializeComponent();
            browse.Click += newEventHandler(browse_Click);
            btnloadexcel.Click += newEventHandler(btnloadexcel_Click);
        } 

	void btnloadexcel_Click(object sender, EventArgs e)
        {
		string pathcon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" +label1.Text 
+ @";Extended Properties =""Excel 8.0;IMEX=1 HDR=Yes;ImportMixedTypes=Text;TypeGuessRows=0""";
		
		OleDbConnection conn = newOleDbConnection(pathcon);

var adapter = newOleDbDataAdapter("SELECT CATEGORY,ITEM_NAME, SUBNAME_or_BRAND, ITEM_SIZE, ITEM_QUANTITY_DESCRIPTION,ITEM_UNIT, QUANTITY, REFERENCE, TRANSACTION, TRANSACTION_DATE" +")"+ "FROM [SHEET1$]", conn);
		var ds = newDataSet();

            	adapter.Fill(ds);

		DataTable QueryTable = ds.Tables[0];

            	dataGridView1.DataSource = QueryTable;
        }

	void browse_Click(object sender, EventArgs e)
        {
		if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            	{
                	label1.Text = openFileDialog1.FileName;
            	}
        }
	publicstaticstring decrypt(string adap)
	{
		byte[] encryptedbytes = Convert.FromBase64String(adap);
		AesCryptoServiceProvider aes = newAesCryptoServiceProvider();
            	aes.BlockSize = 128;
           	aes.KeySize = 256;
            	aes.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(key);
            	aes.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV);
            	aes.Padding = PaddingMode.PKCS7;
            	aes.Mode = CipherMode.CBC;
		ICryptoTransform crypto = aes.CreateDecryptor(aes.Key, aes.IV);
		byte[] secret = crypto.TransformFinalBlock(encryptedbytes, 0, 				
		encryptedbytes.Length);
        	crypto.Dispose();
		return System.Text.ASCIIEncoding.ASCII.GetString(secret);
	}
    }
}


...now, idont know where to insert that decryptor, what will i do??
Posted
Updated 7-Mar-14 19:49pm
v2
Comments
dan!sh 7-Mar-14 3:39am    
What algorithm have you used to encrypt it? Encryption providers generally come with both Encrypt and Decrypt methods.
Vedat Ozan Oner 7-Mar-14 5:43am    
that is a very weird question. because if you are able to encrypt and write into excel file, I can easily assume that you can also decrypt and read it. you shouldn't need any help. If you have an error in your code, send it and let's see together.
ZurdoDev 7-Mar-14 6:38am    
Step 1. Write the code. Step 2. If you get stuck on something specific, ask a specific question.
Maciej Los 7-Mar-14 13:53pm    
Brilliant answer. My virtual 5!
ZurdoDev 7-Mar-14 13:57pm    
I virtually accept. :)

1 solution

Please, read all comments to the question first.

Mugaw25 wrote:
the prog. should read the code from excel and should decrypt it before displaying the data in datagridview.

There is no way to "read the code from Excel".

I suggest you to read data direct from Excel, then to decrypt it using C#. If you are the author of encrypt/decrypt algorithm, there is no obstacle to write it using C#.

Try!

By The Way: if you provide method to read Excel file, i would be able to add more information.

[EDIT]
Mugaw25 wrote:
where to insert that decryptor

Just when the data are being loaded to datatable. You can use DataTable.RowChanging event[^] (or other) to loop through the collection of data and decrypt it.
[/EDIT]
 
Share this answer
 
v2
Comments
Mugaw25 8-Mar-14 1:59am    
sorry :),that's actually "read the data from excel".. i improved my question, if you can read again and type some help :)
Maciej Los 8-Mar-14 10:11am    
See updated answer ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900