Question

Je suis en train d'essayer de mettre en œuvre une classe pour gérer les communications sécurisées entre les instances de mon application à l'aide de la classe RSACrytoServiceProveider. Première question: est-ce une bonne idée de mettre en œuvre une seule classe pour gérer les rôles émetteur / récepteur ou devrais-je diviser les rôles en classes individuelles?. C'est ce que je l'ai fait jusqu'à présent:

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

namespace Agnus.Cipher
{
    public class RSA
    {
        private byte[] plaintextBytes;
        private byte[] ciphertextBytes;
        private RSACryptoServiceProvider rSAProviderThis;
        private RSACryptoServiceProvider rSAProviderOther;

        public string PublicKey
        {
            get { return rSAProviderThis.ToXmlString(false); }
        }

        public RSA()
        {
            rSAProviderThis = new RSACryptoServiceProvider { PersistKeyInCsp = true }; 
            plaintextBytes = Encoding.Unicode.GetBytes(PublicKey);
        }

        public void InitializeRSAProviderOther(string parameters)
        {
            rSAProviderOther.FromXmlString(parameters);
        }

        public byte[] Encrypt()
        {
            return rSAProviderThis.Encrypt(plaintextBytes, true);
        }
        public byte[] Decrypt()
        {
            return rSAProviderThis.Decrypt(ciphertextBytes, true);
        }
        public byte[] Sign()
        {
            using (SHA1Managed SHA1 = new SHA1Managed())
            {
                byte[] hash = SHA1.ComputeHash(ciphertextBytes);
                byte[] signature = rSAProviderThis.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));
                return signature;
            }
        }
        public void Verify()
        {
            throw new NotImplementedException();
        }

    }
}

Deuxième question: comment puis-je envoyer et recevoir des données à introduire dans la classe? Je suis une corne verte dans ce domaine, les pointeurs seraient appréciés.

Était-ce utile?

La solution

Je ferais Chiffrer / signe / déchiffrer / vérifier les méthodes prennent des paramètres pour les données plutôt que d'avoir des variables membres pour eux. Avoir des variables membres pour la clé et fournisseur semble bien que. Fondamentalement, je pense à utiliser la même clé plusieurs fois, mais pas les mêmes données.

Je voudrais aussi fais immuable -. Faire toutes les variables en lecture seule, en prenant tous les paramètres dont vous aurez besoin pour les fournisseurs dans le constructeur au lieu d'avoir une méthode d'initialisation séparée

Au-delà de cela, il semble bien envelopper la fonctionnalité dans une API plus simple pour vos besoins bien, oui.

Autres conseils

Je l'ai fait quelques ajustements, voici ce qui semble la mise en œuvre comme:

using System;
using System.Security.Cryptography;

namespace Agnus.Cipher
{
    public class RSA : IDisposable
    {
        private RSACryptoServiceProvider rSAProviderThis;
        private RSACryptoServiceProvider rSAProviderOther = null;

        public string PublicKey
        {
            get { return rSAProviderThis.ToXmlString(false); }
        }

        public RSA()
        {
            rSAProviderThis = new RSACryptoServiceProvider { PersistKeyInCsp = true }; 
        }

        public void InitializeRSAProviderOther(string parameters)
        {
            rSAProviderOther.FromXmlString(parameters);
        }

        public byte[] Encrypt(byte[] plaintextBytes)
        {
                return rSAProviderThis.Encrypt(plaintextBytes, true);
        }
        public string  Decrypt(byte[] ciphertextBytes)
        {
            try
            {
                return Convert.ToBase64String( rSAProviderThis.Decrypt(ciphertextBytes, true));
            }
            catch (CryptographicException ex)
            {
                Console.WriteLine("Unable to decrypt: " + ex.Message + " " + ex.StackTrace);
            }
            finally
            {
                this.Dispose();
            }
            return string.Empty;
        }
        public string SignData(byte[] ciphertextBytes)
        {
            string  signature = GenerateSignature(ciphertextBytes, rSAProviderThis);
            return signature;
        }

        private string GenerateSignature(byte[] ciphertextBytes, RSACryptoServiceProvider provider)
        {
            using (SHA1Managed SHA1 = new SHA1Managed())
            {
                byte[] hash = SHA1.ComputeHash(ciphertextBytes);
                string signature = Convert.ToBase64String(provider.SignHash(hash, CryptoConfig.MapNameToOID("SHA1")));
                return signature;
            }

        }

