B4J Question Need Help Convert C# code To B4j

behnam_tr

Active Member
Licensed User
Longtime User
Hi everyone
I have a C# code but I am not familiar with this language
I want to convert this code to b4j code
Friends who know, please help

C#:
    private void GenerateToken()
    {
        Random random = new Random();
        string text = random.NextDouble().ToString();
        int[] array = new int[8] { 1382640238, 1684104556, 1701069673, 1885889906, 1299145829, 1298492783, 1920553844, 1919246701 };
        uint[] array2 = new uint[4] { 825438784u, 1935959604u, 892749397u, 1663718965u };
        List<byte> list = new List<byte>();
        List<byte> list2 = new List<byte>();
        for (int i = 0; i < array.Length; i++)
        {
            list.AddRange(BitConverter.GetBytes(array[i]).Reverse());
        }
        for (int j = 0; j < array2.Length; j++)
        {
            list2.AddRange(BitConverter.GetBytes(array2[j]).Reverse());
        }
        byte[] inArray = Encrypter(text, list.ToArray(), list2.ToArray());
        string value = Convert.ToBase64String(inArray);
        
        //["s"] = text;
        //["c"] = value;
    }

    private byte[] Encrypter(string randomst, byte[] array1, byte[] array2)
    {
        if (randomst == null || randomst.Length <= 0)
        {
            throw new ArgumentNullException("plainText");
        }
        if (array1 == null || array1.Length == 0)
        {
            throw new ArgumentNullException("Key");
        }
        if (array2 == null || array2.Length == 0)
        {
            throw new ArgumentNullException("IV");
        }
        using Aes aes = Aes.Create();
        aes.Key = array1;
        aes.IV = array2;
        ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);
        using MemoryStream memoryStream = new MemoryStream();
        using CryptoStream stream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
        using (StreamWriter streamWriter = new StreamWriter(stream))
        {
            streamWriter.Write(randomst);
        }
        return memoryStream.ToArray();
    }
 

JohnJ

Member
Licensed User
Longtime User
Hi everyone
I have a C# code but I am not familiar with this language
I want to convert this code to b4j code
Friends who know, please help

C#:
    private void GenerateToken()
    {
        Random random = new Random();
        string text = random.NextDouble().ToString();
        int[] array = new int[8] { 1382640238, 1684104556, 1701069673, 1885889906, 1299145829, 1298492783, 1920553844, 1919246701 };
        uint[] array2 = new uint[4] { 825438784u, 1935959604u, 892749397u, 1663718965u };
        List<byte> list = new List<byte>();
        List<byte> list2 = new List<byte>();
        for (int i = 0; i < array.Length; i++)
        {
            list.AddRange(BitConverter.GetBytes(array[i]).Reverse());
        }
        for (int j = 0; j < array2.Length; j++)
        {
            list2.AddRange(BitConverter.GetBytes(array2[j]).Reverse());
        }
        byte[] inArray = Encrypter(text, list.ToArray(), list2.ToArray());
        string value = Convert.ToBase64String(inArray);
       
        //["s"] = text;
        //["c"] = value;
    }

    private byte[] Encrypter(string randomst, byte[] array1, byte[] array2)
    {
        if (randomst == null || randomst.Length <= 0)
        {
            throw new ArgumentNullException("plainText");
        }
        if (array1 == null || array1.Length == 0)
        {
            throw new ArgumentNullException("Key");
        }
        if (array2 == null || array2.Length == 0)
        {
            throw new ArgumentNullException("IV");
        }
        using Aes aes = Aes.Create();
        aes.Key = array1;
        aes.IV = array2;
        ICryptoTransform transform = aes.CreateEncryptor(aes.Key, aes.IV);
        using MemoryStream memoryStream = new MemoryStream();
        using CryptoStream stream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
        using (StreamWriter streamWriter = new StreamWriter(stream))
        {
            streamWriter.Write(randomst);
        }
        return memoryStream.ToArray();
    }
Try running it through one of the AIs
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
optional
etc..
Search
 
Upvote 0

Mariano Ismael Castro

Active Member
Licensed User
Hello, I give you a small preview, you continue with the rest, I hope it can help you. The first function seems to be ready but I don't know.
Good luck


Functions:
'libs = ByteConverter
Public Sub GenerateToken
    Dim text As String = Rnd(1, 1000000000)
    Dim Array1 As List = Array As Int(1382640238, 1684104556, 1701069673, 1885889906, 1299145829, 1298492783, 1920553844, 1919246701)
    Dim Array2 As List = Array As Int(825438784, 1935959604, 892749397, 1663718965) 'uint don'n exist in b4x

    Dim list, list2 As List
    list.Initialize
    list2.Initialize
    
    Dim b As ByteConverter
    Dim java As JavaObject
    For i = 0 To Array1.Size - 1
        Dim bytes As List = b.IntsToBytes(Array As Int(Array1.Get(i)))
        java.InitializeStatic("java.util.Collections").RunMethod("reverse", Array(bytes))
        list.AddAll(Array(bytes))
    Next
    
    For j = 0 To Array2.Size - 1
        Dim bytes2 As List = b.IntsToBytes(Array As Int(Array2.Get(j)))
'        Log(bytes2)
        java.InitializeStatic("java.util.Collections").RunMethod("reverse", Array(bytes2))
'        Log(bytes2)
        list2.AddAll(Array(bytes))
    Next
    Log(list2)
    
    Dim inArray() As Byte
    inArray = Encrypter(text, list, list2)
    Dim value As String = BytesToString(inArray, 0, inArray.Length, "UTF8")
    Log(value)
    '["s"] = text
    '["c"] = value
End Sub


Private Sub Encrypter(randomst As String, array1() As Byte, array2() As Byte) As Byte()
    If randomst = Null Or randomst.Length <= 0 Then
        Log("plainText")
        Return Null
    End If
    If array1 = Null Or array1.Length = 0 Then
        Log("Key")
        Return Null
    End If
    If array2 = Null Or array2.Length = 0 Then
        Log("IV")
        Return Null
    End If
    
'    Dim aes As AES
'    aes.Initialize("AES")
'    aes.Key = array1
'    aes.IV = array2
    
'    Dim transform As B4XCipher
'    transform.Initialize("AES", aes.Key, aes.IV, False)
    
    Dim memoryStream As OutputStream
    memoryStream.InitializeToBytesArray(0)
    Dim stream As OutputStream
    stream.InitializeToBytesArray(0)
    
    Dim array4() As Byte = memoryStream.ToBytesArray
    stream.WriteBytes(array4, 0, array4.Length)
    
    
    Dim streamWriter As TextWriter
    streamWriter.Initialize(stream)
    streamWriter.Write(randomst)
    streamWriter.Close
    stream.Close
    
    Return memoryStream.ToBytesArray
End Sub
 
Upvote 0
Top