douxin2002 2012-11-22 04:50
浏览 94

使用Phpseclib在Java中加密并在PHP中解密

Edit 2: Problem has been solved. I didn't understand how the loadkey function in php was supposed to work and I mistakenly assumed it would read a key file (it does not). The solution was to read the contents of the file into a variable and then load the variable with loadkey.

Edit: The problem seems to be with the key. I realized that loadkey is returning false, indicating that it was unable to read the key. Could there be a difference in the formats accepted by phpseclib and the keys created in java?


I am trying to encrypt a AES key in Java (android) and decrypt it in PHP to use symmetric encryption for data transfer. Currently, I am able to encrypt and decrypt a short file or string using RSA in Java, but have not been able to decrypt it in PHP.

I am using phpseclib to decrypt in PHP, and i dont get any error, but my output string is null.

This is the code I am using:

Java:

File archivo_llave_publica = new File(direccion);
        byte[] bytes_llave = leer(archivo_llave_publica);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");          
        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(bytes_llave);
        PublicKey pubKey = keyFactory.generatePublic(publicKeySpec);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        byte[] cipherData = cipher.doFinal(src);
        return cipherData;

PHP:

<?php
include('./Crypt/RSA.php');

$rsa = new Crypt_RSA();
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->loadKey('./key/Pri.txt'); // public key
$temprsa = $rsa->decrypt($key);
if ($temprsa==null){echo "null decrypt";}

Code used to generate keys:

public void generaArchivoLlaves(String pub_file, String pri_file){
    File LlavePrivada = new File(raiz.getAbsolutePath()+"/Bushfire/"+pri_file);
    File LlavePublica = new File(raiz.getAbsolutePath()+"/Bushfire/"+pub_file);
    try {
        KeyPair kp = generaLlaves();
        byte[] privateKeyBytes = kp.getPrivate().getEncoded();
        byte[] publicKeyBytes = kp.getPublic().getEncoded();
        Toast.makeText(this, "Privada:  "+kp.getPrivate().getFormat(), Toast.LENGTH_LONG).show();
        Toast.makeText(this, "Pública:  "+kp.getPublic().getFormat(), Toast.LENGTH_LONG).show();
        escribir(LlavePrivada, privateKeyBytes);
        escribir(LlavePublica, publicKeyBytes);
    }
    catch (NoSuchAlgorithmException e) {Toast.makeText(this, "Error al generar llave", Toast.LENGTH_LONG).show();}
}

public KeyPair generaLlaves() throws NoSuchAlgorithmException{
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(2048);
    KeyPair kp = kpg.genKeyPair();  
    //Toast.makeText(this, "Se generó correctamente", Toast.LENGTH_LONG).show();
    return kp;

}

NOTE: Function escribir just writes the data byte by byte to a file.

What could be causing the problem?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?
    • ¥100 求三轴之间相互配合画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 自己瞎改改,结果现在又运行不了了