简体   繁体   English

android base64在php中编码和解码

[英]android base64 encode and decode in php

hi i'm trying to decode two strings then use the return result (byte[]) then put it in a Biginteger constructor Like this : 嗨,我正在尝试解码两个字符串,然后使用返回结果(byte []),然后将其放入Biginteger构造函数中,如下所示:

BigInteger bigInteger1 = new BigInteger(1, Base64.decode(myString1,0));
BigInteger bigInteger2 = new BigInteger(1, Base64.decode(myString2,0));

then put this BigIntegers on a java.security.KeyFactory class to create a RSAPublicKey like This : 然后将此BigIntegers放在java.security.KeyFactory类上,以创建一个RSAPublicKey,如下所示:

KeyFactory.getInstance(ALG).generatePublic(new RSAPublicKeySpec(bigInteger1,bigInteger2));

then use my public key to encode a string Like this : 然后使用我的公共密钥编码一个像这样的字符串:

public static String encrypt(PublicKey publicKey, String str) {
 Cipher instance = Cipher.getInstance(ALG);
            instance.init(1, publicKey);
            Base64.encodeToString(instance.doFinal(str.getBytes()), 2);
}

on PHP . PHP上 I achieve this goal with android but when I want to do it with PHP I have a lot of problems even on the start when I want to decode my string in PHP with this code : 我用android实现了这个目标,但是当我想用PHP做到这一点时,即使是一开始,当我想用​​以下代码在PHP中解码字符串时,我也会遇到很多问题:

$encoded = base64_decode($base,true);
$decoded = utf8_decode(base64_decode($encoded));

I will get this string: ??2?]????5N?[??S 我将得到以下字符串: ?? 2?] ???? 5N?[?? S

but in android, the decoded string is totally different and always stay the same result 但在android中,解码后的字符串完全不同,并且始终保持相同的结果

I tried to do this job on JSP but it's really hard to learn a new language and I don't have the time. 我试图在JSP上完成这项工作,但是学习一种新语言确实很困难,而且我没有时间。

Can I do this project in spring boot? 我可以在春季启动中完成这个项目吗? I have the codes for java 我有Java代码

please, somebody, help me. 请有人帮帮我。

You're decoding string twice. 您将字符串解码两次。 You should try : 你应该试试 :

$encoded = base64_encode($base); // and not base64_decode
$decoded = base64_decode($encoded); // will be $base

utf8_decode is converting a string with ISO-8859-1 characters encoded with UTF-8 to single-byte ISO-8859-1. utf8_decode正在将使用UTF-8编码的ISO-8859-1字符的字符串转换为单字节ISO-8859-1。 Are you sure you're needing it ? 确定要用吗?

In PHP, you can use intval to performs BigInteger() but I'm not sure you won't be facing an integer overflow. 在PHP中,您可以使用intval执行BigInteger()但我不确定您是否不会遇到整数溢出。

Finally, OpenSSL library will certainly do the job for key generation and encryption. 最后, OpenSSL库肯定会完成密钥生成和加密的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM