AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Algorithmen, Datenstrukturen und Klassendesign Delphi Verschlüsseln in PHP und entschlüsseln in Delphi?
Thema durchsuchen
Ansicht
Themen-Optionen

Verschlüsseln in PHP und entschlüsseln in Delphi?

Ein Thema von gpl · begonnen am 6. Mär 2024 · letzter Beitrag vom 8. Mär 2024
Antwort Antwort
Seite 1 von 2  1 2      
gpl

Registriert seit: 28. Jan 2010
Ort: Nähe Bonn
12 Beiträge
 
Delphi 11 Alexandria
 
#1

Verschlüsseln in PHP und entschlüsseln in Delphi?

  Alt 6. Mär 2024, 14:41
Hallo an alle,

ich stehe aktuell vor dem Problem, dass ich mit Delphi und PHP Daten verschlüsselt austauschen möchte. Also primär vom Webserver zu Delphi und später eventuell auch umgekehrt.

Der Abruf selbst ist kein Problem, aber die Entschlüsselung.
Ich habe dazu so einiges hier im Forum und sonstwo im Netz gefunden und ausprobiert, aber eine wirklich funktionierende Lösung habe ich bisher nicht gefunden.

Eine einzige Lösung hat zwar soweit korrekt entschlüsselt, konnte aber nicht mit UTF-8 umgehen, was für meine geplante Anwendung aber notwendig ist.

Hat sich hier schon mal jemand mit einer solchen Lösung beschäftigt und kann mir einen Tipp geben, oder kennt jemand ein wirklich funktionierendes Beispiel?

Gruß Gerd
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
213 Beiträge
 
#2

AW: Verschlüsseln in PHP und entschlüsseln in Delphi?

  Alt 6. Mär 2024, 15:03
Hallo an alle,

ich stehe aktuell vor dem Problem, dass ich mit Delphi und PHP Daten verschlüsselt austauschen möchte. Also primär vom Webserver zu Delphi und später eventuell auch umgekehrt.

Der Abruf selbst ist kein Problem, aber die Entschlüsselung.
Ich habe dazu so einiges hier im Forum und sonstwo im Netz gefunden und ausprobiert, aber eine wirklich funktionierende Lösung habe ich bisher nicht gefunden.

Eine einzige Lösung hat zwar soweit korrekt entschlüsselt, konnte aber nicht mit UTF-8 umgehen, was für meine geplante Anwendung aber notwendig ist.

Hat sich hier schon mal jemand mit einer solchen Lösung beschäftigt und kann mir einen Tipp geben, oder kennt jemand ein wirklich funktionierendes Beispiel?

Gruß Gerd
Hi,

There can't be one solution to fix such problem, the problem is not with Delphi and not in PHP per se, but with the used libraries and their default in primarily PHP and then Delphi.

To help you solve this, which might be trivial, you need to share more details, like PHP library (with links), your working solution and sample(s) php encrypted data, with of course key (and/or if there is iv or all the parameters like if a password used instead of key), in other words more datils needed.
  Mit Zitat antworten Zitat
Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.755 Beiträge
 
Delphi 10.4 Sydney
 
#3

AW: Verschlüsseln in PHP und entschlüsseln in Delphi?

  Alt 6. Mär 2024, 15:05
wie wird in php verschluesselt?
Wie funktioiert die einzige Loesung?

Das encoding sollte hier doch keine Rolle spielen, denn normalerweise wird auf Byte-Ebene verschluesselt.

Gruesse
Klaus
Klaus
  Mit Zitat antworten Zitat
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
11.014 Beiträge
 
Delphi 12 Athens
 
#4

AW: Verschlüsseln in PHP und entschlüsseln in Delphi?

  Alt 6. Mär 2024, 15:08
Das encoding sollte hier doch keine Rolle spielen, denn normalerweise wird auf Byte-Ebene verschluesselt.
Leider gibt es noch viel zu viele alte Lösungen, die auf PAnsiChar aufsetzen und von Encoding noch nichts gewusst haben.
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.149 Beiträge
 
