Static Method in Interface

Asked

Viewed 190 times

1

I have a class to encrypt data. But it will be used as a DLL, and for that I need to create an interface to show the methods( tested without the interface and it didn’t work). However, it contains 2 static methods, and I’m getting error for it. Is there a way to make this interface or convert my class to no longer use Static?

Follows my code:

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 
{
private string chave = "chave";
public Criptografia(){}


// Essa seqüência constante é usada como um valor "salt" para as chamadas de função PasswordDeriveBytes .
// Este tamanho da IV (em bytes) devem = (KeySize / 8). KeySize padrão é 256, portanto, a IV deve ser
// 32 bytes de comprimento. Usando uma seqüência de 16 caracteres aqui nos dá 32 bytes quando convertido para um array de bytes.
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);
                    }
                }
            }
        }
    }
}

}

Interface:

using System.Runtime.InteropServices;

namespace PortalRH.DLL
{
[Guid("16E2B6C1-1CC2-4B71-BE4E-9F6DF103AA3E")]
public interface ICriptografia
{


    string Encrypt(string str);
    string Decrypt(string str);

}

}

Follow the error returned:

> An object reference is required for the non-static field, method, or property 
> 'PortalRH.DLL.Criptografia.Decrypt(string, string)'   
  • Post the returned error.

  • @Jota Edited with error returned

  • Where is your interface?

  • I’ve added the interface now

1 answer

3


COM does not support static methods, this is a rule and there is nothing to do.

You will have to modify your class so that all methods are members of the instance, or at least include nonstatic methods that call static methods, and then these nonstatic methods should be included in your interface.

  • I could explain a little more, how I do it?

  • @Renilsonandrade Explained: http://answall.com/a/47450/14584

  • Sorry, I hadn’t even looked. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.