B4J Question Alice library error while HMAC_SHA_512

Inrenbang

Member
Licensed User
Trying library from https://www.b4x.com/android/forum/threads/alice-aes-encryption.108364/
B4X:
Public Sub decryptThis(data() As Byte,pwd As String) As String
    Dim bld As AliceContextBuilder
    Dim ac As AliceConstants
    bld.Initialize("")
    bld.Algorithm = ac.AES
    bld.Mode = ac.CTR
    bld.MacAlgorithm = ac.HMAC_SHA_512
    alice.Initialize("",bld.build)
    
    Dim result() As Byte = alice.decryptBytes(data,pwd)
    Return BytesToString(result,0,result.Length,"UTF8")

End Sub
resulting error like this
B4X:
Error occurred on line: 13 (Decrypt)
java.lang.IllegalArgumentException: No enum constant com.rockaport.alice.AliceContext.MacAlgorithm.HmacSHA512
    at java.base/java.lang.Enum.valueOf(Enum.java:240)
    at com.rockaport.alice.AliceContext$MacAlgorithm.valueOf(AliceContext.java:280)
    at de.donmanfred.AliceContextBuilderwrapper.setMacAlgorithm(AliceContextBuilderwrapper.java:51)
    at com.bkn.smartgovadv.decrypt._dekrip(decrypt.java:57)
    at com.bkn.smartgovadv.ws_ujiantest._getsoal(ws_ujiantest.java:243)
    at com.bkn.smartgovadv.ws_ujiantest._websocket_connected(ws_ujiantest.java:447)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:204)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
    at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:121)
    at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:180)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at com.bkn.smartgovadv.main.main(main.java:29)
what do i miss?
 

drgottjr

Expert
Licensed User
Longtime User
these are the defaults for alicecontextbuilder:
Java:
    private AliceContext.Algorithm algorithm = AliceContext.Algorithm.AES;
    private AliceContext.Mode mode = AliceContext.Mode.CTR;
    private AliceContext.Padding padding = AliceContext.Padding.NO_PADDING;
    private AliceContext.KeyLength keyLength = AliceContext.KeyLength.BITS_256;
    private AliceContext.Pbkdf pbkdf = AliceContext.Pbkdf.PBKDF_2_WITH_HMAC_SHA_512;
    private AliceContext.MacAlgorithm macAlgorithm = AliceContext.MacAlgorithm.HMAC_SHA_512;
    private int ivLength = 16;
    private AliceContext.GcmTagLength gcmTagLength = AliceContext.GcmTagLength.BITS_128;
    private int iterations = 10000;

bld.MacAlgorithm = ac.HMAC_SHA_512 is the default.
just remove that line from your code and see how far you get.
why you get that error, I can't say. don't give the compiler a reason
to complain...

i think you're missing a file (maybe alice-0.5.jar). that's where
all the enums are, but i don't see how it's included in any
of the examples i've looked at. HMAC_SHA_512 is
definitely suppported and also appears in the original project.
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
B4X:
Public Sub test
    Dim BC As ByteConverter
    
    Dim Alice As Alice
'    Dim AiceEnum As AliceConstants

    Dim Builder As AliceContextBuilder
    Builder.Initialize("")
    Builder.Algorithm = "AES"
    Builder.Mode = "CTR"
    Builder.MacAlgorithm = "HMAC_SHA_512"
    Alice.Initialize("", Builder.build)
    
    Dim Data As String = "Hello"
    Dim Password As String = "FGERHZXCG434TRWR"

    Dim Result() As Byte = Alice.encryptBytes(Data.GetBytes("UTF8"), BC.ToChars(Password))
    Log(BytesToString(Result, 0, Result.Length, "UTF-8"))
    
    Dim Result() As Byte = Alice.decryptBytes(Result, Password)
    Log(BytesToString(Result, 0, Result.Length, "UTF-8"))
End Sub
1688944371047.png
 
Upvote 0

Inrenbang

Member
Licensed User
?
B4X:
Public Sub test
    Dim BC As ByteConverter
  
    Dim Alice As Alice
'    Dim AiceEnum As AliceConstants

    Dim Builder As AliceContextBuilder
    Builder.Initialize("")
    Builder.Algorithm = "AES"
    Builder.Mode = "CTR"
    Builder.MacAlgorithm = "HMAC_SHA_512"
    Alice.Initialize("", Builder.build)
  
    Dim Data As String = "Hello"
    Dim Password As String = "FGERHZXCG434TRWR"

    Dim Result() As Byte = Alice.encryptBytes(Data.GetBytes("UTF8"), BC.ToChars(Password))
    Log(BytesToString(Result, 0, Result.Length, "UTF-8"))
  
    Dim Result() As Byte = Alice.decryptBytes(Result, Password)
    Log(BytesToString(Result, 0, Result.Length, "UTF-8"))
