im working on webapplication in asp.net with oracle Database12c im using aes128 stored procedure to encrypt the password here is the procedure that makes encryption
DECLARE
l_user_id test.username%TYPE := 'SCOTT';
l_user_psw VARCHAR2 (2000) := 'mypassword123';
l_key VARCHAR2 (2000) := '1234567890999999';
l_mod NUMBER
:= DBMS_CRYPTO.ENCRYPT_AES128
+ DBMS_CRYPTO.CHAIN_CBC
+ DBMS_CRYPTO.PAD_PKCS5;
l_enc RAW (2000);
BEGIN
l_user_psw :=
DBMS_CRYPTO.encrypt (UTL_I18N.string_to_raw (l_user_psw, 'AR8MSWIN1256'),
l_mod,
UTL_I18N.string_to_raw (l_key, 'AR8MSWIN1256'));
DBMS_OUTPUT.put_line ('Encrypted=' || l_user_psw);
INSERT INTO test VALUES (l_user_id, l_user_psw);
dbms_output.put_line('done');
COMMIT;
END;
/
and the final result is
132BEDB1C2CDD8F23B5A619412C27B60
now i want to make identical Aes in c# i know i can call stored procedure from c# and get the same result but i want to make it using c# for security reasons i have tried many ways but ended up with different result ! i need help please !
Since I'm feeling generous, I'll provide a solution for you:
public static string EncryptPassword(string key, string password)
{
// Your current code uses WIN1256 encoding for converting
// your strings to bytes, so we'll use that here
var encoding = System.Text.Encoding.GetEncoding(1256);
byte[] passwordBytes = encoding.GetBytes(password);
byte[] keyBytes = encoding.GetBytes(key);
using (var aes = AesManaged.Create())
{
// Set up the algorithm
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
aes.Key = keyBytes;
aes.BlockSize = 128; // AES-128
// You don't specify an IV in your procedure, so we
// need to zero it
aes.IV = new byte[16];
// Create a memorystream to store the result
using (var ms = new MemoryStream())
{
// create an encryptor transform, and wrap the memorystream in a cryptostream
using (var transform = aes.CreateEncryptor())
using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
{
// write the password bytes
cs.Write(passwordBytes, 0, passwordBytes.Length);
}
// get the encrypted bytes and format it as a hex string and then return it
return BitConverter.ToString(ms.ToArray()).Replace("-", string.Empty);
}
}
}