Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Oracle 11.2 Encrypt String and .net C# Decrypt String Help Request.

933485Apr 26 2012 — edited Jun 5 2013
I have an 11.2 Oracle DB running which is going to store Urls for a web application. These URLS are to be encrypted to prevent users from manipulating them at all .
Coming out of Demantra Application .

Need to create a usable DBMS_Crypto code that can then be decrypted by the C# application.

So far i am getting different strings out of each side for the encryption piece of the puzzle. .. Any ideas of whats I am missing.


* Oracle Program *

v_encryption_type pls_integer := dbms_crypto.encrypt_3des+
dbms_crypto.chain_ecb +
dbms_crypto.pad_zero;

v_encryption_key raw (128) := utl_i18n.string_to_raw( '0123456789012345678901234', 'AL32UTF8' );

v_encrypted_raw := utl_i18n.string_to_raw(plain_text_in, 'AL32UTF8');


v_encrypted_raw := dbms_crypto.encrypt(src => v_encrypted_raw,
typ => v_encryption_type,
key => v_encryption_key,
IV => NULL);

v_encrypted_raw := utl_encode.base64_encode(v_encrypted_raw);

v_encrypted_char := utl_raw.cast_to_varchar2(v_encrypted_raw);

v_encrypted_char := replace(v_encrypted_char,'+',' ');

return v_encrypted_char;



C# Program

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

using System.IO;

namespace Verification
{
public class Cryptography
{
private TripleDESCryptoServiceProvider des;

public readonly static string ENCRYPTION_KEY;

private MD5CryptoServiceProvider hashmd5;
private TextWriter tw;
private FileStream fileStream;

static Cryptography()
{
Cryptography.ENCRYPTION_KEY = "123456789012345678901234";

}

public Cryptography()
{
this.des = new TripleDESCryptoServiceProvider();
this.hashmd5 = new MD5CryptoServiceProvider();
this.des.Padding = PaddingMode.Zeros;



}

public string DecryptQueryString(string strQueryString)
{
string message;
string empty = string.Empty;
fileStream = new FileStream("cryptographydebug.txt", FileMode.Append);
// create a writer and open the file
tw = new StreamWriter(fileStream);
try
{
tw.WriteLine("Inside DecryptQueryString");
this.des.Key = Encoding.ASCII.GetBytes(Cryptography.ENCRYPTION_KEY);
this.des.Mode = CipherMode.ECB;
this.des.Padding = PaddingMode.Zeros;
ICryptoTransform cryptoTransform = this.des.CreateDecryptor();
byte[] numArray = Convert.FromBase64String(strQueryString);
empty = Encoding.UTF8.GetString(cryptoTransform.TransformFinalBlock(numArray, 0, (int)numArray.Length));
tw.WriteLine("hashmd5.GetHashCode");
tw.WriteLine(hashmd5.GetHashCode());
tw.WriteLine("Des.Key" + des.Key.ToString());

tw.WriteLine("hashmd5.ToString");
tw.WriteLine(hashmd5.ToString());
tw.WriteLine("des.GetHashCode");
tw.WriteLine(des.GetHashCode());
tw.WriteLine("des.ToString");
tw.WriteLine(des.ToString());
tw.WriteLine("cryptoTransform.GetHashCode");
tw.WriteLine(cryptoTransform.GetHashCode());
tw.WriteLine("cryptoTransform.ToString");
tw.WriteLine(cryptoTransform.ToString());
tw.WriteLine("DecryptQueryString");
tw.WriteLine(empty);
tw.Close();
}
catch (Exception exception1)
{
Exception exception = exception1;
message = exception.Message;
tw.WriteLine(message);
tw.Close();
return message;
}
message = empty;
return message;
}

public string EncryptQueryString(string strQueryString)
{
string message;
string empty = string.Empty;
fileStream = new FileStream("cryptographydebug.txt", FileMode.Append);
// create a writer and open the file
tw = new StreamWriter(fileStream);
try
{
tw.WriteLine(" Inside EncryptQueryString");
this.des.Key = Encoding.ASCII.GetBytes(Cryptography.ENCRYPTION_KEY);

this.des.Mode = CipherMode.ECB;
this.des.Padding = PaddingMode.Zeros;

ICryptoTransform cryptoTransform = this.des.CreateEncryptor();

ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();

byte[] bytes = Encoding.UTF8.GetBytes(strQueryString);
empty = Convert.ToBase64String(cryptoTransform.TransformFinalBlock(bytes, 0, (int)bytes.Length));
tw.WriteLine("Starting String");
tw.WriteLine(strQueryString);
tw.WriteLine("Des key" + Encoding.UTF8.GetString(des.Key));
tw.WriteLine("hashmd5.GetHashCode");
tw.WriteLine(hashmd5.GetHashCode());
tw.WriteLine("hashmd5.ToString");
tw.WriteLine(hashmd5.ToString());
tw.WriteLine("des.GetHashCode");
tw.WriteLine(des.GetHashCode());
tw.WriteLine("des.ToString");
tw.WriteLine(des.ToString());
tw.WriteLine("cryptoTransform.GetHashCode");
tw.WriteLine(cryptoTransform.GetHashCode());
tw.WriteLine("cryptoTransform.ToString");
tw.WriteLine(cryptoTransform.ToString());
tw.WriteLine("aSCIIEncoding.ToString");
tw.WriteLine(aSCIIEncoding.ToString());
tw.WriteLine("aSCIIEncoding.GetHashCode");
tw.WriteLine(aSCIIEncoding.GetHashCode());
tw.WriteLine("EncryptQueryString");
tw.WriteLine(empty);
tw.Close();

}
catch (Exception exception1)
{
Exception exception = exception1;
message = exception.Message;
tw.WriteLine(message);
tw.Close();
return message;
}
message = empty;
return message;
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 3 2013
Added on Apr 26 2012
1 comment
2,015 views