        public string  VerifySignature(byte[] ciphertextBytes, string parameters, string signatureToVerify)
        {
            InitializeRSAProviderOther(parameters);
            string actualSignature = GenerateSignature(ciphertextBytes, rSAProviderOther);
            if (actualSignature.Equals(signatureToVerify))
            {
                //verification successful
                string decryptedData = this.Decrypt(ciphertextBytes);
                return decryptedData;
                //decryptedData is a symmetric key
            }
            else
            {
                //verification unsuccessful
                //end session
            }
            return string.Empty;
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (rSAProviderOther != null)
            {
                rSAProviderOther.Clear();
            }
            rSAProviderThis.Clear();
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}

vous les gars ont toujours pas dit quoi que ce soit sur la façon dont la communication va être mis en place (je pense prises). S'il vous plaît me éclairer.

Je ne sais pas si ce snip de code peut vous aider, je l'ai écrit ce code pour pouvoir crypter et décrypter des paires de clés privées / publiques dans divers algortims de cryptant et sans données pour chiffrer problème de longueur, enfait RSA la mise en œuvre en .NET souffrent whe vous essayez de gérer plus de 250 (plus ou moins, désolé, je ne me souviens pas) octets de données.

Je viens de couper et coller uniquement les méthodes nécessaires, je aussi CUTTED cause la documentation xml est pas en anglais, si vous avez trouvé cela utile laissez-moi savoir, je peux poster toutes les sources. Je le répète, je ne suis pas testé cette version coupé et coller, mais je la version complète de cette classe qui ne sont pas si différents.

BTW: il est en VB, mais si vous avez juste besoin de se cacher à elle, je pense qu'il est assez;)

Namespace Crypto

    Public Class RSACry

        Shared Sub New()
        End Sub

        Public Enum Algorithms
            DES
            TDES
            RC2
            RDAEL
        End Enum

        Public Shared Function Encrypt(ByVal xmlkeystring As String, ByVal typo As Algorithms, ByVal datatoencrypt As String) As String
            Dim rsaer As RSA = Crypto.RSACry.ReadKeyString(xmlkeystring)
            Dim result() As Byte = Crypto.RSACry.EncryptIt(rsaer, typo, datatoencrypt)
            Return System.Convert.ToBase64String(result)
        End Function

        Public Shared Function Decrypt(ByVal xmlkeystring As String, ByVal typo As Algorithms, ByVal datatodecrypt As String) As String
            Dim rsaer As RSA = Crypto.RSACry.ReadKeyString(xmlkeystring)
            Dim result() As Byte = Crypto.RSACry.DecryptIt(rsaer, typo, datatodecrypt)
            Return System.Text.Encoding.UTF8.GetString(result)
        End Function

        Friend Shared Function EncryptIt(ByRef rsaer As RSA, ByVal typo As Algorithms, ByVal datatoencrypt As String) As Byte()
            Dim result() As Byte = Nothing

            Try
                Dim plainbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(datatoencrypt)
                Dim sa As SymmetricAlgorithm = SymmetricAlgorithm.Create(Crypto.RSACry.GetAlgorithmName(typo))
                Dim ct As ICryptoTransform = sa.CreateEncryptor()
                Dim encrypt() As Byte = ct.TransformFinalBlock(plainbytes, 0, plainbytes.Length)
                Dim fmt As RSAPKCS1KeyExchangeFormatter = New RSAPKCS1KeyExchangeFormatter(rsaer)
                Dim keyex() As Byte = fmt.CreateKeyExchange(sa.Key)

                --return the key exchange, the IV (public) and encrypted data 
                result = New Byte(keyex.Length + sa.IV.Length + encrypt.Length) {}
                Buffer.BlockCopy(keyex, 0, result, 0, keyex.Length)
                Buffer.BlockCopy(sa.IV, 0, result, keyex.Length, sa.IV.Length)
                Buffer.BlockCopy(encrypt, 0, result, keyex.Length + sa.IV.Length, encrypt.Length)

            Catch ex As Exception
                Throw New CryptographicException("Unable to crypt: " + ex.Message)
            End Try

            Return result
        End Function

        Friend Shared Function DecryptIt(ByRef rsaer As RSA, ByVal typo As Algorithms, ByVal datatodecrypt As String) As Byte()
            Dim result() As Byte = Nothing

            Try
                Dim encrbytes() As Byte = System.Convert.FromBase64String(datatodecrypt)
                Dim sa As SymmetricAlgorithm = SymmetricAlgorithm.Create(Crypto.RSACry.GetAlgorithmName(typo))
                Dim keyex() As Byte = New Byte((rsaer.KeySize >> 3) - 1) {}
                Buffer.BlockCopy(encrbytes, 0, keyex, 0, keyex.Length)

                Dim def As RSAPKCS1KeyExchangeDeformatter = New RSAPKCS1KeyExchangeDeformatter(rsaer)
                Dim key() As Byte = def.DecryptKeyExchange(keyex)
                Dim iv() As Byte = New Byte((sa.IV.Length - 1)) {}
                Buffer.BlockCopy(encrbytes, keyex.Length, iv, 0, iv.Length)

                Dim ct As ICryptoTransform = sa.CreateDecryptor(key, iv)
                result = ct.TransformFinalBlock(encrbytes, keyex.Length + iv.Length, (encrbytes.Length - 1) - (keyex.Length + iv.Length))
            Catch ex As Exception
                Throw New CryptographicException("Unable to decrypt: " + ex.Message)
            End Try

            Return result
        End Function    

        Friend Shared Function GetAlgorithmName(ByVal typo As Algorithms) As String
            Dim algtype As String = String.Empty

            Select Case typo
                Case Algorithms.DES
                    Return "DES"
                    Exit Select
                Case Algorithms.RC2
                    Return "RC2"
                    Exit Select
                Case Algorithms.RDAEL
                    Return "Rijndael"
                    Exit Select
                Case Algorithms.TDES
                    Return "TripleDES"
                    Exit Select
                Case Else
                    Return "Rijndael"
                    Exit Select
            End Select

            Return algtype
        End Function

        Friend Shared Function ReadKeyString(ByVal xmlkeystring As String) As RSA
            Dim rsaer As RSA = Nothing

            Try
                If (String.IsNullOrEmpty(xmlkeystring)) Then Throw New Exception("Key is not specified")
                rsaer = RSA.Create()
                rsaer.FromXmlString(xmlkeystring)
            Catch ex As Exception
                Throw New CryptographicException("Unable to load key")
            End Try

            Return rsaer
        End Function    

End Namespace

@gogole: en fait je ne pas utiliser les sockets, ce code est utilisé de manière autonome avec les touches main bringed. Cependant, une fois que vous avez votre mécanisme de chiffrement, vous êtes à plus de la moitié du voyage.

Ici, il y a deux méthodes manquantes pour créer des clés, comme vous avez demandé le code est complet. J'espère que vous avez trouvé cela utile

Public Shared Sub CreateKeyPair(ByVal filename As String)
    Dim xmlpublic As String = String.Empty
    Dim xmlprivate As String = String.Empty

    CreateKeyPair(xmlpublic, xmlprivate)

    Try
        Dim writer As New StreamWriter(filename + ".prv")
        writer.Write(xmlprivate)
        writer.Flush()
        writer.Close()
    Catch ex As Exception
        Throw New CryptographicException("Unable to write private key file: " + ex.Message)
    End Try

    Try
        Dim writer = New StreamWriter(filename + ".pub")
        writer.Write(xmlpublic)
        writer.Flush()
        writer.Close()
    Catch ex As Exception
        Throw New CryptographicException("Unable to write public key file: " + ex.Message)
    End Try
End Sub

Public Shared Sub CreateKeyPair(ByRef xmlpublic As String, ByRef xmlprivate As String)
    Dim rsa As RSA = Nothing

    Try
        rsa.Create()
    Catch ex As Exception
        Throw New CryptographicException("Unable to initialize keys: " + ex.Message)
    End Try

    Try
        xmlpublic = rsa.ToXmlString(True)
    Catch ex As Exception
        Throw New CryptographicException("Unable to generate public key: " + ex.Message)
    End Try

