dongzhaiqiang6108 2019-03-14 03:39
浏览 142
已采纳

在PHP代码加密中转换Java AES加密

I am trying to encrypt the otp in php. i have the java code that encrypt the otp and when i send that encrypted otp it decrypt at client end as expected. but when i encrypt it using php, decryption does not work.

Actual Java Encryption:

public static final String appKey = "wbx+mGnapzZMietP0gK6muJb/vUU7jnAk9Fe5gTHh4w=";    
public static String encryptEK(byte[] plainText, byte[] secret){
        try{
            SecretKeySpec sk = new SecretKeySpec(secret, AES_ALGORITHM);
            ENCRYPT_CIPHER.init(Cipher.ENCRYPT_MODE, sk);
            return Base64.encodeBase64String(ENCRYPT_CIPHER.doFinal(plainText));
        }catch(Exception e){
            e.printStackTrace();
            return "";
        }
    }
    public static String encryptOTP(String otp)
    {
        String encryptedOtp = null;
        try {

            encryptedOtp = encryptEK(otp.getBytes(),decodeBase64StringTOByte(appKey));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedOtp;
    }

encryptOTP("251826")

Current PhP encryption.

    class AtomAES {
     public function encrypt_aps_secret($data = '', $key = NULL) {
            if($key != NULL && $data != ""){
                $method = "AES-256-ECB";
                $encrypted = openssl_encrypt($data, $method, $key, OPENSSL_RAW_DATA);
                $result = base64_encode($encrypted);
                return $result;
            }else{
                return "String to encrypt, Key is required.";
            }
        }
}
    $appKey = mb_convert_encoding("wbx+mGnapzZMietP0gK6muJb/vUU7jnAk9Fe5gTHh4w=", "UTF-8");
    $enc_otp = $atomAES->encrypt_aps_secret("251826", base64_decode(base64_encode($appKey)));
    print_r(json_encode(array("enc_otp"=>mb_convert_encoding($enc_otp, "UTF-8"))));

I need the exact encryption as java does using php. how to achieve this

展开全部

  • 写回答

1条回答 默认 最新

  • dpbv85276 2019-03-14 15:00
    关注
    base64_decode(base64_encode($appKey))
    

    I believe you use the key in php as string bytes, not as a decoded byte array, try following

    base64_decode($appKey)
    

    It is at least what meets the eye. Still there are other assumptions, such as we can only assume ENCRYPT_CIPHER is AES/ECB/PKCS5Padding, as well you should create a new Cipher instance every time, as the ENCRYPT_CIPHER may not be thread-safe

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 sql server2008r2索引超出了数组界限
  • ¥15 MATLAB图片转灰度格式问题
  • ¥15 把h5作品链接复制到自己的账号里
  • ¥15 ensp抓包实验配置
  • ¥15 强化学习算法、MRO
  • ¥15 想要学习一门技术来保证以后能够吃上碗饭,该学什么好
  • ¥50 Verilog硬件开发,射频分析求解答
  • ¥20 matlab绘冲床平面连杆机构图和仿真,求帮助
  • ¥15 为什么树莓派5b显示禁止连接
  • ¥15 请专家处理报错!基于深度学习的车型分类问题:数据集为包含 10 种车型的图像数据集,分为训练集、验证集和测试集。
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部