小苹果6318 2016-06-14 14:46 采纳率: 50%
浏览 1482
已采纳

哪位大神能帮忙把这个java加密解密函数 用php写一个,要求结果一致!

哪位大神能帮忙把这个java加密解密函数 用php写一个,要求结果一致,谢谢了!

SecurityHelper

package com.units;
import java.security.*;

import javax.crypto.*;

import javax.crypto.spec.*;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class SecurityHelper {

protected final static Log logger = LogFactory.getLog(SecurityHelper.class);

private final static int ITERATIONS = 20;

public static String encrypt(String key, String plainText) throws Exception {  

    try {  
        byte[] salt = new byte[8];  
        MessageDigest md = MessageDigest.getInstance("MD5");  
        md.update(key.getBytes());  
        byte[] digest = md.digest();  
        for (int i = 0; i < 8; i++) {  
            salt[i] = digest[i];  
        }  
        PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray());  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");  
        SecretKey skey = keyFactory.generateSecret(pbeKeySpec);  
        PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);  
        Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");  
        cipher.init(Cipher.ENCRYPT_MODE, skey, paramSpec);  
        byte[] cipherText = cipher.doFinal(plainText.getBytes());  
        String saltString = new String(Base64.encode(salt));  
        String ciphertextString = new String(Base64.encode(cipherText));  
        return saltString + ciphertextString;  
    } catch (Exception e) {  
        throw new Exception("Encrypt Text Error:" + e.getMessage(), e);  
    }  
}  

public static String decrypt(String key, String encryptTxt) throws Exception {  
    int saltLength = 12;  
    try {  
        String salt = encryptTxt.substring(0, saltLength);  
        String ciphertext = encryptTxt.substring(saltLength, encryptTxt.length());  
        byte[] saltarray = Base64.decode(salt.getBytes());  
        byte[] ciphertextArray = Base64.decode(ciphertext.getBytes());  
        PBEKeySpec keySpec = new PBEKeySpec(key.toCharArray());  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");  
        SecretKey skey = keyFactory.generateSecret(keySpec);  
        PBEParameterSpec paramSpec = new PBEParameterSpec(saltarray, ITERATIONS);  
        Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");  
        cipher.init(Cipher.DECRYPT_MODE, skey, paramSpec);  
        byte[] plaintextArray = cipher.doFinal(ciphertextArray);  
        return new String(plaintextArray);  
    } catch (Exception e) {  
        throw new Exception(e);  
    }  
}  



public static void main(String[] args) {  
    String encryptTxt = "";  
    String plainTxt = "{\"code\":\"0000\",\"detail\":\"成功\"}";  
    try {  
        System.out.println(plainTxt);  
        encryptTxt = encrypt("mypassword01", plainTxt);  
        plainTxt = decrypt("mypassword01", encryptTxt);  
        System.out.println(encryptTxt);  
        System.out.println(plainTxt);  
    } catch (Exception e) {  
        e.printStackTrace();  
        System.exit(-1);  
    }  
}  

}

Base64

package com.units;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import javax.mail.internet.MimeUtility;

public class Base64 {

public static byte[] encode(byte[] b) throws Exception {  
    ByteArrayOutputStream baos = null;  
    OutputStream b64os = null;  
    try {  
        baos = new ByteArrayOutputStream();  
        b64os = MimeUtility.encode(baos, "base64");  
        b64os.write(b);  
        b64os.close();  
        return baos.toByteArray();  
    } catch (Exception e) {  
        throw new Exception(e);  
    } finally {  
        try {  
            if (baos != null) {  
                baos.close();  
                baos = null;  
            }  
        } catch (Exception e) {  
        }  
        try {  
            if (b64os != null) {  
                b64os.close();  
                b64os = null;  
            }  
        } catch (Exception e) {  
        }  
    }  
}  

public static byte[] decode(byte[] b) throws Exception {  
    ByteArrayInputStream bais = null;  
    InputStream b64is = null;  
    try {  
        bais = new ByteArrayInputStream(b);  
        b64is = MimeUtility.decode(bais, "base64");  
        byte[] tmp = new byte[b.length];  
        int n = b64is.read(tmp);  
        byte[] res = new byte[n];  
        System.arraycopy(tmp, 0, res, 0, n);  
        return res;  
    } catch (Exception e) {  
        throw new Exception(e);  
    } finally {  
        try {  
            if (bais != null) {  
                bais.close();  
                bais = null;  
            }  
        } catch (Exception e) {  
        }  
        try {  
            if (b64is != null) {  
                b64is.close();  
                b64is = null;  
            }  
        } catch (Exception e) {  
        }  
    }  
}  

}

  • 写回答

2条回答 默认 最新

  • _1_1_7_ 2016-06-15 00:39
    关注

    可以考虑一下用PHP调用java的class的方式:
    直接用命令行方式调用,java的main函数传两个参数(加密时:密钥+明文,解密时:密钥+密文);
    或者使用php-java-bridge包;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作