Closed. This question needs to be more focused。它当前不接受答案。
                        
                    
                
            
        
            
        
                
                    
                
            
                
                    想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                
                    2个月前关闭。
            
        

    

我有一个使用php中的公共密钥编码加密逻辑的项目。
给定以下示例Java代码,如何在php中进行编码?
规范文件说用RSA(2048位)加密源。

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONObject;

//some other codes...

String PUBLIC_KEY = "";
Map<String, String> map = new HashMap<>();
String data = new JSONObject(map).toString();

//generating public key object
byte[] buffer = Base64.decodeBase64(PUBLIC_KEY.getBytes());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
//encryption data
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedByte = cipher.doFinal(data.getBytes());
//base 64
String encrypted = Base64.encodeToString(encryptedByte, Base64.DEFAULT);
return encrypted;

最佳答案

通过使用模块phpseclib \ Crypt \ RSA自己解决了php转换

    $token = file_get_contents($path);
    $base64_decoded = base64_decode($token);
    $pubKey = new RSA();
    $pubKey->setEncryptionMode(RSA::ENCRYPTION_PKCS1);
    $pubKey->loadKey($base64_decoded);
    $crypted = $pubKey->encrypt($json);
    return base64_encode($crypted);

10-06 13:42
查看更多