End Sub
View attachment 143594
Thank you, this code remove the java.lang.IllegalArgumentException: No enum constant com.rockaport.alice.AliceContext.MacAlgorithm.HmacSHA512 error, but i think the mac algo is still invalid, fyi the encrypted data is AES-256-CTR cipher and HMAC-Based Key Derivation Function (HKDF), HMAC digest using SHA512
B4X:
Error occurred on line: 18 (Decrypt)
java.security.GeneralSecurityException: Received mac is different from calculated
    at com.rockaport.alice.Alice.decrypt(Alice.java:375)
    at de.donmanfred.Alicewrapper.decryptBytes(Alicewrapper.java:53)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:64)
    at com.bkn.smartgovadv.decrypt._dekrip(decrypt.java:35)
    at com.bkn.smartgovadv.ws_ujiantest._getsoal(ws_ujiantest.java:244)
    at com.bkn.smartgovadv.ws_ujiantest._websocket_connected(ws_ujiantest.java:448)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
(null string)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:204)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)

i am not sure is alice support hkdf?
 
Upvote 0

Inrenbang

Member
Licensed User
Sample project attached, below is how i generate encrypted data using PHP CodeIgniter 4
Encryption.php configuration:
<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

/**
 * Encryption configuration.
 *
 * These are the settings used for encryption, if you don't pass a parameter
 * array to the encrypter for creation/initialization.
 */
class Encryption extends BaseConfig
{
    /**
     * --------------------------------------------------------------------------
     * Encryption Key Starter
     * --------------------------------------------------------------------------
     *
     * If you use the Encryption class you must set an encryption key (seed).
     * You need to ensure it is long enough for the cipher and mode you plan to use.
     * See the user guide for more info.
     *
     * @var string
     */
    public $key = '123456789';

    /**
     * --------------------------------------------------------------------------
     * Encryption Driver to Use
     * --------------------------------------------------------------------------
     *
     * One of the supported encryption drivers.
     *
     * Available drivers:
     * - OpenSSL
     * - Sodium
     *
     * @var string
     */
    public $driver = 'OpenSSL';

    /**
     * --------------------------------------------------------------------------
     * SodiumHandler's Padding Length in Bytes
     * --------------------------------------------------------------------------
     *
     * This is the number of bytes that will be padded to the plaintext message
     * before it is encrypted. This value should be greater than zero.
     *
     * See the user guide for more information on padding.
     *
     * @var int
     */
    public $blockSize = 16;

    /**
     * --------------------------------------------------------------------------
     * Encryption digest
     * --------------------------------------------------------------------------
     *
     * HMAC digest to use, e.g. 'SHA512' or 'SHA256'. Default value is 'SHA512'.
     *
     * @var string
     */
    public $digest = 'SHA512';
}

and this is how i execute encryption

PHP:
$string = "hello";
$encrypter = \Config\Services::encrypter();
$encrypted = $encrypter->encrypt($string, ['key' => '123456789']);
echo base64_encode($encrypted);
 

Attachments

  • SampleProject.zip
    1.1 KB · Views: 62
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Thank you, this code remove the java.lang.IllegalArgumentException: No enum constant com.rockaport.alice.AliceContext.MacAlgorithm.HmacSHA512 error, but i think the mac algo is still invalid, fyi the encrypted data is AES-256-CTR cipher and HMAC-Based Key Derivation Function (HKDF), HMAC digest using SHA512
B4X:
Error occurred on line: 18 (Decrypt)
java.security.GeneralSecurityException: Received mac is different from calculated
    at com.rockaport.alice.Alice.decrypt(Alice.java:375)
    at de.donmanfred.Alicewrapper.decryptBytes(Alicewrapper.java:53)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:64)
    at com.bkn.smartgovadv.decrypt._dekrip(decrypt.java:35)
    at com.bkn.smartgovadv.ws_ujiantest._getsoal(ws_ujiantest.java:244)
    at com.bkn.smartgovadv.ws_ujiantest._websocket_connected(ws_ujiantest.java:448)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
(null string)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:98)
    at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:204)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)

i am not sure is alice support hkdf?
i am not sure is alice support hkdf?
No
Password Based Key Derivation Functions (PBKDF)

Note:
see processes in \system\Encryption\Handlers\OpenSSLHandler.php

Tips.
You can configure codeigniter encryption to be compatible with other

 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
The problem is in the generation of hash_hkdf.

I have only seen this solution in java (jar) for B4J

or

PHP (SHA512)
1688982844160.png
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
You can post the key that is generated to $encryptKey in PHP

PHP:
      // derive a secret key
        $encryptKey = \hash_hkdf($this->digest, $this->key, 0, $this->encryptKeyInfo);

        return \openssl_decrypt($data, $this->cipher, $encryptKey, OPENSSL_RAW_DATA, $iv);
 
Upvote 0

Inrenbang

Member
Licensed User
Above solution didn't work, thank you all, i think this is the dead end, i'll try another solution, or maybe decrypt and reencrypt using compatible encryption.
 
Upvote 0
Top