简体   繁体   中英

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 :

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 :

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 . 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 :

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

I will get this string: ??2?]????5N?[??S

but in android, the decoded string is totally different and always stay the same result

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.

Can I do this project in spring boot? I have the codes for 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. 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.

Finally, OpenSSL library will certainly do the job for key generation and encryption.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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