3
Hello, I have a DLL to encrypt data. I need to use the Component Object Model(COM) Interop to be able to communicate two applications and both traffic the encrypted data through this DLL. Searching, I found examples of integrating with Microsoft Office, but not with another application.
As requested, follow the example I found on the Microsoft website( e em cursos do Devmedia)
using System.Collections.Generic;
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
class Account
{
public int ID { get; set; }
public double Balance { get; set; }
}
var bankAccounts = new List<Account>
{
new Account
{
ID = 345,
Balance = 541.27
},
new Account
{
ID = 123,
Balance = -127.44
}
};
void DisplayInExcel(IEnumerable<Account> accounts,
Action<Account, Excel.Range> DisplayFunc)
{
var excelApp = this.Application;
// Add a new Excel workbook.
excelApp.Workbooks.Add();
excelApp.Visible = true;
excelApp.Range["A1"].Value = "ID";
excelApp.Range["B1"].Value = "Balance";
excelApp.Range["A2"].Select();
foreach (var ac in accounts)
{
DisplayFunc(ac, excelApp.ActiveCell);
excelApp.ActiveCell.Offset[1, 0].Select();
}
// Copy the results to the Clipboard.
excelApp.Range["A1:B3"].Copy();
excelApp.Columns[1].AutoFit();
excelApp.Columns[2].AutoFit();
}
I was able to develop this code, based on research. But I don’t know if it fits this concept. Could you help me with this?
Classe DLL
using System;
using System.Security.Cryptography;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace PortalRH.DLL
{
[ComVisible(true), ClassInterface(ClassInterfaceType.None),
Guid("00AC4F7E-71B0-4BC7-AD8E-1175CD88457A")]
public class Criptografia : ICriptografia
{
private string chave = "Exemplo";
private string texto;
public Criptografia(){}
private static readonly byte[] initVectorBytes = Encoding.ASCII.GetBytes("tu89geji340t89u2");
// Esta constante é utilizado para determinar o tamanho da chave do algoritmo de encriptação.
private const int keysize = 256;
public static string Encrypt(string plainText, string passPhrase)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
{
byte[] keyBytes = password.GetBytes(keysize / 8);
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
}
public static string Decrypt(string cipherText, string passPhrase)
{
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
{
byte[] keyBytes = password.GetBytes(keysize / 8);
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
}
}
}
public string Matricula()
{
const string matricula = "5444";
string plaintext = Encrypt(matricula, chave);
return (plaintext);
}
public string Contrato()
{
const string contrato = "67334";
string plaintext = Encrypt(contrato, chave);
return (plaintext);
}
}
}
My interface
using System.Runtime.InteropServices;
namespace PortalRH.DLL
{
[InterfaceType(ComInterfaceType.InterfaceIsDual),
Guid("37DFD392-8BC3-4A08-B69A-5FDF1B6DF4B1")]
public interface ICriptografia
{
string Matricula();
string Contrato();
}
}
You can put these examples in your question?
– Leonel Sanches da Silva