I have some code that converts a string into base64 for some
encryption.
It was written on the 1.1 framework but I am trying to get it to work
on 2.0. It throws exceptions on the cryptStream.Flu shFinalBlock() and
cryptStream.Cle ar(). Can anyone tell me what is up with this?
Thanks ahead of time for any help you can offer.
public static string EncryptAndBase6 4( string val )
{
byte[] buf = UnicodeEncoding .Unicode.GetByt es(val);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(me mStream, enc3des,
CryptoStreamMod e.Write);
cryptStream.Wri te(buf, 0, buf.Length);
cryptStream.Flu shFinalBlock();
cryptStream.Cle ar();
Array.Clear(buf , 0, buf.Length);
buf = memStream.GetBu ffer();
val = Convert.ToBase6 4String(buf, 0, (int)memStream. Length);
Array.Clear(buf , 0, (int)memStream. Length);
return val;
}
public static object DecryptFromBase 64( object val )
{
if( val.Equals( DBNull.Value ) )
return DBNull.Value;
return DecryptFromBase 64( (string)val );
}
public static string DecryptFromBase 64( string base64str )
{
byte[] buf = Convert.FromBas e64String(base6 4str);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(me mStream, dec3des,
CryptoStreamMod e.Write );
cryptStream.Wri te(buf, 0, buf.Length);
cryptStream.Flu shFinalBlock();
cryptStream.Cle ar();
Array.Clear(buf , 0, buf.Length);
buf = memStream.GetBu ffer();
string ret = UnicodeEncoding .Unicode.GetStr ing(buf, 0,
(int)memStream. Length);
Array.Clear(buf , 0, (int)memStream. Length);
memStream.Close ();
return ret;
} 6 1818
SenseiHitokiri <se************ @gmail.comwrote :
I have some code that converts a string into base64 for some
encryption.
It was written on the 1.1 framework but I am trying to get it to work
on 2.0. It throws exceptions on the cryptStream.Flu shFinalBlock() and
cryptStream.Cle ar(). Can anyone tell me what is up with this?
Thanks ahead of time for any help you can offer.
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
hmm I swore I posted the code up in here but I don't see it. Well here
it is again. C#
using System;
using System.Security .Cryptography;
using System.Text;
namespace DebugCrypto
{
class DebugCrypto
{
static void Main(string[] args)
{
DebugCrypto x = new DebugCrypto();
Console.Read();
}
void Program()
{
string base64str = "FmcT1N0kYX/IId4m6GuYpg=="; //string to
decrypt
ICryptoTransfor m dec3des = SetupCrypto();
byte[] buf = Convert.FromBas e64String(base6 4str);
System.IO.Memor yStream memStream = new
System.IO.Memor yStream();
CryptoStream cryptStream = new CryptoStream(me mStream,
dec3des, CryptoStreamMod e.Write);
cryptStream.Wri te(buf, 0, buf.Length);
try
{
cryptStream.Flu shFinalBlock(); //This is what
errors out
cryptStream.Cle ar(); // and this
}
catch (Exception ex)
{
Console.WriteLi ne(ex.ToString( ));
}
Array.Clear(buf , 0, buf.Length);
buf = memStream.GetBu ffer();
Console.WriteLi ne(UnicodeEncod ing.Unicode.Get String(buf,
0, (int)memStream. Length));
Array.Clear(buf , 0, (int)memStream. Length);
memStream.Close ();
}
ICryptoTransfor m SetupCrypto()
{
TripleDESCrypto ServiceProvider des3 = new
TripleDESCrypto ServiceProvider ();
byte[] salt =
System.Text.UTF 8Encoding.UTF8. GetBytes("testK ey"); //Encryption key
SHA1 hash = new SHA1Managed();
for (int i = 0; i < 200; ++i)
{
salt = hash.ComputeHas h(salt, 0, salt.Length);
hash.Initialize ();
}
PasswordDeriveB ytes pdb = new
PasswordDeriveB ytes("testKey", salt, "SHA512", 1000);
Array.Clear(sal t, 0, salt.Length);
hash.Clear();
byte[] key3des = pdb.GetBytes(24 );
byte[] iv3des = pdb.GetBytes(8) ;
return( des3.CreateDecr yptor(key3des, iv3des) );
}
}
}
SenseiHitokiri <se************ @gmail.comwrote :
hmm I swore I posted the code up in here but I don't see it. Well here
it is again. C#
<snip>
Well, the exception (after putting in a call to x.Program() - your
program as posted does nothing :) suggests that the data is invalid.
We'll need to see a short but complete program that produces that data
to start with, too.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
On Mar 20, 3:05 am, Jon Skeet [C# MVP] <s...@pobox.com wrote:
SenseiHitokiri <senseihitok... @gmail.comwrote :
hmm I swore I posted the code up in here but I don't see it. Well here
it is again. C#
<snip>
Well, the exception (after putting in a call to x.Program() - your
program as posted does nothing :) suggests that the data is invalid.
We'll need to see a short but complete program that produces that data
to start with, too.
--
Jon Skeet - <s...@pobox.com >http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Hmm... that may be a little harder. I'm taking over this code from
someone else and at the moment that is in a seperate app that I do not
have access to. I can try to rip out the encrypt code from this
program and throw it up here but that may take me a bit to discern
it. Damn I wish he gave me some documentation lol.
Ok give this a whirl. It generates the encrypted string and prints it
to the screen but then gives me a bad data error when trying to
decrypt.
using System;
using System.Security .Cryptography;
using System.Text;
using System.IO;
namespace DebugCrypto
{
class DebugCrypto
{
ICryptoTransfor m enc3des, dec3des;
byte[] key3des = null;
byte[] iv3des = null;
static void Main(string[] args)
{
DebugCrypto x = new DebugCrypto();
string read = Console.ReadLin e();
while ( read != "exit" )
{
string encrypted = x.EncryptAndBas e64(read);
Console.WriteLi ne("Encrypting : ");
Console.WriteLi ne(encrypted);
Console.WriteLi ne("Decrypting : ");
Console.WriteLi ne(x.DecryptFro mBase64(encrypt ed));
Console.WriteLi ne("\n");
}
}
public DebugCrypto()
{
string sKey = "testKey";
TripleDESCrypto ServiceProvider des3 = new
TripleDESCrypto ServiceProvider ();
byte[] salt =
System.Text.UTF 8Encoding.UTF8. GetBytes(sKey);
SHA1 hash = new SHA1Managed();
for (int i = 0; i < 200; ++i)
{
salt = hash.ComputeHas h(salt, 0, salt.Length);
hash.Initialize ();
}
PasswordDeriveB ytes pdb = new PasswordDeriveB ytes(sKey,
salt, "SHA512", 1000);
Array.Clear(sal t, 0, salt.Length);
hash.Clear();
key3des = pdb.GetBytes(24 );
iv3des = pdb.GetBytes(8) ;
enc3des = des3.CreateEncr yptor(key3des, iv3des);
dec3des = des3.CreateDecr yptor(key3des, iv3des);
des3.Clear();
}
public string EncryptAndBase6 4(string val)
{
byte[] buf = UnicodeEncoding .Unicode.GetByt es(val);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(me mStream,
enc3des, CryptoStreamMod e.Write);
cryptStream.Wri te(buf, 0, buf.Length);
int memLength = (int)memStream. Length;
cryptStream.Flu shFinalBlock();
cryptStream.Cle ar();
Array.Clear(buf , 0, buf.Length);
buf = memStream.GetBu ffer();
val = Convert.ToBase6 4String(buf, 0, memLength );
Array.Clear(buf , 0, memLength );
return val;
}
public string DecryptFromBase 64(string base64str)
{
byte[] buf = Convert.FromBas e64String(base6 4str);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(me mStream,
dec3des, CryptoStreamMod e.Write);
cryptStream.Wri te(buf, 0, buf.Length);
int memLength = (int)memStream. Length;
cryptStream.Flu shFinalBlock();
cryptStream.Cle ar();
Array.Clear(buf , 0, buf.Length);
buf = memStream.GetBu ffer();
string ret = UnicodeEncoding .Unicode.GetStr ing(buf, 0,
memLength);
Array.Clear(buf , 0, memLength );
memStream.Close ();
return ret;
}
}
}
SenseiHitokiri <se************ @gmail.comwrote :
Ok give this a whirl. It generates the encrypted string and prints it
to the screen but then gives me a bad data error when trying to
decrypt.
The encryption is broken:
public string EncryptAndBase6 4(string val)
{
byte[] buf = UnicodeEncoding .Unicode.GetByt es(val);
MemoryStream memStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(me mStream,
enc3des, CryptoStreamMod e.Write);
cryptStream.Wri te(buf, 0, buf.Length);
int memLength = (int)memStream. Length;
cryptStream.Flu shFinalBlock();
cryptStream.Cle ar();
Array.Clear(buf , 0, buf.Length);
buf = memStream.GetBu ffer();
val = Convert.ToBase6 4String(buf, 0, memLength );
Array.Clear(buf , 0, memLength );
return val;
}
Note how it works out the length of memStream *before* it flushes the
final block. Just move that line to *after* cryptStream.Flu shFinalBlock
(and do the same in the decryption) and it works. However, it's not the
nicest way of doing it. It's simpler to use MemoryStream.To Array to get
the buffer, rather than asking for the length at all. I would
personally write the decryption code as (the encryption side mirrors
it, of course):
public string DecryptFromBase 64(string base64str)
{
byte[] buf = Convert.FromBas e64String(base6 4str);
using (MemoryStream memStream = new MemoryStream())
{
using (CryptoStream cryptStream = new CryptoStream
(memStream, dec3des, CryptoStreamMod e.Write))
{
cryptStream.Wri te(buf, 0, buf.Length);
// I don't think this is necessary in .NET 2.0
// but it is in 1.1 due to a bug in CryptoStream,
// IIRC
cryptStream.Flu shFinalBlock();
}
byte[] decrypted = memStream.ToArr ay();
return Encoding.Unicod e.GetString(dec rypted);
}
}
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Simon Roses Femerling |
last post by:
Hey all :)
I'm looking for a good crypto toolkit (that should be multiplatform, work on python 2.3, etc..)
What I have found looks old and too complex (installing 3 party libs, etc.)
I just want to encrypt files using a secure encryption.
I guess a pure python toolkit is out of the question!
Any comments ?
|
by: Stu |
last post by:
Hi,
Im reading a file in from disk as a byte array then passing it to a memory
stream for decryption using crypto api functions. What I have found is that
you need to
reduce the array length by 2 from the original lenght in order to get it to
work
as there seems to be 2 extra 0 bytes at the end.
Functions included
Stu
|
by: Bryan Olson |
last post by:
The current Python standard library provides two cryptographic
hash functions: MD5 and SHA-1 . The authors of MD5 originally
stated:
It is conjectured that it is computationally infeasible to
produce two messages having the same message digest.
That conjecture is false, as demonstrated by Wang, Feng, Lai and
Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short-
cut solution for finding collisions in SHA-1 . Their result
|
by: Michael Sparks |
last post by:
Hi,
I suspect this is a bug with AMK's Crypto package from
http://www.amk.ca/python/code/crypto , but want to
check to see if I'm being dumb before posting a bug
report.
I'm looking at using this library and to familiarise myself writing
small tests with each of the ciphers. When I hit Crypto.Cipher.ARC4 I've
|
by: Andy Chau |
last post by:
I try to use RSA to implement the following scheme but wasn't sucessful.
Sever encrypt a message using a public key, the client decrpyt the message
using a private key.
I don't want the client to be able to encrypt a message.
However, using the Crypto API I need to pass in both the private and public
key pairs in order to decrypt the message.
When the client has both private and public key, it can just use the public
| |
by: Mark |
last post by:
I have been playing around with encrypting passwords using a class found in a
MS KB (see farther down). It seems to work great so long as the original
password is comprised of characters on the keyboard. However, if the
password is mixed with characters in both ASCII code set 0-127 and 128-255, I
run into a problem. Users can set a password using keyboard characters and
by holding ALT and typing in the decimal value for the non-keyboard...
|
by: Slug |
last post by:
Hello all,
I've been trying to get a public key solution working but have been having a
few problems. For starters there is a lot of contradictory information out
there, MSDN is not much help, and a lot of the sample code available I have
found are buggy so don't provide much insight.
Unfortunately for me every developer I know either gives me a completely
blank look when I try to talk crypto, or they have some wildly inaccurate
and...
|
by: vermarajeev |
last post by:
Hi guys,
I want to encrypt/decrypt a file
with AES in CTR mode using crypto++ library.
To encrypt a file using AES in CTR mode
the solution is something like this
int CRYPTOPP_API main(int argc, char *argv)
{
std::string command, executableName, macFilename;
|
by: vermarajeev |
last post by:
Hi guys,
I have written code to encrypt and decrypt files using perl script. Please help me to port below code to crypto++ library.
//ENCRYPTION
my $cipher = Crypt::CBC->new(
-cipher => "Crypt::Rijndael",
-key => $key,
-header => 'salt',
);
|
by: Fett |
last post by:
I need a crypto package that works on windows with python 2.5. Can
anyone suggest one for me?
I have been searching for a couple days for a good cryptography
package to use for public/private key encryption, at this point I
would settle for symmetric even.
Every encryption package I have found for python was either operating
system specific (read *nix only):
http://www.freenet.org.nz/ezPyCrypto/
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |