Die Verwendung eines Öffentlichen RSA-Schlüssel entschlüsselt einen string, der verschlüsselt wurde, mit Privaten RSA-Schlüssel

Ich weiß, sind die Antwort, die ich bin wahrscheinlich zu bekommen, ist, warum zum Teufel würden Sie tun wollen?!

Leider, trotz meiner Proteste, ich muss es tun, obwohl ich weiß, dass es wenig Sinn macht.

Habe ich Funktionen geschrieben .Net zu entschlüsseln, die mit einem privaten Schlüssel, verschlüsselt mit einem öffentlichen Schlüssel.
Ich habe auch RSA signieren und überprüfen und ein angemessenes Verständnis von, wie das alles funktionieren, denke ich.

Ich bin jetzt gesendet wird, einen Wert, der RSA-Verschlüsselung mit einem privaten Schlüssel, die ich sollte daraus einen nutzbaren Wert durch Entschlüsselung mit dem öffentlichen Schlüssel.

Ich kann nicht scheinen, um herauszufinden, wie dies zu tun. Bin ich ein idiot? Ist dies eine normale Sache zu tun?

Erzählte mir von der person, senden Sie mir das Wert, dies ist kein problem in PHP. Ich weiß es nicht und noch nicht verwendet PHP noch. Ich kann nicht finden, eine Bibliothek, es zu tun in einer der wichtigsten Sprachen, die ich kenne, D. H. C++, Java, C#. Der server dem ich arbeite, verwendet .Net.

Ich bin der Hoffnung, jemand könnte in der Lage sein, mir helfen.

Wäre es toll, wenn es irgendeine Art von vernünftigen Lösung außerdem betteln Sie zu ändern, was Sie tun.

Dies ist meine Methode (aktualisiert von meinen bisherigen schlechten wie bereits von Iridium), aber wenn ich versuche zu entschlüsseln, die den Wert bekomme ich auch eine exception

"Fehler beim Dekodieren OAEP-padding."

Wenn ich die rsa verwenden.Decrypt(Byte, false) bekomme ich eine bad-Taste Ausnahme.

