B4i Library iRSA - RSA encryption / decryption

This is a wrapper of: https://github.com/ideawu/Objective-C-RSA

It is an implementation of RSA asymmetric encryption.

Usage example (based on the library developer blog):

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'Public variables can be accessed from all modules.
   Public App As Application
   Public NavControl As NavigationController
   Private Page1 As Page
   Private rsa As RSA
   Private PrivateKey As String = $"-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMQKGp7zSUktNOQk
PdfcvJ3sWP7O46ENmSO4s0+iDdhHbR7klyt2oj0gXM1sAJj8ZBWFudu8GpiDKqXw
N88IZemfT5c1LEQshTD1WHfZC6EY2vf9MGl5yGtV5WkU8vDJpg0STUJrCAcJ5Cp/
m5qqJqgEM5Op6jHzwtzWV3syx+CBAgMBAAECgYEApSzqPzE3d3uqi+tpXB71oY5J
cfB55PIjLPDrzFX7mlacP6JVKN7dVemVp9OvMTe/UE8LSXRVaFlkLsqXC07FJjhu
wFXHPdnUf5sanLLdnzt3Mc8vMgUamGJl+er0wdzxM1kPTh0Tmq+DSlu5TlopAHd5
IqF3DYiORIen3xIwp0ECQQDj6GFaXWzWAu5oUq6j1msTRV3mRZnx8Amxt1ssYM0+
JLf6QYmpkGFqiQOhHkMgVUwRFqJC8A9EVR1eqabcBXbpAkEA3DQfLVr94vsIWL6+
VrFcPJW9Xk28CNY6Xnvkin815o2Q0JUHIIIod1eVKCiYDUzZAYAsW0gefJ49sJ4Y
iRJN2QJAKuxeQX2s/NWKfz1rRNIiUnvTBoZ/SvCxcrYcxsvoe9bAi7KCMdxObJkn
hNXFQLav39wKbV73ESCSqnx7P58L2QJABmhR2+0A5EDvvj1WpokkqPKmfv7+ELfD
HQq33LvU4q+N3jPn8C85ZDedNHzx57kru1pyb/mKQZANNX10M1DgCQJBAMKn0lEx
QH2GrkjeWgGVpPZkp0YC+ztNjaUMJmY5g0INUlDgqTWFNftxe8ROvt7JtUvlgtKC
XdXQrKaEnpebeUQ=
-----END PRIVATE KEY-----"$
   Private PublicKey As String = $"-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEChqe80lJLTTkJD3X3Lyd7Fj+
zuOhDZkjuLNPog3YR20e5JcrdqI9IFzNbACY/GQVhbnbvBqYgyql8DfPCGXpn0+X
NSxELIUw9Vh32QuhGNr3/TBpechrVeVpFPLwyaYNEk1CawgHCeQqf5uaqiaoBDOT
qeox88Lc1ld7MsfggQIDAQAB
-----END PUBLIC KEY-----"$
End Sub

Private Sub Application_Start (Nav As NavigationController)
   'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.Title = "Page 1"
   Page1.RootPanel.Color = Colors.White
   NavControl.ShowPage(Page1)
   Dim enc() As Byte = rsa.EncryptWithPrivateKey("abc".GetBytes("utf8"), PrivateKey)
   Dim bc As ByteConverter 'iRandomAccessFile
   Log(bc.HexFromBytes(enc))
   Dim dec() As Byte = rsa.DecryptWithPublicKey(enc, PublicKey)
   Log(BytesToString(dec, 0, dec.Length, "UTF8"))
End Sub

If you are running it on the simulator then you need to add this line:
B4X:
#Entitlement: <key>keychain-access-groups</key><array><string>$(AppIdentifierPrefix)</string></array>
 

Attachments

  • iRSA.zip
    49.8 KB · Views: 92

Nestor Castro Jr

Member
Licensed User
Hello Erel,

I tried to use your sample with a Key pairs that I genareted with 3th part software but it does not work. My keys has 1024 bits. Do you know what I'm doing worng?

This is my Keys:

Private PrivateKey As String = $"-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDWoUvEUYyayg3LfgJjCp1dElri0OloQ0eiT9vp900aHnvcT/Et
N4VPHh8arokkoK252BD7f8Z8SSXJdkZ7YHWNsHyKj5WZW0N3qlvswsR1O9v2I8Zz
MX2PnSfQolTLrPLNNZIfCwuu5FhkTjfi6PK2rjnoFRPy3a0iMGxXRPLQTwIDAQAB
AoGAPGWDNql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