Hey, i build a auth method for my app to protect my API and one of this step is to signed requests to the api to ensure that every request comes from my app. RSA is the best method for that, because this is not a hash, so that the same input is ever the same output. The API knows the Private Key, the apps knows the public key.
On B4J you need the "bcprov-jdk15on-161.jar" you can download it here.
Require libs.
B4A:
6h of work... in this time I would have written 5 XUI views ^^
On B4J you need the "bcprov-jdk15on-161.jar" you can download it here.
Require libs.
B4A:
- ByeConverter
- Encryption
- StringUtils
- ByeConverter
- Encryption
- jStringUtils
- iRandomAccessFile
- iRSA
- iStringUtils
B4X:
Sub Button1_Click
Dim msg As String = "Test123!"
Dim privatekey2 As String = "MIICXAIBAAKBgQCbxeEx0hYmBFlz7l3Lh6HeA4YkgCoJgwG2FJ5KCkZ8k21YJslzgLf9NwrjXjqRwN311E5FAH/nXTPLddjnh0mAQiaMgGf/+w1lRbn1gjIYAUMK9FsVuZJ+POu8e6TgRTFTiz1/BLu0trZaH/XqqnnUjsjEXd63VVdepZ62vxfVywIDAQABAoGAQ7qa+n188dSsTDLVB1yWraBcn9w16uLSSKfYVxr2oM29GjnrF1RdKzTWgBuFXcA9AdjomynncuJcVdeMksaI1xCnS0J6ge4fd1dWMqRZoJo7pBUWrqUaaAWCiSOmhf+5e6MnbdKQTR+mmBUSNnmqPMZ4nHBwrcuxzAAzE8rKKFkCQQD4J4YtyFHRdaQ38YZS0N1SXLZ9RNFRZzopHx7kTlhW5AwHXOCKC1Zef/ZlWMdHjENY0Op4eXu6oQhh5jtBtdXdAkEAoLKm9+DNSVa7hX2GkMqO9rKrPo/Cu7qhymBzdFI/rdZM8dv+FBJ8Z9RJTQq6wm5LpB4v0/A7TfTS2umGuuEDxwJAFtUcJW4/CPS4DWWtpEUPeBqLt+7zC3hiA5KXdw42VphY+vxytIDii4NemmiWvWCMecmPKVKULdHPQaK7ZQpkCQJABbfVsSRSqbVoXsukqipLBAQ/i65Z44w0jZr5AL6cfXcOrdyCIfy2aItpQFCNbLW4A1L/qK73rUJ07k04+hVfqQJBAKC8P7K7laB+KQcqcJ0LLWsZWrbFtQq9XoqflKMtG5K9FHfMpblPiqgh9v6a83xr0Di+NyCl4ibiIE8ghXs9wx0="
Dim publickey2 As String = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbxeEx0hYmBFlz7l3Lh6HeA4YkgCoJgwG2FJ5KCkZ8k21YJslzgLf9NwrjXjqRwN311E5FAH/nXTPLddjnh0mAQiaMgGf/+w1lRbn1gjIYAUMK9FsVuZJ+POu8e6TgRTFTiz1/BLu0trZaH/XqqnnUjsjEXd63VVdepZ62vxfVywIDAQAB"
Dim output As String = EncryptRSAWithPublicKey(msg,publickey2)
Log(output)
Log(DecryptRSAWithPrivateKey(output,publickey2))
End Sub
Private Sub EncryptRSAWithPublicKey(Text As String,PublicKey As String) As String
#If B4I
Dim su As StringUtils
Dim rsa As RSA
Return su.EncodeBase64(rsa.EncryptWithPublicKey(Text.GetBytes("UTF8"),PublicKey))
#Else
Dim su As StringUtils
Dim pubkey() As Byte = su.DecodeBase64(PublicKey)
Dim Enc As Cipher
Enc.Initialize("RSA/ECB/PKCS1Padding")
Dim kpg As KeyPairGenerator
kpg.Initialize("RSA", 1024)
kpg.PublicKeyFromBytes(pubkey)
Dim bytes() As Byte = Text.GetBytes("UTF8")
Return su.EncodeBase64(Enc.Encrypt(bytes,kpg.PublicKey,False))
#End If
End Sub
Private Sub DecryptRSAWithPrivateKey(encryptedstring As String,PrivateKey As String) As String
#If B4J
#AdditionalJar: bcprov-jdk15on-161.jar
#if java
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Provider;
import java.security.Security;
static{
Provider BC = new BouncyCastleProvider();
Security.addProvider(BC);
}
#End If
#End if
#If B4A or B4J
Dim su As StringUtils
Dim privkey() As Byte = su.DecodeBase64(PrivateKey)
Dim kpg As KeyPairGenerator
kpg.Initialize("RSA", 1024)
kpg.PrivateKeyFromBytes(privkey)
Dim Enc As Cipher
Enc.Initialize("RSA/ECB/PKCS1Padding")
Dim bc As ByteConverter
Return bc.StringFromBytes(Enc.Decrypt(su.DecodeBase64(encryptedstring),kpg.PrivateKey,False),"UTF8")
#End If
End Sub
6h of work... in this time I would have written 5 XUI views ^^