public static string DecryptUsingPublic(string dataEncrypted, string publicKey)
    {
        if (dataEncrypted == null) throw new ArgumentNullException("dataEncrypted");
        if (publicKey == null) throw new ArgumentNullException("publicKey");
        try
        {
            RSAParameters _publicKey = LoadRsaPublicKey(publicKey, false);
            RSACryptoServiceProvider rsa = InitRSAProvider(_publicKey);

            byte[] bytes = Convert.FromBase64String(dataEncrypted);
            byte[] decryptedBytes = rsa.Decrypt(bytes, true);

            ArrayList arrayList = new ArrayList();
            arrayList.AddRange(decryptedBytes);

           return Encoding.UTF8.GetString(decryptedBytes);
        }
        catch
        {
            return null;
        }
    }

    private static RSAParameters LoadRsaPublicKey(String publicKeyFilePath, Boolean isFile)
    {
        RSAParameters RSAKeyInfo = new RSAParameters();
        byte[] pubkey = ReadFileKey(publicKeyFilePath, "PUBLIC KEY", isFile);
        byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };
        byte[] seq = new byte[15];
        //---------  Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob  ------
        MemoryStream mem = new MemoryStream(pubkey);
        BinaryReader binr = new BinaryReader(mem);    //wrap Memory Stream with BinaryReader for easy reading
        byte bt = 0;
        ushort twobytes = 0;

        try
        {

            twobytes = binr.ReadUInt16();
            if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
                binr.ReadByte();    //advance 1 byte
            else if (twobytes == 0x8230)
                binr.ReadInt16();   //advance 2 bytes
            else
                return RSAKeyInfo;

            seq = binr.ReadBytes(15);       //read the Sequence OID
            if (!CompareBytearrays(seq, SeqOID))    //make sure Sequence for OID is correct
                return RSAKeyInfo;

            twobytes = binr.ReadUInt16();
            if (twobytes == 0x8103) //data read as little endian order (actual data order for Bit String is 03 81)
                binr.ReadByte();    //advance 1 byte
            else if (twobytes == 0x8203)
                binr.ReadInt16();   //advance 2 bytes
            else
                return RSAKeyInfo;

            bt = binr.ReadByte();
            if (bt != 0x00)     //expect null byte next
                return RSAKeyInfo;

            twobytes = binr.ReadUInt16();
            if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)
                binr.ReadByte();    //advance 1 byte
            else if (twobytes == 0x8230)
                binr.ReadInt16();   //advance 2 bytes
            else
                return RSAKeyInfo;

            twobytes = binr.ReadUInt16();
            byte lowbyte = 0x00;
            byte highbyte = 0x00;

            if (twobytes == 0x8102) //data read as little endian order (actual data order for Integer is 02 81)
                lowbyte = binr.ReadByte();  //read next bytes which is bytes in modulus
            else if (twobytes == 0x8202)
            {
                highbyte = binr.ReadByte(); //advance 2 bytes
                lowbyte = binr.ReadByte();
            }
            else
                return RSAKeyInfo;
            byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };   //reverse byte order since asn.1 key uses big endian order
            int modsize = BitConverter.ToInt32(modint, 0);

            byte firstbyte = binr.ReadByte();
            binr.BaseStream.Seek(-1, SeekOrigin.Current);

            if (firstbyte == 0x00)
            {   //if first byte (highest order) of modulus is zero, don't include it
                binr.ReadByte();    //skip this null byte
                modsize -= 1;   //reduce modulus buffer size by 1
            }

            byte[] modulus = binr.ReadBytes(modsize);   //read the modulus bytes

            if (binr.ReadByte() != 0x02)            //expect an Integer for the exponent data
                return RSAKeyInfo;
            int expbytes = (int)binr.ReadByte();        //should only need one byte for actual exponent data (for all useful values)
            byte[] exponent = binr.ReadBytes(expbytes);


            RSAKeyInfo.Modulus = modulus;
            RSAKeyInfo.Exponent = exponent;

            return RSAKeyInfo;
        }
        catch (Exception)
        {
            return RSAKeyInfo;
        }

        finally { binr.Close(); }
        //return RSAparams;

    }

 private static RSACryptoServiceProvider InitRSAProvider(RSAParameters rsaParam)
    {
        //
        //Initailize the CSP
        //  Supresses creation of a new key
        //
        CspParameters csp = new CspParameters();
        //csp.KeyContainerName = "RSA Test (OK to Delete)";

        const int PROV_RSA_FULL = 1;
        csp.ProviderType = PROV_RSA_FULL;

        const int AT_KEYEXCHANGE = 1;
        //const int AT_SIGNATURE = 2;
        csp.KeyNumber = AT_KEYEXCHANGE;
        //
        //Initialize the Provider
        //
        RSACryptoServiceProvider rsa =
          new RSACryptoServiceProvider(csp);
        rsa.PersistKeyInCsp = false;

        //
        //The moment of truth...
        //
        rsa.ImportParameters(rsaParam);
        return rsa;
    }

    private static int GetIntegerSize(BinaryReader binr)
    {
        byte bt = 0;
        byte lowbyte = 0x00;
        byte highbyte = 0x00;
        int count = 0;
        bt = binr.ReadByte();
        if (bt != 0x02)     //expect integer
            return 0;
        bt = binr.ReadByte();

        if (bt == 0x81)
            count = binr.ReadByte();    //data size in next byte
        else
            if (bt == 0x82)
            {
                highbyte = binr.ReadByte(); //data size in next 2 bytes
                lowbyte = binr.ReadByte();
                byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
                count = BitConverter.ToInt32(modint, 0);
            }
            else
            {
                count = bt;     //we already have the data size
            }

        while (binr.ReadByte() == 0x00)
        {   //remove high order zeros in data
            count -= 1;
        }
        binr.BaseStream.Seek(-1, SeekOrigin.Current);       //last ReadByte wasn't a removed zero, so back up a byte
        return count;
    }

    private static bool CompareBytearrays(byte[] a, byte[] b)
    {
        if (a.Length != b.Length)
            return false;
        int i = 0;
        foreach (byte c in a)
        {
            if (c != b[i])
                return false;
            i++;
        }
        return true;
    }

Den beiden oben genannten Methoden InitRSAProvider und LoadRsaPublicKey wurden, bekommen Tutorien zu ermöglichen PEM-Schlüssel als Strings verwendet werden .Net.

Sagen Sie, was? Das ist genau das, was der Algorithmus machen soll. Entschlüsselung mit einem privaten Schlüssel, was verschlüsselt wurde, mit dem entsprechenden öffentlichen Schlüssel. Entweder Sie sind verwirrt, big time oder ich bin!!! C# unterstützt perfekt RSA 🙂
Sorry. Bearbeitet meine Frage. Ich schrieb es völlig falsch. Gehirn beginnt zu klettern, ein bisschen an den Freitagen.
Was kannst du nicht herausfinden? Sie sagen, Sie haben bereits Funktionen zum entschlüsseln von über einen öffentlichen Schlüssel.
Okay, tut mir Leid. Sollte ich gelesen haben meine Frage richtig. Total Durcheinander. Ich habe die standard-entschlüsseln mit dem Privaten Schlüssel nicht den öffentlichen. Wenn ich versuche zu entschlüsseln, die den Wert verschlüsselt mit dem privaten Schlüssel die mit dem öffentlichen Schlüssel bekomme ich auch eine exception - Die Daten entschlüsselt werden, überschreitet die maximal für dieses Modul von 128 bytes. Ich habe seit Jahren einen versuchen, viele Dinge, aber ich bekomme immer Ausnahmen.
Was tun LoadRsaPublicKey und InitRSAProvider tun? Können Sie den code für diese Methoden auch?

InformationsquelleAutor Ruairi O'Brien | 2011-10-21

Schreibe einen Kommentar