Delphi 12 Athens
 
#5

AW: Verschlüsseln in PHP und entschlüsseln in Delphi?

  Alt 6. Mär 2024, 15:17
PHP wird heute oft als UTF-8 laufen. In Delphi sind Strings aber nun standardmäßig Unicode.

Wenn man also einen "String" ver-/entschlüsselt (z.B. ein Passwort), dann muß schon beachtet werden, welche Encoding auf jeder Seite benutzt wird.


Je, byte-weise wird meistens ver-/entschlüsselt,
aber wenn du jetzt UTF-8 vertschlüsslst, und das entschlüsselte als Unicode (2 Byte) betrachtest, dann sieht das Ergebnis ein bissl chinesisch aus.


Ansonsten braucht es natürlich auf beiden Seiten auch die gleichen Algorithmen und eventuell noch auf gleiche Settings achten.
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu ( 6. Mär 2024 um 15:20 Uhr)
  Mit Zitat antworten Zitat
gpl

Registriert seit: 28. Jan 2010
Ort: Nähe Bonn
12 Beiträge
 
Delphi 11 Alexandria
 
#6

AW: Verschlüsseln in PHP und entschlüsseln in Delphi?

  Alt 6. Mär 2024, 15:19
Ich habe jetzt gefühlt 100 verschiedene Ansätze ausprobiert und viele davon waren schon älter und konnten wohl daher nicht mit UTF-8 umgehen.

Auf der PHP-Seite habe ich zum Testen ein kleines Script aus den gefundenen Ansätzen erstellt, dass einen Text verschlüsselt und sofort wieder entschlüsselt.
Wie man sieht, funktioniert das innerhalb PHP problemlos.

Code:
<?php

function MyEncrypt ($data, $key, $iv)
  {
    //Remove the base64 encoding from our key
    $encryption_key = $key;
    //Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
    $encrypted = openssl_encrypt($data, 'AES-256-CBC', $encryption_key, 0, $iv);
    //The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
    return base64_encode($encrypted);
  }
 
function MyDecrypt ($data, $key, $iv)
  {
   $encryption_key = $key;
   $indata = base64_decode($data); // Base64-Dekodieren
   $unencrypted = openssl_decrypt($indata, 'AES-256-CBC', $encryption_key, 0, $iv);
   return $unencrypted;
  } 

$key = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF";
$plaintext = 'zu verschlüsselnde Nachricht äöüÄÖÜß';
$iv = "1234567890123456";
echo 'Plaintext    : '.$plaintext.'<BR>';
echo 'Key          : '.$key.' ('.strlen($key).')<BR>';
echo 'IV           : '.$iv.' ('.strlen($iv).')<BR>';
$enc = MyEncrypt($plaintext, $key, $iv);
echo 'Verschlüsselt : '.$enc.'<BR>';
echo '<br>';
echo 'Entschlüsselt : '.MyDecrypt($enc, $key, $iv);
 
?>
Ergebnis:
Code:
Plaintext : zu verschlüsselnde Nachricht äöüÄÖÜß
Key : ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEF (32)
IV : 1234567890123456 (16)
Verschlüsselt : akMrallnS09ZVmlvOER5Y2w3WCtsVDJ3UEpYbTY4Yy9QQWg2N0xjaFYrdHNpL29pUEdOVVdvUHZNT2ZiQVMwWQ==

Entschlüsselt : zu verschlüsselnde Nachricht äöüÄÖÜß
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.149 Beiträge
 
Delphi 12 Athens
 
#7

AW: Verschlüsseln in PHP und entschlüsseln in Delphi?

  Alt 6. Mär 2024, 15:23
für AES mit CBC
siehe GetIt:

LockBox
Delphi Encryption Compendium
usw.

oder im Indy sollte sich auch was für OpenSSL finden, wenn man dessen AES-Code nutzen mächte
sowie andere OpenSSL-Libs für Delphi
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu ( 6. Mär 2024 um 15:25 Uhr)
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
213 Beiträge
 
