473,512 Members | 14,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.  

Jul 21 '05 #1
0 882

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

Similar topics

2
10154
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
4063
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
2806
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
4989
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
12186
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
2402
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
2557
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
2717
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
5660
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
1295
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
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
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
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
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,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
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...
0
1585
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 ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
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...

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.