2 Replies - 143 Views - Last Post: 08 June 2019 - 09:08 PM Rate Topic: -----

#1 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 15-January 13

AES Rijndael algorithm to decipher a message: IndexOutOfRangeException

Posted 08 June 2019 - 06:15 PM

Good evening!

I'm trying to decipher a message coded using AES Rijndael algorithm. I tried to do similar to the code posted bellow but I'm getting System.IndexOutOfRangeException. And I have no clue what I'm doing wrong.
https://docs.microso...etframework-4.8

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Decriptador {
	public class Decrip {
		private Byte[] iv;
		private Byte[] key; 
		
		public Decrip(String key) {
			iv = new Byte[] {21, 10, 21, 251, 132, 76, 121, 27, 210, 81, 215, 99, 14, 235, 11, 75};
			this.key = Encoding.ASCII.GetBytes(key);
		}

		public String descripto(String msgEncrypted) {
			String msgDecrypted = "";
			Byte[] cipherText = Encoding.ASCII.GetBytes(msgEncrypted);

			RijndaelManaged rijAlg = new RijndaelManaged();
			ICryptoTransform decryptor = rijAlg.CreateDecryptor(key, iv);
			MemoryStream msDecrypt = new MemoryStream(cipherText);
			CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
			StreamReader srDecrypt = new StreamReader(csDecrypt);

			msgDecrypted = srDecrypt.ReadToEnd();
			

			return msgDecrypted;
		}
	}
}

using System;

namespace Decriptador {
	public class Menu {
		private Decrip decrip;

		public Menu() {
			decrip = new Decrip("123654");
		}

		public void showMenu() {
			Console.WriteLine("Type the encripted message: ");
			//Console.Write("The original message is: " + decrip.descripto(Console.ReadLine());
			Console.Write("The original message is: " + decrip.descripto("RrbjFtq8X7vDlXbyEeOW9w=="));
		}
	}
}

Quote

Unhandled Exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Security.Cryptography.RijndaelManagedTransform.GenerateKeyExpansion (System.Byte[] rgbKey) [0x0011f] in <2db172fd6cc04cc789459631328c1dd5>:0
at System.Security.Cryptography.RijndaelManagedTransform..ctor (System.Byte[] rgbKey, System.Security.Cryptography.CipherMode mode, System.Byte[] rgbIV, System.Int32 blockSize, System.Int32 feedbackSize, System.Security.Cryptography.PaddingMode PaddingValue, System.Security.Cryptography.RijndaelManagedTransformMode transformMode) [0x002fd] in <2db172fd6cc04cc789459631328c1dd5>:0
at System.Security.Cryptography.RijndaelManaged.NewEncryptor (System.Byte[] rgbKey, System.Security.Cryptography.CipherMode mode, System.Byte[] rgbIV, System.Int32 feedbackSize, System.Security.Cryptography.RijndaelManagedTransformMode encryptMode) [0x00024] in <2db172fd6cc04cc789459631328c1dd5>:0
at System.Security.Cryptography.RijndaelManaged.CreateDecryptor (System.Byte[] rgbKey, System.Byte[] rgbIV) [0x00000] in <2db172fd6cc04cc789459631328c1dd5>:0
at Decriptador.Decrip.descripto (System.String msgEncrypted) [0x00018] in <9fd7543e8ab24e81a1b3cbb41762c1b4>:0
at Decriptador.Menu.showMenu () [0x00014] in <9fd7543e8ab24e81a1b3cbb41762c1b4>:0
at Decriptador.Program.Main (System.String[] args) [0x00006] in <9fd7543e8ab24e81a1b3cbb41762c1b4>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Security.Cryptography.RijndaelManagedTransform.GenerateKeyExpansion (System.Byte[] rgbKey) [0x0011f] in <2db172fd6cc04cc789459631328c1dd5>:0
at System.Security.Cryptography.RijndaelManagedTransform..ctor (System.Byte[] rgbKey, System.Security.Cryptography.CipherMode mode, System.Byte[] rgbIV, System.Int32 blockSize, System.Int32 feedbackSize, System.Security.Cryptography.PaddingMode PaddingValue, System.Security.Cryptography.RijndaelManagedTransformMode transformMode) [0x002fd] in <2db172fd6cc04cc789459631328c1dd5>:0
at System.Security.Cryptography.RijndaelManaged.NewEncryptor (System.Byte[] rgbKey, System.Security.Cryptography.CipherMode mode, System.Byte[] rgbIV, System.Int32 feedbackSize, System.Security.Cryptography.RijndaelManagedTransformMode encryptMode) [0x00024] in <2db172fd6cc04cc789459631328c1dd5>:0
at System.Security.Cryptography.RijndaelManaged.CreateDecryptor (System.Byte[] rgbKey, System.Byte[] rgbIV) [0x00000] in <2db172fd6cc04cc789459631328c1dd5>:0
at Decriptador.Decrip.descripto (System.String msgEncrypted) [0x00018] in <9fd7543e8ab24e81a1b3cbb41762c1b4>:0
at Decriptador.Menu.showMenu () [0x00014] in <9fd7543e8ab24e81a1b3cbb41762c1b4>:0
at Decriptador.Program.Main (System.String[] args) [0x00006] in <9fd7543e8ab24e81a1b3cbb41762c1b4>:0

This post has been edited by robgeek: 08 June 2019 - 06:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: AES Rijndael algorithm to decipher a message: IndexOutOfRangeException

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: AES Rijndael algorithm to decipher a message: IndexOutOfRangeException

Posted 08 June 2019 - 08:48 PM

Put a breakpoint on line 16, and step through it. See what your actual input values are.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: AES Rijndael algorithm to decipher a message: IndexOutOfRangeException

Posted 08 June 2019 - 09:08 PM

Read the documentation:

Quote

Parameters
rgbKey
Byte[]
The secret key to be used for the symmetric algorithm. The key size must be 128, 192, or 256 bits.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1