You need to sign in to do that
Don't have an account?
Santosh Uppala
convert c# code to apex
Hi all,
I am facing issues while converting C# code to apex. Below is the C# code:
using System.IO;
using System.Security.Cryptography;
using System.Text;
{
//REST API keys for encryption
const string key = "a678-78js-986s-iujk-8769";
const string IV = "ohjk-0987-6h7j-12sg-678j";
//PlainTest of credentials along with DT in UTC for auth
string plainText = username + Environment.NewLine +
password + Environment.NewLine +
DateTime.UtcNow.Ticks;
//Check the input
if (plainText == null || plainText.Length == 0)
throw new ArgumentNullException("plaintext", "plaintext cannot be blank.");
if (key == null || key.Length == 0)
throw new ArgumentNullException("key", "key cannot be blank.");
//Get the input as bytes
byte[] buffPlain = Encoding.UTF8.GetBytes(plainText);
//Create and setup the crypto objects
RijndaelManaged crypt = new RijndaelManaged();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
crypt.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
crypt.IV = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(IV));
crypt.Mode = CipherMode.CBC;
//Create the memory streams
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, crypt.CreateEncryptor(), CryptoStreamMode.Write);
//Write the data through the crypto stream into the memory stream
try
{
cs.Write(buffPlain, 0, buffPlain.Length);
cs.FlushFinalBlock();
}
finally
{
//Close the streams
cs.Close();
ms.Close();
//Clear the crypto objects;
hashMD5.Clear();
crypt.Clear();
}
//Get the encrypted bytes
byte[] buffEnc = ms.ToArray();
StringBuilder ret = new StringBuilder();
//Convert the bytes to hex with a length of 2
foreach (byte b in buffEnc)
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
I am facing issues while converting C# code to apex. Below is the C# code:
using System.IO;
using System.Security.Cryptography;
using System.Text;
{
//REST API keys for encryption
const string key = "a678-78js-986s-iujk-8769";
const string IV = "ohjk-0987-6h7j-12sg-678j";
//PlainTest of credentials along with DT in UTC for auth
string plainText = username + Environment.NewLine +
password + Environment.NewLine +
DateTime.UtcNow.Ticks;
//Check the input
if (plainText == null || plainText.Length == 0)
throw new ArgumentNullException("plaintext", "plaintext cannot be blank.");
if (key == null || key.Length == 0)
throw new ArgumentNullException("key", "key cannot be blank.");
//Get the input as bytes
byte[] buffPlain = Encoding.UTF8.GetBytes(plainText);
//Create and setup the crypto objects
RijndaelManaged crypt = new RijndaelManaged();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
crypt.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
crypt.IV = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(IV));
crypt.Mode = CipherMode.CBC;
//Create the memory streams
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, crypt.CreateEncryptor(), CryptoStreamMode.Write);
//Write the data through the crypto stream into the memory stream
try
{
cs.Write(buffPlain, 0, buffPlain.Length);
cs.FlushFinalBlock();
}
finally
{
//Close the streams
cs.Close();
ms.Close();
//Clear the crypto objects;
hashMD5.Clear();
crypt.Clear();
}
//Get the encrypted bytes
byte[] buffEnc = ms.ToArray();
StringBuilder ret = new StringBuilder();
//Convert the bytes to hex with a length of 2
foreach (byte b in buffEnc)
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
https://github.com/yallie/ApexSharp