 |
|
 |
Hi
Thanks for the code, but Im struggling to encrypt .pdf file.
I will post a working code to encrypt pdf files asp....
|
|
|
|
 |
|
 |
Hi Steve
Thanks for the post, it is the only one that makes sence.
However when i try to encrypt a file it shows the error "Encryption failed!".
I tried to change the input and output files, but now I'm not sure if that is whats causing the error.
What exactly should be the input and output file?
eg. I want to encrypt c:\test.txt
Thanks in advance
Wikus
|
|
|
|
 |
|
 |
Cs.length throws an exception of type System.NotSupportedException
also if I want to save all the crypted bytes for further use (in a byte[] variable ) what should I do?
modified on Wednesday, May 11, 2011 10:03 AM
|
|
|
|
 |
|
 |
how can i use a key larger than 8 chars?? please help
|
|
|
|
 |
|
|
 |
|
 |
Sir, i m getting error in the line
RijndaelManaged RMCrypto = new RijndaelManaged();
when i debugged control does not go to the above line...
it just excutes the line before that i.e "FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);" this line.
n it gives output as error in encryption(catches an exception). so sir plz guide me as early as possible..
i imported all necessary namespaces.
|
|
|
|
 |
|
 |
Short Code to achieve Cryptography.
|
|
|
|
 |
|
 |
I know I'm doing something dumb, but vc express 08 keeps complaining of:
'UnicodeEncoding' : undeclared identifier
'byte' : undeclared identifier
'FileStream' : undeclared identifier
'fsCrypt' : undeclared identifier
'RijndaelManaged' : undeclared identifier
'CryptoStream' : undeclared identifier
'cs' : undeclared identifier
'FileStream' : undeclared identifier
'fsIn' : undeclared identifier
At the top of my file, I've included everything under the sun
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
Its a console app, so I've just kept the wizard's default include, and added the namespaces..
#include "stdafx.h"
#include <iostream>
using namespace std;
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}
I cut and pasted the entire EncryptFile function, without ever calling it
void EncryptFile(string inputFile, string outputFile)
{
try
{
string password = "myKey123"; UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch
{
MessageBox.Show("Encryption failed!", "Error");
}
}
The program works fine (I get the Hello World) if I remove the EncryptFile function.. what am I missing?
Thanks!
|
|
|
|
 |
|
 |
Hi,
Sounds silly, but you are are trying to do this in a C# console app and not a C++ one? It's just that I would expect to see the following in a C++ project, not C#:
#include "stdafx.h"
#include <iostream>
Also, the error:
'byte' : undeclared identifier
would lead me towards that same conclusion as 'byte' is not a built-in type in C++, but is in C#.
Hope this helps.
Steve
|
|
|
|
 |
|
 |
Doh! Thanks a bunch, I missed that! Created a new project, and it's up!
BTW, I'm looking to incorporate this project into a simple programming app that takes an encrypted hex file and programs a board with it. The goal being that I can email the user encrypted hex files, and let the application decrypt it so that the user is prevented from viewing/modifying the hex file.
However, doesn't this mean I have to put the decrypt aes/des key in the programming application.. something that I've read is a no-no? If I have the user enter a passphrase+salt that generates the key, couldn't they just as easily then use the passphrase and generate an aes or des key on their own to decrypt the file outside of my application program?
I can't imagine I'm reinventing the wheel, but the more I think about it the more holes I seem to find!
|
|
|
|
 |
|
 |
Not identified self Name using Not giving the code of dll
RijndaelManaged RMCrypto = new RijndaelManaged();
So bad
|
|
|
|
 |
|
 |
Hi,
If you read the text above the code blocks you will see:
"They both require the use of the System.Security, System.Security.Cryptography, System.Runtime.InteropServices, and System.Text.RegularExpressions namespaces."
Hope this answers your problem.
Steve
|
|
|
|
 |
|
 |
Hello,
I am Taking this in other Direction i Want to know is It Possible to know Key From Encrypt and Decrypt String.
Means Input is :- Encrypt And Decrypt String
Output :- Key.
http://webaspdotnet.blogspot.com/
|
|
|
|
 |
|
 |
Hi, I'm new to encryption/decryption, I simply need to encrypt/decrypt .txt filed. I used you methods they're easy and clear. Encryption piece works fine but for decryption part it does process my files however it returns weird data when I open the .txt files. Any ideas why? Thanks
|
|
|
|
 |
|
 |
Thanks Mr. Steve
Do you have any idea how to minimize execution time of this algorithm?
I was testing this code on file size 307 MB and it takes 15 second in case of Decryption with IV 8 bit size and Key 4 bit size.How can I reach to time less than 15 seconds .
|
|
|
|
 |
|
 |
You know, 307MB is a heck a load bytes, AES is a symmetric algorithm therefore is pretty fast. Only way to increase speed is you can use better computer, so I don't know, maybe you can try some other algorithm, say.. 3DES?
Free the mallocs!!
|
|
|
|
 |
|
 |
I would make the key a public static member that must be set by the caller, this way if your encryption library gets compromised all the solutions implementing the library are not un-necessarily exposed.
Also, refer to the .NET Bible (ISBN-10 0735621721 Practical Guidelines and Best Practices for C#.NET Developers) Section 33.16 Protect Libraries from Unauthorized Use. You need to make sure that the only assemblies that can access your encryption library are ones that you explicitly define (otherwise any idiot could crack your code just by loading your library into Visual Studio). So you need to digitally sign all assemblies that are going to need to use this library and then walk the stack to validate that the caller assembly has been granted explicit access to execute the encrypt and decrypt methods via the digital signatures you establish for the solution.
|
|
|
|
 |
|
 |
Are you sure you want to put your key in code? Seems a bit of a security concern
|
|
|
|
 |
|
 |
Although you are right in considering the key in code a security flaw, I think it would be better to suggest an alternative . Any ideas?
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
 |
|
 |
You're most certainly correct ,
A combination of username and password as a key perhaps. That's the easiest alternative that I can think of.
|
|
|
|
 |
|
 |
A username and password would work well and would be very simple to implement from this code. However, this code was simply to encrypt some application settings which I didn't want the user to be able to amend (trial version expiry date etc.) so it wasn't necessary in this case. Also, the username and password wouldn't be known by the user.
I suppose you could use some other value from the registry or system information. If you were to choose something unique from system information as the key, it would have the added benefit of only being able to decrypt a file on the machine it was encrypted on.
|
|
|
|
 |
|
 |
In that case, a suggestion would be to use public key encryption. That way it doesn't matter who has access to your public key, they still can't change the file.
|
|
|
|
 |