TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Samia Souror
NA
33
5.2k
Index was outside the bounds of the array
Nov 21 2018 7:38 AM
Hi,
I'm trying to encrypt a field in my local database table using my own algorithm. encryption is done well. but decryption produces to me an exception error
Exception thrown: 'System.IndexOutOfRangeException'
my code is
private
void
button10_Click(
object
sender, EventArgs e)
{
OracleConnection conn;
String VtextKey =
this
.txtKey.Text;
using
(conn =
new
OracleConnection(oradb))
{
OracleCommand select =
new
OracleCommand(
"select empno,ENAME,job from emp"
, conn);
conn.Open();
OracleDataReader reader = select.ExecuteReader();
Int64 vempno = 0;
String EnameValue =
""
;
String jobValue =
""
;
String DecryptEname =
""
;
String Decryptjob =
""
;
if
(reader.HasRows)
{
while
(reader.Read())
{
vempno = reader.GetInt64(0);
EnameValue = reader.GetString(1);
jobValue = reader.GetString(2);
Enigma enigma =
new
Enigma();
DecryptEname = enigma.Decrypt(EnameValue, VtextKey);
OracleCommand update =
new
OracleCommand(
"update emp set ename =:fname, job =:job where empno =:empno"
, conn);
OracleParameter ename =
new
OracleParameter(
"ename"
, DecryptEname);
OracleParameter job =
new
OracleParameter(
"job"
, Decryptjob);
OracleParameter empno =
new
OracleParameter(
"empno"
, vempno);
update.Parameters.Add(ename);
update.Parameters.Add(job);
update.Parameters.Add(empno);
update.ExecuteNonQuery();
}
MessageBox.Show(
"User updated!"
);
OracleDataAdapter a =
new
OracleDataAdapter(
"SELECT * FROM emp"
, conn);
DataTable t =
new
DataTable();
a.Fill(t);
dataGridView1.DataSource = t;
reader.Close();
}
}
}
and the exception error
is
on
this
line
PlainBytes[i / 2] = (
byte
)(((Cipher ^ ((Left) ? LeftHash[Mapping] : RightHash[Mapping]))));
this
is
the decryption function
public
String Decrypt(String EncryptedText, String Key)
{
String PlainText =
""
;
Byte[] PlainBytes =
new
Byte[CipherText.Length / 2];
MD5 HashGenerator = MD5.Create();
Byte[] KeyHashBytes = HashGenerator.ComputeHash(Encoding.UTF8.GetBytes(Key));
StringBuilder Builder =
new
StringBuilder();
for
(
int
i = 0; i < KeyHashBytes.Length; i++)
{
Builder.Append(KeyHashBytes[i].ToString(
"x2"
));
}
String KeyHashString = Builder.ToString();
String[] KeyHashChunks =
new
String[2];
KeyHashChunks[0] = KeyHashString.Substring(0, 16);
KeyHashChunks[1] = KeyHashString.Substring(16);
Byte[] LeftHash = Encoding.ASCII.GetBytes(KeyHashChunks[0]);
Byte[] RightHash = Encoding.ASCII.GetBytes(KeyHashChunks[1]);
Byte[] EncryptedBytes = Encoding.ASCII.GetBytes(EncryptedText);
for
(
int
i = 0; i < (EncryptedBytes.Length-1); i += 2)
{
Byte Cipher = EncryptedBytes[i];
Byte Meta = EncryptedBytes[i + 1];
Meta = (
byte
)((
int
)Meta - 33);
Cipher = (
byte
)((
int
)Cipher - 33);
int
Remains = ((
int
)Meta > 32) ? 1 : 0;
Meta = (Remains == 1) ? (
byte
)((
int
)Meta - 32) : Meta;
Boolean Left = ((
int
)Meta > 16) ?
false
:
true
;
Meta = (Left) ? Meta : (
byte
)((
int
)Meta - 16);
int
Mapping = (
int
)Meta;
Cipher = (
byte
)(((
int
)Cipher * 2) + Remains);
PlainBytes[i / 2] = (
byte
)(((Cipher ^ ((Left) ? LeftHash[Mapping] : RightHash[Mapping]))));
}
PlainText = Encoding.ASCII.GetString(PlainBytes);
return
PlainText;
}
Reply
Answers (
3
)
on Server Excel open & write problem occured
Savedatabase 1values tdatabase2 afterinsertingusinganjularjs