Android Question B4X encryption audio stream

Philip Prins

Active Member
Licensed User
Longtime User
Hello I want to encrypt and decrypt an audiostream:

B4X:
Sub Encrypt(CLR() As Byte) As Byte()
    Dim c As B4XCipher
    Dim bc As ByteConverter
    Dim S As String = bc.HexFromBytes(CLR)
    
    Log(S)
    Dim Enc() As Byte = c.Encrypt(S.GetBytes("UTF8"),Starter.EncryptionPassword)
    Log(Enc.Length)
    Return Enc
End Sub

Sub Decrypt(EncryptedData() As Byte) As Byte()
    Dim c As B4XCipher
    Dim bc As ByteConverter
    Dim S As String = bc.HexFromBytes(EncryptedData)
    
    Dim Dec() As Byte = c.Decrypt(S.GetBytes("UTF8"),Starter.EncryptionPassword)
    Return Dec
End Sub

When decrypting i get the following error:
javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_internal:WRONG_FINAL_BLOCK_LENGTH

On the forum i find a lot o f encryption threads, what is the correct way to do this.
 

OliverA

Expert
Licensed User
Longtime User
Your steps in Decrypt are wrong. First, decrypt the incoming byte data w/o converting it to a string. Take the decrypted byte array and turn it into a string. Turn string into byte array with BytesFromHex. Even though that would do it, don't.

Why are you converting the byte array to a string in encrypt just to convert it to a byte array again in order to encrypt it? Just encrypt the byte array (CLR) which will result in the encrypted byte array Enc. In decrypt, just decrypt EncryptedData directly, resulting in the decrypted audio data in Dec.
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
Your steps in Decrypt are wrong. First, decrypt the incoming byte data w/o converting it to a string. Take the decrypted byte array and turn it into a string. Turn string into byte array with BytesFromHex. Even though that would do it, don't.

Why are you converting the byte array to a string in encrypt just to convert it to a byte array again in order to encrypt it? Just encrypt the byte array (CLR) which will result in the encrypted byte array Enc. In decrypt, just decrypt EncryptedData directly, resulting in the decrypted audio data in Dec.
Hello Oliver,

I tried that but i get the same error ,illegal blocksize

B4X:
Sub Encrypt(CLR() As Byte) As Byte()
    'Return CLR
    Dim c As B4XCipher
    'Dim bc As ByteConverter
    'Dim S As String = bc.HexFromBytes(CLR)
    
    'Log(S)
    Dim Enc() As Byte = c.Encrypt(CLR,Starter.EncryptionPassword)
    Log(Enc.Length)
    Return Enc
End Sub

Sub Decrypt(EncryptedData() As Byte) As Byte()
    'Return EncryptedData
    Dim c As B4XCipher
    'Dim bc As ByteConverter
    'Dim S As String = bc.HexFromBytes(EncryptedData)
    
    Dim Dec() As Byte = c.Decrypt(EncryptedData,Starter.EncryptionPassword)
    Return Dec
End Sub

javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_internal:WRONG_FINAL_BLOCK_LENGTH
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Then something may be happening between encrypting the data and decrypting the data. Given an audiostream byte array, does this throw and error message?
B4X:
'replace inputAudio with a audiostream byte array
Dim someAudio() as Byte = Decrypt(Encrypt(inputAudio))
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
you need to use a stream cipher.

when your encrypting a stream with a block cipher, you can never guarantee you have all the bytes there as necessary for the encryption.

if you use a block cipher, you have to packetize your data coming in. Such as waiting for X amount of bytes befor eencryption.
 
Upvote 0
Top