我需要两个函数来加密 php 中的一些数据并在 java 中“正确地”解密它

发布于 2024-09-16 19:17:25 字数 140 浏览 1 评论 0原文

我需要获得两个功能。我想以 xml 格式将数据从我的网站传输到我的服务器。现在在我的服务器上,我想创建一个函数来加密数据并将其放入 xml 中,并使用 java 中的另一个函数来解密它。

请告诉我是否有任何预定义的功能,或者您能抽出 5 分钟时间吗?

I need to get two functions. I want to transfer data from my website to my server in xml format. Now on my server, I want to make a function that encrypts the data and place it in an xml, and another function in java to decrypt it.

Please tell me if there is any predefined function or can you just spare 5 minutes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

硬不硬你别怂 2024-09-23 19:17:25

好吧,您可以在 PHP 中使用任何加密 mcrypt 函数。 AES 128 加密的一个示例:

  $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  $key = "Put your secret key here";
  $text = "<xml>This is your XML text</xml>";

  //encrypting now with RIJNDAEL 128 encryption.
  $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB, $iv);

  //Display encrypted content
  echo $crypttext;

对于解密,请使用此代码(我不是 Java 专业人士,因此可能存在一些错误):

package org.kamal.crypto;

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;

public class SimpleProtector
{
    private static final String ALGORITHM = "AES";
    private static final byte[] keyValue = 
        new byte[] { 'P', 'u', 't', ' ', 'Y', 'o', 'u', 'r', ' ', 'S', 'e', 'c', 'r', 'e', 't', ' ', 'K', 'e', 'y', '', 'H', 'e', 'r', 'e'};

    public static String decrypt(String encryptedValue) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGORITHM);
        // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        // key = keyFactory.generateSecret(new DESKeySpec(keyValue));
        return key;
    }
}

Well, you can use any encrypting mcrypt function in PHP. One example for encrypting in AES 128:

  $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  $key = "Put your secret key here";
  $text = "<xml>This is your XML text</xml>";

  //encrypting now with RIJNDAEL 128 encryption.
  $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_ECB, $iv);

  //Display encrypted content
  echo $crypttext;

And for decrypting, use this code (I'm not a Java pro, so there may be some bugs):

package org.kamal.crypto;

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;

public class SimpleProtector
{
    private static final String ALGORITHM = "AES";
    private static final byte[] keyValue = 
        new byte[] { 'P', 'u', 't', ' ', 'Y', 'o', 'u', 'r', ' ', 'S', 'e', 'c', 'r', 'e', 't', ' ', 'K', 'e', 'y', '', 'H', 'e', 'r', 'e'};

    public static String decrypt(String encryptedValue) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    private static Key generateKey() throws Exception {
        Key key = new SecretKeySpec(keyValue, ALGORITHM);
        // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        // key = keyFactory.generateSecret(new DESKeySpec(keyValue));
        return key;
    }
}
把昨日还给我 2024-09-23 19:17:25

没有必要在这里重新发明轮子。使用 SSL,这是 HTTPS 请求所涉及的。您可以通过 CURL 来完成这些操作。

PHP内置了curl,也有java版本
http://php.net/manual/en/book.curl.php

JAVA 中的 cURL 等效项

希望有所帮助。

No point reinventing the wheel here. Use SSL, which is what an HTTPS request would involve. You can do those through CURL.

curl is built into PHP and there is also a java version
http://php.net/manual/en/book.curl.php

cURL equivalent in JAVA

Hope that helps.

归途 2024-09-23 19:17:25

您是否研究过 JSON

它没有加密,但它是在不同程序之间来回传递数据的简单方法和语言。

Have you looked into JSON?

It's not encrypted, but it's an easy way to pass data back and forth between different programs and languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文