Bonjour,
je cherche à faire communiquer 2 serveurs (un .net et un PHP)de manière "sécurisé"
Je me suis donc orienté vers l'utilisation du système de clé public/clé privé (ici via RSA)
mon objectif:
utiliser la clé public du serveur php pour encrypter un message (en c#) ,
utiliser la clé privé du serveur php pour décodé le message reçu (en PHP)
J'ai pas mal galéré car mes clés au format pem, j'ai ajouter comme référence bouncycastle http://www.bouncycastle.org/csharp/
Voici les extraits de mon code:
c#
php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 protected void Button1_Click(object sender, EventArgs e) { string inputMessage = @"Texte secret Provenant de C#"; UTF8Encoding utf8enc = new UTF8Encoding(); byte[] inputBytes = utf8enc.GetBytes(inputMessage); var fileStream = File.OpenText("C:\\Users\\utilisateurdupc\\Desktop\\public1024.key"); var pemReader = new PemReader(fileStream); var KeyParameter = (AsymmetricKeyParameter)pemReader.ReadObject(); AsymmetricKeyParameter publicKey = KeyParameter; // Creating the RSA algorithm object IAsymmetricBlockCipher cipher = new RsaEngine(); // Initializing the RSA object for Encryption with RSA public key. Remember, for encryption, public key is needed cipher.Init(true, publicKey); //Encrypting the input bytes byte[] cipheredBytes = cipher.ProcessBlock(inputBytes, 0, inputMessage.Length); Debug.WriteLine(cipheredBytes); Debug.WriteLine(Convert.ToBase64String(cipheredBytes)); }
De mon code C# je récupère la Chaine Convert.ToBase64String(cipheredBytes) : iVxSTQF897bnlWlb6rA8k1ZRd7FmRcUklQTyGyHwL1fuaXzpR1QG7a450Y+yEV5ik8hlymRkxpREAiDCwYUDgULF985dHFaEpoa/RmnGhDVm8WOxvB+yuIupTrAa/rgEjYS74PlNtJ44x2zjweqLxo1hLBXZL3kxbQcfNT53g1I=
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 <? $privateKey = openssl_pkey_get_private(file_get_contents("./priv1024.key")); $publicKey = openssl_pkey_get_public(file_get_contents("./public1024.key")); $message= "iVxSTQF897bnlWlb6rA8k1ZRd7FmRcUklQTyGyHwL1fuaXzpR1QG7a450Y+yEV5ik8hlymRkxpREAiDCwYUDgULF985dHFaEpoa/RmnGhDVm8WOxvB+yuIupTrAa/rgEjYS74PlNtJ44x2zjweqLxo1hLBXZL3kxbQcfNT53g1I="; $encryptedData=base64_decode($message); $erreur=openssl_private_decrypt($encryptedData, $plainTextData, $privateKey); var_dump($erreur,$encryptedData, $plainTextData, $privateKey); ?>
Mon problème:
Mon soucis est que lorsque j'utilise la chaine produite par le C# Le code php je ne parviens pas à décrypter correctement mon message
J'imagine faire une erreur (coté C#) mais je ne trouve pas l'endroit
D'avance merci pour votre aide
note: j'ai réalisé un projet spécifique pour tester cette fonctionnalité (D’où l'aspect brut, sans liaison, sans tenant ni aboutissant)
note2:message aux modos pas entierment sur d'avoir posté au bon endroit au besoin n'hésitez pas à déplacer
Partager