B4X:
#If JAVA
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
class Security
{
private final String TAG = "IABUtil/Security";
private final String KEY_FACTORY_ALGORITHM = "RSA";
private final String SIGNATURE_ALGORITHM = "SHA1withRSA";
public boolean verifyPurchase(String base64PublicKey, String signedData, String signature)
{
if (TextUtils.isEmpty(signedData) || TextUtils.isEmpty(base64PublicKey) || TextUtils.isEmpty(signature))
{
Log.e(TAG, "Purchase verification failed: missing data.");
return false;
}
PublicKey key = Security.generatePublicKey(base64PublicKey);
return Security.verify(key, signedData, signature);
}
public static PublicKey generatePublicKey(String encodedPublicKey)
{
try
{
byte[] decodedKey = Base64.decode(encodedPublicKey, Base64.DEFAULT);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
}
catch (NoSuchAlgorithmException e)
{
throw new RuntimeException(e);
}
catch (InvalidKeySpecException e)
{
Log.e(TAG, "Invalid key specification.");
throw new IllegalArgumentException(e);
}
catch (IllegalArgumentException e)
{
Log.e(TAG, "Base64 decoding failed.");
throw e;
}
}
public boolean verify(PublicKey publicKey, String signedData, String signature)
{
Signature sig;
try
{
sig = Signature.getInstance(SIGNATURE_ALGORITHM);
sig.initVerify(publicKey);
sig.update(signedData.getBytes());
if (!sig.verify(Base64.decode(signature, Base64.DEFAULT)))
{
Log.e(TAG, "Signature verification failed.");
return false;
}
return true;
}
catch (NoSuchAlgorithmException e)
{
Log.e(TAG, "NoSuchAlgorithmException.");
}
catch (InvalidKeyException e)
{
Log.e(TAG, "Invalid key specification.");
}
catch (SignatureException e)
{
Log.e(TAG, "Signature exception.");
}
catch (IllegalArgumentException e)
{
Log.e(TAG, "Base64 decoding failed.");
}
return false;
}
}
#End If
B4A Versione: 9.30
Java Versione: 8
Analisi del Codice. (0.02s)
Building folders structure. (0.01s)
Compilazione del codice. (0.02s)
Compilazione del codice di layouts (0.00s)
Organizzazione Librerie. (0.00s)
(AndroidX SDK)
Generazione file R. (0.00s)
Compilazione del codice debugger. (0.00s)
Compilazione del codice Java prodotto. Error
javac 1.8.0_202
src\com\angelo\letturaXML\main.java:1512: error: non-static method verify(PublicKey,String,String) cannot be referenced from a static context
return Security.verify(key, signedData, signature);
^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error