    Try
        xmlprivate = rsa.ToXmlString(False)
    Catch ex As Exception
        Throw New CryptographicException("Unable to generate private key: " + ex.Message)
    End Try
End Sub

Ne sait pas beaucoup vb mais essayé de convertir un @Andrea Celin du Code utile dans c #

namespace Crypto
{
using System;
using System.Security.Cryptography;
class RSACry
{
    public enum Algorithms
    {
        DES,
        TDES,
        RC2,
        RDAEL
    };
    public string Encrypt(string xmlkeystring, Algorithms typo, string datatoencrypt)
    {
        RSA rsaer = RSACry.ReadKeyString(xmlkeystring);
        byte[] result = RSACry.EncryptIt(rsaer, typo, datatoencrypt);
        return System.Convert.ToBase64String(result);
    }

    public string Decrypt(string xmlkeystring,Algorithms typo,string datatodecrypt)
    {
        RSA rsaer = RSACry.ReadKeyString(xmlkeystring);
        byte[] result =RSACry.DecryptIt(rsaer, typo, datatodecrypt);
        return System.Text.Encoding.UTF8.GetString(result);
    }

    public static byte[] EncryptIt(RSA rsaer, Algorithms typo, string datatoencrypt)
    {
        byte[] result = null;
        try
        {
            byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes(datatoencrypt);
            SymmetricAlgorithm sa = SymmetricAlgorithm.Create(RSACry.GetAlgorithmName(typo));
            ICryptoTransform ct = sa.CreateEncryptor();
            byte[] encrypt = ct.TransformFinalBlock(plainbytes, 0, plainbytes.Length);
            RSAPKCS1KeyExchangeFormatter fmt = new RSAPKCS1KeyExchangeFormatter(rsaer);
            byte[] keyex = fmt.CreateKeyExchange(sa.Key);

            //--return the key exchange, the IV (public) and encrypted data 
            result = new byte[keyex.Length + sa.IV.Length + encrypt.Length];
            Buffer.BlockCopy(keyex, 0, result, 0, keyex.Length);
            Buffer.BlockCopy(sa.IV, 0, result, keyex.Length, sa.IV.Length);
            Buffer.BlockCopy(encrypt, 0, result, keyex.Length + sa.IV.Length, encrypt.Length);
        }
        catch (Exception ex)
        {
            throw new CryptographicException("Unable to crypt: " + ex.Message);
        }
        return result;
    }

    public static byte[] DecryptIt(RSA rsaer, Algorithms typo, string datatodecrypt)
    {
        byte[] result = null;

        try
        {
            byte[] encrbytes = System.Convert.FromBase64String(datatodecrypt);
            SymmetricAlgorithm sa = SymmetricAlgorithm.Create(RSACry.GetAlgorithmName(typo));
            byte[] keyex = new byte[(rsaer.KeySize >> 3) - 1];
            Buffer.BlockCopy(encrbytes, 0, keyex, 0, keyex.Length);

            RSAPKCS1KeyExchangeDeformatter def = new RSAPKCS1KeyExchangeDeformatter(rsaer);
            byte[] key = def.DecryptKeyExchange(keyex);
            byte[] iv = new byte[sa.IV.Length - 1];
            Buffer.BlockCopy(encrbytes, keyex.Length, iv, 0, iv.Length);

            ICryptoTransform ct = sa.CreateDecryptor(key, iv);
            result = ct.TransformFinalBlock(encrbytes, keyex.Length + iv.Length, (encrbytes.Length - 1) - (keyex.Length + iv.Length));
        }
        catch (Exception ex)
        {
            throw new CryptographicException("Unable to decrypt: " + ex.Message);
        }

        return result;
    }

    public static string GetAlgorithmName(Algorithms typo)
    {
        string algtype = String.Empty;
        switch(typo)
        {
            case Algorithms.DES:
                algtype = "DES";
                break;
            case Algorithms.RC2:
                algtype = "RC2";
                break;
            case Algorithms.RDAEL:
                algtype = "Rijndael";
                break;
            case Algorithms.TDES:
                algtype = "TripleDES";
                break;
            default:
                algtype = "Rijndael";
                break;
        }
        return algtype;
    }

    public static RSA ReadKeyString(string xmlkeystring)
    {
        RSA rsaer = null;
        try
        {
            if (String.IsNullOrEmpty(xmlkeystring))
            { throw new Exception("Key is not specified"); }
            rsaer = RSA.Create();
            rsaer.FromXmlString(xmlkeystring);
        }
        catch (Exception ex)
        {
            throw new CryptographicException("Unable to load key :"+ex.Message);
        }
        return rsaer;
    }
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top