#8

AW: Verschlüsseln in PHP und entschlüsseln in Delphi?

  Alt 7. Mär 2024, 09:11
@gpl ,

Please look at the answer for this question on SO :
https://stackoverflow.com/questions/...ncrypt-decrypt

Then adjust the PHP code to something similar then after that we talk about Delphi, encrypting data and send it over the wire (Internet) should be authenticated, it looks like trivial and not needed but when things go south the damage will be huge.

By just encrypting some data with fixed IV (!) and send it to different machine with authentication (for verification of integrity of the data) is like loading a gun with live bullet and aim it at your feet and wait, then wait for someone cough loudly around you !

I saw a damage similar to this implementation, the server was using different (random) IV per encryption as it should, yet there wasn't any authentication, the thing is with AES or any other symmetric encryption is that it will produce data no matter for any key (and IV), and there is no way to know if it is corrupted or not, anyway the server was sending the data and in that software after an update they request extra fields for the address (extra phone number) and someone forgot few lines that handle the password(s) from internally debugging the development build, after putting that server online some clients updated by adding the fields not looking that the other fields is garbage (or mostly empty because it was structured as list of strings) then clicked apply, data sent to the server and saved now it is all the fields are lost except the last one, which also unreadable because the list (data structure) was violated too.

This could have been prevented by adding an authentication, hence the decryption with wrong key(IV or password ...) could either raise an exception or simply not get the corrupt data allowing overwriting and losing valuable information.

So and sorry for long writing, either take some advices from that SO question or search for better solution, change the PHP side after, only after that you will not need a lot to write the same with Delphi as long you are not using some PHP library with unknown default parameters.

Also my suggestion is something like this answer to be exact:
https://stackoverflow.com/questions/...72528#46872528
it does Encrypt-then-MAC, but if you are going to use fixed IV (highly not recommended and simply wrong) then you should switch to MAC-then-Encrypt by calculating the HMAC for "$plaintext . iv" instead of "$ciphertext . iv" then test for data integrity (unlike that example) after the decryption not before.

Also you didn't mention what is the encryption library in Delphi you will use or prefer to.

ps: as said above the problem is from string encoding.
ps2 : DON'T REUSE IV, IV should be unique for each encryption, also should be sent over with the encrypted data, reusing it will defeat the whole point of the encryption.
  Mit Zitat antworten Zitat
TurboMagic

Registriert seit: 28. Feb 2016
Ort: Nordost Baden-Württemberg
2.825 Beiträge
 
Delphi 12 Athens
 
#9

AW: Verschlüsseln in PHP und entschlüsseln in Delphi?

  Alt 7. Mär 2024, 21:54
PHP wird heute oft als UTF-8 laufen. In Delphi sind Strings aber nun standardmäßig Unicode.
Vorsicht! Auch UTF8 ist Unicode! Nur eben anders codiert.
Aber das wusstest du ja bestimmt schon.
  Mit Zitat antworten Zitat
TurboMagic

Registriert seit: 28. Feb 2016
Ort: Nordost Baden-Württemberg
2.825 Beiträge
 
Delphi 12 Athens
 
#10

AW: Verschlüsseln in PHP und entschlüsseln in Delphi?

  Alt 7. Mär 2024, 21:57
für AES mit CBC
siehe GetIt:

LockBox
Delphi Encryption Compendium
usw.

oder im Indy sollte sich auch was für OpenSSL finden, wenn man dessen AES-Code nutzen mächte
sowie andere OpenSSL-Libs für Delphi


Öhm, sorry wenn ich dich schon wieder korrigieren muss. Aber die Delphi Encryption Compendium Fassung
im GetIt ist dafür unbrauchbar, da diese keine Verschlüsselungen enthält. Du findest diese aber auf
GitHub:

https://github.com/MHumm/DelphiEncryptionCompendium

Grüße
TurboMagic
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:34 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz