Why don't you use the binarywriter class to writer your encrypted data to the file and then decrypt it later using the binaryreader. See a sample (although this is a V1 application) here but the author uses a binaryreader and binarywriter to store the encrypted data to a file.
Mark, Thank you for the advice. I'll try it to see if I can get it working withoug converting it to a string, but eventually I will need to get a string out of it.
Yes. I'm trying to get it to work using the openCFNet objects and I'm having problems. I've never had any luck getting answers on their forum, so that's why I asked if anyone here had done it.
I can encrypt data without any problems, but when I try to decrypt it I get a 'bad data' error. For proof of concept purposes, both keys are hardcoded in the project.
Here is my code. I don't know how to format it nicely for the forum, sorry...
Thanks!
public
string EncryptDataString(string strInput)
{
string xmlString;
InitObject();
xmlString = pvtstr_JustPublicKey;
if (strInput.Length <=0)
return null;
try
{
xmlString = xmlString.Substring( 0);
RSACryptoServiceProvider RSAProvider =
new OpenNETCF.Security.Cryptography.RSACryptoServiceProvider();
Mark, that was dead on. Thank you! I feel like I'm closer to a solution now.
I can encrypt the string and convert it to a byte array. I can then decrypt the byte array. However, I must be able to decrypt a string...it's unavoidable. I have to store the string in a text file to be used later. I'm having troubles converting the encrypted string to a byte array and they byte array to a string. Do you know...or anyone...how to do this The following code does not seem to work...
RSA Encryption
jmckown
Hello
Let's open my query...Does ANYONE use ANY KIND of .net encryption on the pda Is this possible
Please help
antnyuk
bump...
Any advice
Carl Perkins
That would be yes and yes.
I've a question of my own: does your code work as expected on the desktop using RSA class from System.Security.Cryptography
Anton V. Tolkachev
Hi,
Why don't you use the binarywriter class to writer your encrypted data to the file and then decrypt it later using the binaryreader. See a sample (although this is a V1 application) here but the author uses a binaryreader and binarywriter to store the encrypted data to a file.
http://www.eggheadcafe.com/articles/compactframeworkencryption.asp
Thanks.
Bob M
do you have a specific question you would ask if there were
Sanjay sagar
Mark, Thank you for the advice. I'll try it to see if I can get it working withoug converting it to a string, but eventually I will need to get a string out of it.
Ian Donald
Yes, it works on the pc. I had to fiddle with some stuff because of differences in the framework. Here's my code for the pc:
Thank you for any help!!
public string EncryptDataString(string strInput)
{
string xmlString;
xmlString = pvtstr_JustPublicKey;
if (strInput.Length <=0)
return null;
//try
//{
xmlString = xmlString.Substring( xmlString.IndexOf( str_CreateString +"!!!" ) + 13);
RSACryptoServiceProvider RSAProvider = new System.Security.Cryptography.RSACryptoServiceProvider();
// RSAParameters parameters = new System.Security.Cryptography.RSAParameters();
RSAProvider.FromXmlString( xmlString );
System.Int32 numberOfBlocks =( strInput.Length / 32 ) + 1;
System.Char[] charArray = strInput.ToCharArray();
System.Byte[][] byteBlockArray = new byte[ numberOfBlocks ][];
System.Int32 incrementer = 0;
for( System.Int32 i = 1; i <= numberOfBlocks; i++ )
{
if( i == numberOfBlocks )
{
byteBlockArray[ i - 1 ] = System.Text.ASCIIEncoding.ASCII.GetBytes( charArray, incrementer, charArray.Length - incrementer );
}
else
{
byteBlockArray[ i - 1 ] = System.Text.ASCIIEncoding.ASCII.GetBytes( charArray, incrementer, 32 );
incrementer += 32;
}
}
System.String tempStorage = strInput;
strInput = "";
for( System.Int32 j = 0; j < byteBlockArray.Length; j++ )
{
strInput = strInput + System.Convert.ToBase64String( RSAProvider.Encrypt( byteBlockArray[ j ], true ) ) ;
}
return strInput;
}
public string DecryptDataString(string strInput)
{
string xmlString = pvtstr_PublicAndPrivateKey;
InitObject();
xmlString = xmlString.Substring( strInput.IndexOf( "</KeySize>" ) + 12 );
RSACryptoServiceProvider RSAProvider = new RSACryptoServiceProvider();
RSAParameters parameters = new RSAParameters();
RSAProvider.FromXmlString( xmlString );
System.String encryptedBlock = "";
System.Collections.Queue encryptedBlocks = new System.Collections.Queue();
while( strInput.Length != 0 )
{
encryptedBlock = strInput.Substring( 0, strInput.IndexOf( "==" ) + 2 );
encryptedBlocks.Enqueue( encryptedBlock );
strInput = strInput.Remove( 0, encryptedBlock.Length );
}
encryptedBlocks.TrimToSize();
System.Int32 numberOfBlocks = encryptedBlocks.Count;
for( System.Int32 i = 1; i <= numberOfBlocks; i++ )
{
encryptedBlock =( System.String )encryptedBlocks.Dequeue();
strInput = strInput + System.Text.ASCIIEncoding.ASCII.GetString( RSAProvider.Decrypt( System.Convert.FromBase64String( encryptedBlock ), true ) ) ;
}
return strInput;
}
fastdev
Yes. I'm trying to get it to work using the openCFNet objects and I'm having problems. I've never had any luck getting answers on their forum, so that's why I asked if anyone here had done it.
I can encrypt data without any problems, but when I try to decrypt it I get a 'bad data' error. For proof of concept purposes, both keys are hardcoded in the project.
Here is my code. I don't know how to format it nicely for the forum, sorry...
Thanks!
public
string EncryptDataString(string strInput){
string xmlString;InitObject();
xmlString = pvtstr_JustPublicKey;
if (strInput.Length <=0) return null; try{
xmlString = xmlString.Substring( 0);
RSACryptoServiceProvider RSAProvider =
new OpenNETCF.Security.Cryptography.RSACryptoServiceProvider();RSAProvider.FromXmlString( xmlString );
System.Int32 numberOfBlocks =( strInput.Length / 32 ) + 1;
System.Char[] charArray = strInput.ToCharArray();
System.Byte[][] byteBlockArray =
new byte[ numberOfBlocks ][];System.Int32 incrementer = 0;
for( System.Int32 i = 1; i <= numberOfBlocks; i++ ){
if( i == numberOfBlocks ){
byteBlockArray[ i - 1 ] = System.Text.ASCIIEncoding.ASCII.GetBytes( charArray, incrementer, charArray.Length - incrementer );
}
else{
byteBlockArray[ i - 1 ] = System.Text.ASCIIEncoding.ASCII.GetBytes( charArray, incrementer, 32 );
incrementer += 32;
}
}
System.String tempStorage = strInput;
strInput = "";
for( System.Int32 j = 0; j < byteBlockArray.Length; j++ ){
strInput = strInput + System.Convert.ToBase64String( RSAProvider.EncryptValue( byteBlockArray[ j ]),0,byteBlockArray.Length ) ;
}
}
catch(Exception ex){
string s = ex.Message;}
return strInput;}
public string DecryptDataString(string strInput){
string xmlString = pvtstr_PublicAndPrivateKey;InitObject();
xmlString = xmlString.Substring( 0 );
RSACryptoServiceProvider RSAProvider =
new RSACryptoServiceProvider(KeySpec.KEYEXCHANGE,true);RSAParameters parameters =
new RSAParameters();RSAProvider.FromXmlString( xmlString );
System.String encryptedBlock = "";
System.Collections.Queue encryptedBlocks =
new System.Collections.Queue(); while( strInput.Length != 0 ){
encryptedBlock = strInput.Substring( 0, strInput.IndexOf( "==" ) + 2 );
encryptedBlocks.Enqueue( encryptedBlock );
strInput = strInput.Remove( 0, encryptedBlock.Length );
}
encryptedBlocks.TrimToSize();
System.Int32 numberOfBlocks = encryptedBlocks.Count;
for( System.Int32 i = 1; i <= numberOfBlocks; i++ ){
try{
encryptedBlock =( System.String )encryptedBlocks.Dequeue();
strInput = strInput + System.Text.ASCIIEncoding.ASCII.GetString( RSAProvider.DecryptValue( System.Convert.FromBase64String( encryptedBlock )),0,encryptedBlock.Length ) ;
}
catch(Exception e){
MessageBox.Show(e.Message);
}
}
return strInput;}
PrashG
Try keeping the encrypted data as a byte[], don't turn it into a string and back.
Malleswar
Mark, that was dead on. Thank you! I feel like I'm closer to a solution now.
I can encrypt the string and convert it to a byte array. I can then decrypt the byte array. However, I must be able to decrypt a string...it's unavoidable. I have to store the string in a text file to be used later. I'm having troubles converting the encrypted string to a byte array and they byte array to a string. Do you know...or anyone...how to do this The following code does not seem to work...
Thanks!
Paula
while( strInput.Length != 0 ){
encryptedBlock = strInput.Substring( 0, strInput.IndexOf( "==" ) + 2 );
encryptedBlocks.Enqueue( encryptedBlock );
strInput = strInput.Remove( 0, encryptedBlock.Length );
}
encryptedBlocks.TrimToSize();
System.Int32 numberOfBlocks = encryptedBlocks.Count;
for( System.Int32 i = 1; i <= numberOfBlocks; i++ ){
try{
encryptedBlock =( System.String )encryptedBlocks.Dequeue();
strInput = strInput + System.Text.ASCIIEncoding.ASCII.GetString( RSAProvider.DecryptValue( System.Convert.FromBase64String( encryptedBlock )),0,encryptedBlock.Length ) ;
}
catch(Exception e){
MessageBox.Show(e.Message);
}
}
johoja
{
string xmlString;InitObject();
xmlString = pvtstr_JustPublicKey;
if (strInput.Length <=0) return null; try{
RSACryptoServiceProvider RSAProvider =
new OpenNETCF.Security.Cryptography.RSACryptoServiceProvider();RSAProvider.FromXmlString( xmlString );
System.Int32 numberOfBlocks =( strInput.Length / 32 ) + 1;
System.Char[] charArray = strInput.ToCharArray();
System.Byte[][] byteBlockArray =
new byte[ numberOfBlocks ][];System.Int32 incrementer = 0;
for( System.Int32 i = 1; i <= numberOfBlocks; i++ ){
if( i == numberOfBlocks ){
byteBlockArray[ i - 1 ] = System.Text.ASCIIEncoding.ASCII.GetBytes( charArray, incrementer, charArray.Length - incrementer );
}
else{
byteBlockArray[ i - 1 ] = System.Text.ASCIIEncoding.ASCII.GetBytes( charArray, incrementer, 32 );
incrementer += 32;
}
}
System.String tempStorage = strInput;
strInput = "";
byte[] byte_EncryptedByte = null; for( System.Int32 j = 0; j < byteBlockArray.Length; j++ ){
byte_EncryptedByte = RSAProvider.EncryptValue( byteBlockArray[ j ]);
}
//write the value to a file using binary writerBinaryWriter obj_Writer =
new BinaryWriter(File.Open("Program Files\\PDAServAnywhere\\tmp.txt",FileMode.Create));obj_Writer.Write(byte_EncryptedByte);
obj_Writer.Close();
BinaryReader obj_Reader =
new BinaryReader(File.Open("Program Files\\PDAServAnywhere\\tmp.txt",FileMode.Open,FileAccess.Read));obj_Reader.BaseStream.Position = 0;
byte[] byte_ToDecryptByte = null; //should figure out the size later, but it worksbyte_ToDecryptByte = obj_Reader.ReadBytes(1000);
string str_Return = DecryptDataString(byte_ToDecryptByte);}
catch(Exception ex){
MessageBox.Show(ex.Message);
}
return str_Return;}
public string DecryptDataString(byte[] strInput){
RSACryptoServiceProvider RSAProvider =
new RSACryptoServiceProvider(KeySpec.KEYEXCHANGE,true);RSAParameters parameters =
new RSAParameters(); string xmlString = pvtstr_PublicAndPrivateKey;RSAProvider.FromXmlString( xmlString );
byte[] byte_DecryptedByte = null; try{
byte_DecryptedByte = RSAProvider.DecryptValue(strInput);
}
catch(Exception e){
MessageBox.Show(e.Message);
}
string str_Return = System.Text.ASCIIEncoding.ASCII.GetString(byte_DecryptedByte,0,byte_DecryptedByte.Length); return str_Return;}
SWBgHz
Mark, that worked great! It's all working.
I'll clean up my code and post it here for anyone else who might need it.
Thank you!!
Studio Two
This is not an answer...
It should not be marked as an answer.