473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

encryption problems

This is the problem: I do not get the output I need when encoding and
decoding data using rijndael alghoritm.
Look at the code and see what the problem is actually:

Please paste this code into your Visual Studio and compile it + run it; so
you can see what the actual problem is.

Thanks.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. using System.IO;
  4.  
  5. using System.Text;
  6.  
  7. using System.Security.Cryptography;
  8.  
  9. namespace ConsoleApplication1
  10.  
  11. {
  12.  
  13. class MyMainClass
  14.  
  15. {
  16.  
  17. public static void Main()
  18.  
  19. {
  20.  
  21. string original = "Original string to be sent.";
  22.  
  23. string roundtrip;
  24.  
  25. ASCIIEncoding textConverter = new ASCIIEncoding();
  26.  
  27. RijndaelManaged myRijndael = new RijndaelManaged();
  28.  
  29. byte[] fromEncrypt;
  30.  
  31. byte[] encrypted;
  32.  
  33. byte[] toEncrypt;
  34.  
  35. byte[] key;
  36.  
  37. byte[] IV;
  38.  
  39. //Create a new key and initialization vector.
  40.  
  41. myRijndael.GenerateKey();
  42.  
  43. myRijndael.GenerateIV();
  44.  
  45. //Get the key and IV.
  46.  
  47. key = myRijndael.Key;
  48.  
  49. IV = myRijndael.IV;
  50.  
  51. //Get an encryptor.
  52.  
  53. ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
  54.  
  55.  
  56.  
  57. //Encrypt the data.
  58.  
  59. MemoryStream msEncrypt = new MemoryStream();
  60.  
  61. CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor,
  62. CryptoStreamMode.Write);
  63.  
  64. //Convert the data to a byte array.
  65.  
  66. toEncrypt = textConverter.GetBytes(original);
  67.  
  68. //Write all data to the crypto stream and flush it.
  69.  
  70. csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
  71.  
  72. csEncrypt.FlushFinalBlock();
  73.  
  74. //Get encrypted array of bytes.
  75.  
  76. encrypted = msEncrypt.ToArray();
  77.  
  78. //Here I send data trough network stream
  79.  
  80.  
  81.  
  82. //create byte array to be sent trough tcp network
  83.  
  84. byte[] finalized = new byte[key.Length+IV.Length+encrypted.Length];
  85.  
  86. //merge all values into single byte array
  87.  
  88. key.CopyTo(finalized,0);
  89.  
  90. IV.CopyTo(finalized,32);
  91.  
  92. encrypted.CopyTo(finalized,48);
  93.  
  94.  
  95.  
  96. //here goes tcp code with sending the array trough network. it works fine,
  97. and is no problem. For simplicitiy's sake, here i'll just simulate new
  98. application that uses values it got from the first application.
  99.  
  100.  
  101.  
  102. //SIMULATED NEW APPLICATION
  103.  
  104. //Create values that will be used in decryption process and that are passed
  105. trough network
  106.  
  107. byte[] key1 = new byte[32];
  108.  
  109. byte[] IV1 = new byte[16];
  110.  
  111. byte[] encrypted1 = new byte[finalized.Length-48];
  112.  
  113. //read all values from the passed byte array and divid those correctly.
  114.  
  115. for (int i=0; i<32; i++)
  116.  
  117. {
  118.  
  119. key1[i]=finalized[i];
  120.  
  121. }
  122.  
  123. for (int i=32; i<48; i++)
  124.  
  125. {
  126.  
  127. IV1[i-32]=finalized[i];
  128.  
  129. }
  130.  
  131. for (int i=48; i<finalized.Length; i++)
  132.  
  133. {
  134.  
  135. encrypted1[i-48]=finalized[i];
  136.  
  137. }
  138.  
  139. //now use values to get the result:
  140.  
  141.  
  142.  
  143. //Get a decryptor that uses the same key and IV as the encryptor.
  144.  
  145. ICryptoTransform decryptor = myRijndael.CreateDecryptor(key1, IV1);
  146.  
  147. //Now decrypt the previously encrypted message using the decryptor
  148.  
  149. MemoryStream msDecrypt = new MemoryStream(encrypted1);
  150.  
  151. CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
  152. CryptoStreamMode.Read);
  153.  
  154. fromEncrypt = new byte[encrypted1.Length];
  155.  
  156. //Read the data out of the crypto stream.
  157.  
  158. csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
  159.  
  160. //Convert the byte array back into a string.
  161.  
  162. roundtrip = textConverter.GetString(fromEncrypt);
  163.  
  164. //Display the original data and the decrypted data to see where the actual
  165. problem is:
  166.  
  167. Console.WriteLine("Original string: {0}", original + "_");
  168.  
  169. Console.WriteLine("String I got to another application: {0}", roundtrip +
  170. "_");
  171.  
  172. //Guess what! The result string has some dummy stuff at the end and it is
  173. just not the data I encoded. It is actually there, but I really don't want
  174. that shit at the end. I placed "_" sign just to see that there is a problem
  175. with data I got.
  176.  
  177.  
  178.  
  179. }
  180.  
  181. }
  182.  
  183.  
  184.  
  185. }
  186.  
  187.  

Nov 22 '05 #1
0 705

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hal Vaughan | last post by:
I have no background in encryption, so I'm working with samples I've found in various places and patching them together. I know Blowfish can use a 56 byte key. The version of this program in Perl...
34
by: Blake T. Garretson | last post by:
I want to save some sensitive data (passwords, PIN numbers, etc.) to disk in a secure manner in one of my programs. What is the easiest/best way to accomplish strong file encryption in Python? ...
7
by: helmut woess | last post by:
Hi, has anybody knowledge about the safetyness of encrypting stored procs in SQL-Server 2005 using WITH ENCRYPTION? Or can they be hacked with the same old tools which exists for SQL 2000? ...
3
by: Molly Gibson | last post by:
Hi all, I have recently installed Apache/1.3.28 + mod_auth_pgsql-0.9.12 (http://www.giuseppetanzilli.it/mod_auth_pgsql/) The only way I have been able to get it to successfully authenticate...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
2
by: tfoxusenet | last post by:
Hi, I need to encrypt/decrypt some data for my C# application that can also be read on a unix system, and need a quick, simple, cross-platform solution I can embed in my own code that doesn't...
5
by: OFM | last post by:
I am running an oracle database with the application written in PHP. I would like to be able to have the option to encrypt data residing in certain columns in certain tables i.e. encrypt the SSNO...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
4
by: Fritjolf | last post by:
Hi. I've got a strange problem... I've made a simple program to test encryption/decryption. I use Rijndael encryption and here are the most important properties. RijndaelManaged cipher =...
0
jeffstl
by: jeffstl | last post by:
After checking the box "Force Protocol Encryption" on SQL Server Network Utility there are several Crystal reports that begin to throw the following error when run Query Engine Error:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.