0

I have an encryption key and i want to convert this given string into a byte array, in order to do file decryption process.

Exemple :

val string = "0123456789012345678901234567890123456789012345678901234567890123"
// what i want to have is something like this :
val result = byteArrayOf(0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03)
Faz
  • 322
  • 2
  • 14
  • I see no logic in your code. I thought you would write `val result= byteArrayOf("0123456789012345678901234567890123456789012345678901234567890123")`. – blackapps Apr 07 '20 at 09:34
  • i receive encryption key as string and i have to convert it into byteArrays for decryption operation ... but when i say convert i mean we must have same values from given string in byteArray , as you can see in my example ... – Faz Apr 07 '20 at 09:37
  • So what you want to know is how to implement a function byteArrayOf like `val byteArray= byteArrayOf("0123456789012345678901234567890123456789012345678901234567890123")` Where the bytes of byteArray get the values `0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x00,0x01,0x02,0x03)` – blackapps Apr 07 '20 at 09:44
  • No, I want exactly as i wrote above .... byteArrayOf(0x00.... – Faz Apr 07 '20 at 09:46
  • byteArrayOf takes some bytes, the problem is what i have is a string. so i have to generate this bytes. and apparently they must be in hex format .. – Faz Apr 07 '20 at 09:50
  • Then do not use that function if it already exists and does something you do not need. Write your own function. `val byteArray = getByteArray("01234....")`. – blackapps Apr 07 '20 at 09:53
  • Bytes are not in hex format. They are just eight bits. And you can display the value of the bytes as decimal or hexadecimal. Or octal or as binary. What you want. But a byte is only eight bits. – blackapps Apr 07 '20 at 09:55
  • So if the string only contains digits 0-9 then you could as well say that the byte array should have the values `1,2,3,4,....`. – blackapps Apr 07 '20 at 09:59
  • `val byteArray = string.map { (it - '0').toByte() }.toByteArray()` – IR42 Apr 07 '20 at 09:59
  • @blackapps , the string does not contains only digit numbers. this is just an example – Faz Apr 07 '20 at 10:05
  • @IR42 nice idea , but your solution gives an byteArray with a size of 95 – Faz Apr 07 '20 at 10:07
  • Then give a better example. And you are not very helpfull not telling what it should be if 95 is not correct. – blackapps Apr 07 '20 at 10:13
  • - I use an algorithm of decryption which takes a byteArraysof 32 (AES 256) to generate a SecretKeySpec. When i do manually it works, like this : val b = byteArrayOf(0x00,0x01 ... etc. - My question is how we can do with the given string (it ontains 64 characters) – Faz Apr 07 '20 at 10:28
  • plz forget this 95 , i was wrong – Faz Apr 07 '20 at 11:34
  • The size is 64 , which is always not the result that i'm looking for (=32) – Faz Apr 07 '20 at 11:46

1 Answers1

0

voila :

private fun hexStringToByteArray(s: String): ByteArray? {
    val len = s.length
    val data = ByteArray(len / 2)
    var i = 0
    while (i < len) {
        data[i / 2] = ((Character.digit(s[i], 16) shl 4) +
                Character.digit(s[i + 1], 16)).toByte()
        i += 2
    }
    return data
}

from here https://stackoverflow.com/a/18714790/5015227

Faz
  • 322
  • 2
  • 14