Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to use Instead of Base64, use Base64InputStream which provides decoding string (unlimited size) in kotlin

New member
Joined
Feb 21, 2023
Messages
2
`val obj = JSONObject(jwt) val data = obj.getString("data")

Code:
`val obj = JSONObject(jwt) val data = obj.getString("data")       
 val encryptedStr = data.split(".")
 val encryptedStrSplit = encryptedStr[1]

 // decrypt using Base64.decode
 val decodeResponse = android.util.Base64.decode(encryptedStrSplit, android.util.Base64.URL_SAFE).toString(Charsets.UTF_8)
 Timber.tag("OK").d("decodeResponse: $decodeResponse")

 return decodeResponse`

I tried using this method but didn't work

`fun decodeBase64String(encodedString: String): String { val encodedStream: InputStream = ByteArrayInputStream(encodedString.toByteArray(Charsets.UTF_8)) val base64Stream = Base64InputStream(encodedStream)

Code:
val decodedBytes = base64Stream.readBytes()
return decodedBytes.toString(Charsets.UTF_8)
}'
 
New member
Joined
Feb 14, 2023
Messages
7
Code:
import java.io.ByteArrayInputStream
import java.io.Base64
import java.io.Base64InputStream

fun main() {
    val encodedString = "SGVsbG8gV29ybGQh"
    
    // Convert the encoded string to a byte array
    val decodedBytes = Base64.getDecoder().decode(encodedString)
    
    // Create a ByteArrayInputStream from the decoded byte array
    val byteArrayInputStream = ByteArrayInputStream(decodedBytes)
    
    // Create a Base64InputStream from the ByteArrayInputStream
    val base64InputStream = Base64InputStream(byteArrayInputStream, Base64.DEFAULT)
    
    // Read the decoded data from the stream
    val decodedString = base64InputStream.bufferedReader().readText()
    
    // Print the decoded string
    println(decodedString) // Output: "Hello World!"
}
 
Top