Unable to exchange data encrypted with AES-256 between Java and PHP

前端 未结 4 1567
陌清茗
陌清茗 2020-12-04 22:51

My problem is: what I encrypt in Java I can decrypt perfectly in Java, but PHP mcrypt can\'t decrypt. What I encrypt with mcrypt I can decrypt with

4条回答
  •  一向
    一向 (楼主)
    2020-12-04 23:32

    Keep in mind to have the same encoding for the strings. Try to convert the strings in both languages to UTF-8, e.g., and than convert to binary data that is encoded:

    PHP (s. utf8_encode() function):

    $strAndBlob = utf8_encode("My string");
    

    Java:

    String str = "My string";
    byte[] blob = str.getBytes("utf-8");
    

    PHP, e.g., must not use UTF-8 by default.

提交回复
热议问题