Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Cryptography - Gideros Forum

Cryptography

turker2000turker2000 Member
edited November 2017 in General questions
Hello to everyone,
I have a problem with Cryptography.md5 that I do not understand.
When I use this function, the returning result (16 byte string) is out of specification. How do I get the standard end result?

For example:
Print(Cryptography.md5("a")) -----> �u�����1�iw&a
or
dataSaver.saveValue("|D|somevalue", Cryptography.md5("a")) -----> {"data":{"|D|somevalue":"\fÁu¹Àñ¶¨1Ùâiw&a"}}

The actual result should be 0cc175b9c0f1b6a831c399e269772661
How can I get this result?

Where am I making mistakes? Can you help with the code sample?

Note:
I am also having the same problem between Gideros Cryptography.aesEncrypt and .NET System.Security.Cryptography. The key and IV are the same as the result of AES128 which is not the same.

Comments

  • hgy29hgy29 Maintainer
    Hi @turker2000,

    Cryptography.md5() returns the 16 bytes hash in binary format, that is a string of 16 bytes, while you expect it to be hex-string encoded (32 characters).

    Try this:
    print(("%02x"):rep(16):format(Cryptography.md5("a"):byte(1,16)))

    Likes: turker2000, antix

    +1 -1 (+2 / -0 ) Share on Facebook
  • Thank you very much for your help. Could you give an idea for AES too? The results do not hold the same. Here's an example code I use for .NET to give you an idea.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
     
    using System.Security.Cryptography;
     
    namespace cso2_AES_sifreleme
    {
        class AESSinif
        {
            private const string AES_IV = <a href="/profile/0123456789abcdef">@0123456789abcdef</a>;
            private string aes_anahtar = <a href="/profile/1234567890123456">@1234567890123456</a>;
     
     
            public string AESsifrele(string metin)
            {
                AesCryptoServiceProvider aes_saglayici = new AesCryptoServiceProvider();
                /*
                Şifreleme yöntemi olarak AES şifreleme yöntemini seçiyoruz.
                 */
     
                aes_saglayici.BlockSize = 128;
                /*
                AES bloklar halinde şifreleme yapar. 
                Biz de bloklama yöntemini belirliyoruz.
                 */
     
                aes_saglayici.KeySize = 128;
                /*
                AES şifreleme metodunda anahtar ile şifreleme yapılıyor.
                Anahtar boyutları 128, 192 ve 256 olabilir.
                 */
     
                aes_saglayici.IV = Encoding.UTF8.GetBytes(AES_IV);
                //IV = Initial Vector
                aes_saglayici.Key = Encoding.UTF8.GetBytes(aes_anahtar);
                aes_saglayici.Mode = CipherMode.CBC;
                aes_saglayici.Padding = PaddingMode.PKCS7;
     
                byte[] kaynak = Encoding.Unicode.GetBytes(metin);
                /*
                 Metni byte dizisine çeviriyoruz.
                 */
                using (ICryptoTransform sifrele = aes_saglayici.CreateEncryptor())
                {
                    byte[] hedef = sifrele.TransformFinalBlock(kaynak, 0, kaynak.Length);
                    return Convert.ToBase64String(hedef);
                }
            }
     
     
            public string AESsifreCoz(string sifreliMetin)
            {
                AesCryptoServiceProvider aes_saglayici = new AesCryptoServiceProvider();
                aes_saglayici.BlockSize = 128;
                aes_saglayici.KeySize = 128;
                aes_saglayici.IV = Encoding.UTF8.GetBytes(AES_IV);
                aes_saglayici.Key = Encoding.UTF8.GetBytes(aes_anahtar);
                aes_saglayici.Mode = CipherMode.CBC;
                aes_saglayici.Padding = PaddingMode.PKCS7;
     
                byte[] kaynak = System.Convert.FromBase64String(sifreliMetin);
                //byte[] kaynak = Encoding.UTF8.GetBytes(sifreliMetin);
     
                using (ICryptoTransform decrypt = aes_saglayici.CreateDecryptor())
                {
                    byte[] hedef = decrypt.TransformFinalBlock(kaynak, 0, kaynak.Length);
                    return Encoding.Unicode.GetString(hedef);
                }
            }
     
        }
    }
    the results are the same with Gideros ---> ÓÙğ±\u0011Ù`qú§\\\u00161»\u0016„
    The result produced with .NET ----> z6rzKFeAyyNZZOQMRCHvnQ==
  • hgy29hgy29 Maintainer
    At first sight you didn't encode the result to base64 in gideros while do it in .NET
  • @JoanWile, please don't post comments when all you seem to be doing is wanting people to buy reviews on your site. This forum is for Gideros users, not people selling junk reviews, thanks :)

    Likes: SinisterSoft

    +1 -1 (+1 / -0 ) Share on Facebook
  • YanYan Member
    hgy29 said:

    Hi @turker2000,

    Cryptography.md5() returns the 16 bytes hash in binary format, that is a string of 16 bytes, while you expect it to be hex-string encoded (32 characters).

    Try this:

    print(("%02x"):rep(16):format(Cryptography.md5("a"):byte(1,16)))
    Hi, can you explain this part please ?
    vk.com/yan_alex
  • hgy29hgy29 Maintainer
    Yan said:

    hgy29 said:

    print(("%02x"):rep(16):format(Cryptography.md5("a"):byte(1,16)))
    Hi, can you explain this part please ?
    You know that strings in lua are just a sequence of bytes, not specifically characters. Md5 uses this to return its 16 bytes hash as a 16 bytes string. OP wanted to show it in hexadecimal, so the code posted above does that:
    It takes the format pattern "%02x" (which format a byte as a two hex characters string), repeats it 16 times, feeds it to string.format() along with actual bytes to print from the md5 result.
  • YanYan Member
    edited March 7
    hgy29 said:

    Yan said:

    hgy29 said:

    print(("%02x"):rep(16):format(Cryptography.md5("a"):byte(1,16)))
    Hi, can you explain this part please ?
    You know that strings in lua are just a sequence of bytes, not specifically characters. Md5 uses this to return its 16 bytes hash as a 16 bytes string. OP wanted to show it in hexadecimal, so the code posted above does that:
    It takes the format pattern "%02x" (which format a byte as a two hex characters string), repeats it 16 times, feeds it to string.format() along with actual bytes to print from the md5 result.
    still didnt understand in the string.format function is 1st parameter should be - the string defining the format of the output, here is a byte string, and how this actually acceptable - ("%02x%02x%02x... .... ...."):format(...)

    the string is represented in lua as table ?
    vk.com/yan_alex
  • hgy29hgy29 Maintainer
    edited March 7
    Ah, because each lua string is considered to be of class 'string' when used with the ':' operator, so ("stuff"):format() is the same as string.format("stuff")

    Likes: pie, antix

    +1 -1 (+2 / -0 ) Share on Facebook
  • YanYan Member
    hgy29 said:

    Ah, because each lua string is considered to be of class 'table' when used with the ':' operator, so ("stuff"):format() is the same as string.format("stuff")

    thanks, complitely clear as usually !
    vk.com/yan_alex
Sign In or Register to comment.