#Region Project Attributes
#ApplicationLabel: b4aDecrypt
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#AdditionalJar: bcprov-jdk16-1.43
'#AdditionalJar: org-apache-commons-codec
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim nativeMe As JavaObject
Dim Base64Con As Base64Image
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim su As StringUtils
Private wv1 As WebView
Dim WebViewExtras1 As WebViewExtras
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("main")
wv1.JavaScriptEnabled = True
WebViewExtras1.addJavascriptInterface(wv1, "B4A") ' NOT ESSENTIAL
WebViewExtras1.addWebChromeClient(wv1, "")
Dim jv As String = File.ReadString(File.DirAssets, "javascript.txt")
Log("JV = " & jv)
WebViewExtras1.executeJavascript(wv1, jv)
Sleep(1000)
Base64Con.Initialize
nativeMe.InitializeContext
' nativeMe.RunMethod("makeBollie", Null)
'This is the HEX string that I generated from the returned scanned PDF417 barcode on my drivers license
' Dim mystring As String = "019b094500006713c45dcaf37a7573c2daed9badf4374e3629389f8a6bf3c65104384407f66f3233d4bab833052c89dd15cc80f930691ff8fcc77bf0494ea408b5ec0465a9b88580c95c46cc21349fca03a10b95778cbd9672c53d150e4aae7cbf9b61bdadcd3c69f11033236d2e1227f69f49db401a85a7021a547c616b7038337238d37d1086bb3c730c737faac164bae9cdaf0273904fa875799078779660f87fbf0a28af53739ac87c39758100fd9bcc3bedc8716c35212b7f911add64c43281bd18eb12f1bc7f01249f14ecf7e6e8ed2e2c66f32f0716aef21b5b7febd889457632caaa878b6d253d5d4f038de565df54b29c8b3dd87714780c0e8c027b1577d9375f76c2ee868bf2760e27dd2ac4b1611ac2f0695884d2eb80b00c2cd52505a3f3acf58a2306a47172519e67dd3c5ac1e6e49f8535fec93e8d9b322ef9ba2cc5b092ede4a45fa0ba7c014463e860d6faf105ebec9baee6a3255c343a0cc9974b379a7d70b346986688513ef22493fb4d1ae8443cd721827f7eec933891e07f471cc2e197c11ea82233d32c666f70802dae71d2bafcec2550f7458d40c23f0c0f3d9452253d9ae6c79bd7d9592fa744c1c3c40b945f48693ca1b447675987ea51d631bed99c63865b25a8446ff2259a454f3707646c27812f175410c930c82c60c3126755650c5d137dcd15b286c32dcccbb3074d667299144df44cc4eb24675e8827835a581ef384754c2c007d720d82c89c7608cac15437b060f2ef9a97547fca3adbad61da1e9b0db9061fe83c10cf326e92b8b7e787ef8e45ffc056e100a504ae09578fdcedd0d3abf1286c776e7a4bfd02b93f34e9121bf66f17a77a4db36ba04032a553892d1c3f6c377be4f7696357bebaa983ea3166592c8afc35282ae55aba64d83f7436aa0b4b58670855a28f2e14e67b2e84411e39b7fe19cd68be5ca411b9f60a9f0339e19ae8bda50223fb4930df0b8a20ed4528a601296a814721245db392b6afa929f1d547c3"
'This is the above mystring split into 5 blocks of 128 bytes and 1 block of 74 buytes. The first 6 bytes (019b09450000) should be ignored as it only relates to the License Version Number.
Dim mystring1 As String = "6713c45dcaf37a7573c2daed9badf4374e3629389f8a6bf3c65104384407f66f3233d4bab833052c89dd15cc80f930691ff8fcc77bf0494ea408b5ec0465a9b88580c95c46cc21349fca03a10b95778cbd9672c53d150e4aae7cbf9b61bdadcd3c69f11033236d2e1227f69f49db401a85a7021a547c616b7038337238d37d10"
Dim mystring2 As String = "86bb3c730c737faac164bae9cdaf0273904fa875799078779660f87fbf0a28af53739ac87c39758100fd9bcc3bedc8716c35212b7f911add64c43281bd18eb12f1bc7f01249f14ecf7e6e8ed2e2c66f32f0716aef21b5b7febd889457632caaa878b6d253d5d4f038de565df54b29c8b3dd87714780c0e8c027b1577d9375f76"
Dim mystring3 As String = "c2ee868bf2760e27dd2ac4b1611ac2f0695884d2eb80b00c2cd52505a3f3acf58a2306a47172519e67dd3c5ac1e6e49f8535fec93e8d9b322ef9ba2cc5b092ede4a45fa0ba7c014463e860d6faf105ebec9baee6a3255c343a0cc9974b379a7d70b346986688513ef22493fb4d1ae8443cd721827f7eec933891e07f471cc2e1"
Dim mystring4 As String = "97c11ea82233d32c666f70802dae71d2bafcec2550f7458d40c23f0c0f3d9452253d9ae6c79bd7d9592fa744c1c3c40b945f48693ca1b447675987ea51d631bed99c63865b25a8446ff2259a454f3707646c27812f175410c930c82c60c3126755650c5d137dcd15b286c32dcccbb3074d667299144df44cc4eb24675e882783"
Dim mystring5 As String = "5a581ef384754c2c007d720d82c89c7608cac15437b060f2ef9a97547fca3adbad61da1e9b0db9061fe83c10cf326e92b8b7e787ef8e45ffc056e100a504ae09578fdcedd0d3abf1286c776e7a4bfd02b93f34e9121bf66f17a77a4db36ba04032a553892d1c3f6c377be4f7696357bebaa983ea3166592c8afc35282ae55aba"
Dim mystring6 As String = "64d83f7436aa0b4b58670855a28f2e14e67b2e84411e39b7fe19cd68be5ca411b9f60a9f0339e19ae8bda50223fb4930df0b8a20ed4528a601296a814721245db392b6afa929f1d547c3"
' Log("mystring length = " & mystring.Length)
'First Block - 128 bytes
Dim mybyte(128) As Byte = nativeMe.RunMethod("hexStringToByteArray", Array(mystring1))
Sleep(0)
' For i = 0 To mybyte.Length - 1
' Log("b(" & i & ") = " & mybyte(i))
' Next
Sleep(100)
Dim newbyte(128) As Byte
For i = 0 To 127
newbyte(i) = mybyte(i)
Next
Dim returnedstring As String
returnedstring = nativeMe.RunMethod("tryBollie1", Array(newbyte))
Log("returnedstring 1 = " & returnedstring)
Dim MyBase64String As String = returnedstring
Dim MyBase64StringAsBytes() As Byte = su.DecodeBase64(MyBase64String)
Dim s As String = BytesToString(MyBase64StringAsBytes, 0, MyBase64StringAsBytes.Length, "UTF8")
Log( "Decoded block 1: """ & s & """")
Log(" ")
'Second Block - 128 bytes
Dim mybyte(128) As Byte = nativeMe.RunMethod("hexStringToByteArray", Array(mystring2))
Sleep(0)
' For i = 0 To mybyte.Length - 1
' Log("b(" & i & ") = " & mybyte(i))
' Next
Sleep(100)
Dim newbyte(128) As Byte
For i = 0 To 127
newbyte(i) = mybyte(i)
Next
Dim returnedstring As String
returnedstring = nativeMe.RunMethod("tryBollie1", Array(newbyte))
Log("returnedstring 2 = " & returnedstring)
Dim MyBase64String As String = returnedstring
Dim MyBase64StringAsBytes() As Byte = su.DecodeBase64(MyBase64String)
Dim s As String = BytesToString(MyBase64StringAsBytes, 0, MyBase64StringAsBytes.Length, "UTF8")
Log( "Decoded block 2: """ & s & """")
Log(" ")
'Third Block - 128 bytes
Dim mybyte(128) As Byte = nativeMe.RunMethod("hexStringToByteArray", Array(mystring3))
Sleep(0)
' For i = 0 To mybyte.Length - 1
' Log("b(" & i & ") = " & mybyte(i))
' Next
Sleep(100)
Dim newbyte(128) As Byte
For i = 0 To 127
newbyte(i) = mybyte(i)
Next
Dim returnedstring As String
returnedstring = nativeMe.RunMethod("tryBollie1", Array(newbyte))
Log("returnedstring 3 = " & returnedstring)
Dim MyBase64String As String = returnedstring
Dim MyBase64StringAsBytes() As Byte = su.DecodeBase64(MyBase64String)
Dim s As String = BytesToString(MyBase64StringAsBytes, 0, MyBase64StringAsBytes.Length, "UTF8")
Log( "Decoded block 3: """ & s & """")
Log(" ")
'Fourth Block - 128 bytes
Dim mybyte(128) As Byte = nativeMe.RunMethod("hexStringToByteArray", Array(mystring4))
Sleep(0)
' For i = 0 To mybyte.Length - 1
' Log("b(" & i & ") = " & mybyte(i))
' Next
Sleep(100)
Dim newbyte(128) As Byte
For i = 0 To 127
newbyte(i) = mybyte(i)
Next
Dim returnedstring As String
returnedstring = nativeMe.RunMethod("tryBollie1", Array(newbyte))
Log("returnedstring 4 = " & returnedstring)
Dim MyBase64String As String = returnedstring
Dim MyBase64StringAsBytes() As Byte = su.DecodeBase64(MyBase64String)
Dim s As String = BytesToString(MyBase64StringAsBytes, 0, MyBase64StringAsBytes.Length, "UTF8")
Log( "Decoded block 4: """ & s & """")
Log(" ")
'Fifth Block - 128 bytes
Dim mybyte(128) As Byte = nativeMe.RunMethod("hexStringToByteArray", Array(mystring5))
Sleep(0)
' For i = 0 To mybyte.Length - 1
' Log("b(" & i & ") = " & mybyte(i))
' Next
Sleep(100)
Dim newbyte(128) As Byte
For i = 0 To 127
newbyte(i) = mybyte(i)
Next
Dim returnedstring As String
returnedstring = nativeMe.RunMethod("tryBollie1", Array(newbyte))
Log("returnedstring 5 = " & returnedstring)
Dim MyBase64String As String = returnedstring
Dim MyBase64StringAsBytes() As Byte = su.DecodeBase64(MyBase64String)
Dim s As String = BytesToString(MyBase64StringAsBytes, 0, MyBase64StringAsBytes.Length, "UTF8")
Log( "Decoded block 5: """ & s & """")
Log(" ")
'Sixth Block - 74 bytes
Dim mybyte(74) As Byte = nativeMe.RunMethod("hexStringToByteArray", Array(mystring6))
Sleep(0)
' For i = 0 To mybyte.Length - 1
' Log("b(" & i & ") = " & mybyte(i))
' Next
Sleep(100)
Dim newbyte(74) As Byte
For i = 0 To 73
newbyte(i) = mybyte(i)
Next
Dim returnedstring As String
returnedstring = nativeMe.RunMethod("tryBollie2", Array(newbyte))
Log("returnedstring 6 = " & returnedstring)
Dim MyBase64String As String = returnedstring
Dim MyBase64StringAsBytes() As Byte = su.DecodeBase64(MyBase64String)
Dim s As String = BytesToString(MyBase64StringAsBytes, 0, MyBase64StringAsBytes.Length, "UTF8")
Log( "Decoded block 6: """ & s & """")
Log(" ")
' Dim mybitmap As Bitmap = Base64Con.DecodeToImage(returnedstring)
' ImageView1.Bitmap = mybitmap
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
#If Java
import java.io.StringReader;
import java.math.BigInteger;
import java.security.interfaces.RSAPublicKey;
import org.bouncycastle.openssl.PEMReader;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import java.util.Base64;
public byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
//Unsigned integer types have no direct equivalent in Java:
public byte[] GetPaddedValue(BigInteger value, int length) {
//Unsigned integer types have no direct equivalent in Java:
byte[] result = value.toByteArray();
if (result[0] == 0) {
byte[] tmp = new byte[result.length - 1];
System.arraycopy(result, 1, tmp, 0, tmp.length);
result = tmp;
}
//BA.Log("VALUE = " + value);
String valLength = value.toString();
//BA.Log("VALUE LENGTH = " + valLength.length());
//Unsigned integer types have no direct equivalent in Java:
byte[] padded = new byte[length];
//BA.Log("PADDED SIZE = " + padded.length);
//BA.Log("RESULT SIZE = " + result.length);
//BA.Log("BEFORE COPY");
System.arraycopy(result, 0, padded, (length - result.length), result.length);
//BA.Log("AFTER COPY");
return padded;
}
//Unsigned integer types have no direct equivalent in Java:
public byte[] EncryptValue(byte[] rgb, BigInteger e, BigInteger n, int size) {
//BA.Log("IN ENCRYPTED");
BigInteger input = new BigInteger(rgb);
BigInteger output = input.modPow(e, n);
//Unsigned integer types have no direct equivalent in Java:
byte[] result = GetPaddedValue(output, size);
return result;
}
public String tryBollie1(byte[] mybyte) {
String longKey = "-----BEGIN RSA PUBLIC KEY-----" + "\r\n" +
"MIGWAoGBAMqfGO9sPz+kxaRh/qVKsZQGul7NdG1gonSS3KPXTjtcHTFfexA4MkGA" + "\r\n" +
"mwKeu9XeTRFgMMxX99WmyaFvNzuxSlCFI/foCkx0TZCFZjpKFHLXryxWrkG1Bl9+" + "\r\n" +
"+gKTvTJ4rWk1RvnxYhm3n/Rxo2NoJM/822Oo7YBZ5rmk8NuJU4HLAhAYcJLaZFTO" + "\r\n" +
"sYU+aRX4RmoF" + "\r\n" +
"-----END RSA PUBLIC KEY-----";
//BA.Log("lonkKey = " + longKey);
String base64 = null;
try {
StringReader reader = new StringReader(longKey);
//BA.Log("LongKey = " + reader);
PEMReader pem = new PEMReader(reader);
//BA.Log("1");
// RSAKeyParameters o = (RSAKeyParameters)pem.readObject();
RSAPublicKey pubKey = (RSAPublicKey) new PEMReader(reader).readObject();
RSAKeyParameters o = new RSAKeyParameters(false, pubKey.getModulus(), pubKey.getPublicExponent());
//BA.Log("2");
//Unsigned integer types have no direct equivalent in Java:
byte[] data = mybyte;
//BA.Log("mybyte size = " + mybyte.length);
//BA.Log("data size = " + data.length);
//Unsigned integer types have no direct equivalent in Java:
byte[] decrypted = EncryptValue(data, o.getExponent(), o.getModulus(), 128);
//BA.Log("3");
base64 = Base64.getEncoder().encodeToString(decrypted);
//BA.Log("As base64: " + base64);
} catch (Exception e) {
//BA.Log("RSA decryption error : " + e);
}
return base64;
}
public String tryBollie2(byte[] mybyte) {
String longKey = "-----BEGIN RSA PUBLIC KEY-----" + "\r\n" +
"MF8CSwC0BKDfEdHKz/GhoEjU1XP5U6YsWD10klknVhpteh4rFAQlJq9wtVBUc5Dq" + "\r\n" +
"bsdI0w/bga20kODDahmGtASy9fae9dobZj5ZUJEw5wIQMJz+2XGf4qXiDJu0R2U4" + "\r\n" +
"Kw==" + "\r\n" +
"-----END RSA PUBLIC KEY-----";
//BA.Log("lonkKey = " + longKey);
String base64 = null;
try {
StringReader reader = new StringReader(longKey);
//BA.Log("LongKey = " + reader);
PEMReader pem = new PEMReader(reader);
//BA.Log("1");
// RSAKeyParameters o = (RSAKeyParameters)pem.readObject();
RSAPublicKey pubKey = (RSAPublicKey) new PEMReader(reader).readObject();
RSAKeyParameters o = new RSAKeyParameters(false, pubKey.getModulus(), pubKey.getPublicExponent());
//BA.Log("2");
//Unsigned integer types have no direct equivalent in Java:
byte[] data = mybyte;
//BA.Log("mybyte size = " + mybyte.length);
//BA.Log("data size = " + data.length);
//Unsigned integer types have no direct equivalent in Java:
byte[] decrypted = EncryptValue(data, o.getExponent(), o.getModulus(), 128);
//BA.Log("3");
base64 = Base64.getEncoder().encodeToString(decrypted);
//BA.Log("As base64: " + base64);
} catch (Exception e) {
//BA.Log("RSA decryption error : " + e);
}
return base64;
}
#End If
Sub wv1_PageFinished (Url As String)
' Now that the web page has loaded we can get the page content as a String
Sleep(0)
' see the documentation http://www.b4x.com/forum/additional-libraries-classes-official-updates/12453-webviewextras.html#post70053 for details of the second parameter callUIThread
' Dim Javascript As String
' Javascript="B4A.CallSub('ProcessHTML', false, document.documentElement.outerHTML)"
' Log("PageFinished: "&Javascript)
' WebViewExtras1.executeJavascript(wv1, Javascript)
End Sub