PHP与android实现openssl 参数加密通信

ANDROID代码:

package com.app.util;

import android.util.Base64;
import com.app.model.RuntimeData;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;

public class RSAUtil {
  private static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";
  
  public static String encrypt(byte[] paramArrayOfbyte) throws Exception {
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decode(("xxxx这里为公密", 2));
    PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec);
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
    cipher.init(1, publicKey);
    return Base64.encodeToString(cipher.doFinal(paramArrayOfbyte), 2);
  }
  
  public static String getMd5ByFile(File paramFile) throws Exception {
    Exception exception;
    try {
      FileInputStream fileInputStream = new FileInputStream(paramFile);
    } finally {
      exception = null;
    } 
    if (paramFile != null)
      paramFile.close(); 
    throw exception;
  }
}
 
PHP代码:

$private_key="私钥部分";
$public_key="公钥部分"
$pi_key = openssl_pkey_get_private($private_key);
$pu_key = openssl_pkey_get_public($public_key);
/*
#公钥加密数据 rsa+base64
$data = "hello world!";
openssl_public_encrypt($data,$encrypted,$pu_key);
$base64_encrypted = base64_encode($encrypted);
//echo $base64_encrypted."\n";
*/

#私钥解密数据
//openssl_private_decrypt(base64_decode($base64_encrypted),$decrypted_data,$pi_key); 
$mobile=$str;
openssl_private_decrypt(base64_decode($mobile),$decrypted_data,$pi_key);


转载声明:本文为红盾科技技术分享平台的原创文章,转载请注明原文地址,谢谢合作

发表评论: