![]() It works in B4J and B4A.| Miscellaneous How to Use the Rijndael Algorithm with a 256-bit Block Size in. $dec = openssl_decrypt($string, $encrypt_method, $pw, 0,$iv) //decrypt. Must be Base64 encoded for OpenSSL decrypt $string=Base64_Encode(substr($total, 48, strlen($total)-48)) //extract the message. $salt=substr($total, 0, 32) //extract the salt $total=base64_decode($string) //salt + iv + message $enc=Base64_encode($Salt.$IV.Base64_decode($enc)) //add salt, iv and the message to a Base64 string (OPENSSL encrypt returns a Base64 string so we have to decode it first) $enc = openssl_encrypt($string, $encrypt_method, $pw, 0,$IV) //encrypt with the given IV $Salt=openssl_random_pseudo_bytes(32, $securityok) //generate a random salt ![]() $IV=openssl_random_pseudo_bytes(16, $securityok) //generate a radom IV Print json_encode ("Error: Function not defined (". 'ok','b4xdecrypted' => $decrypted,'phpencrypted' => $encrypted)) //this is an associative array or a map encrypt it twice with different passwords as both sides need the same password, use RSA to exchange a different aes password for every client/instance/session (SSL does quite the same) mix the data bytes with the salt (odd/even) add another salt before encryption (e.g. on some platforms you need to pad the message on your own (e.g. salt and iv are removed from the message the receiver has to know the SAME password and the position/length of salt and iv Send the data (here a map) to the receiver (here PHP, can be any other platform, too) hash the password to 32 bytes (see the sub) or use random 32 bytes (which can be converted to a Base64 string to handle it better) get the bytes of the string which you want to encrypt (or use a byte array directly if you want to encrypt just bytes) ![]() the methods need a Base64 encoded string (= encrypted bytes) as an input or output the message structure is: salt + iv + message an encrypted message is always a multiple of 16 bytes long (AES = blockcypher). ![]() a "salt" is added, too (here: 32 bytes. the IV is randomized with every message (= some sort of changing the sort order of the data = same data will look different every time) AES256 uses a fixes length password of 32 bytes (= 256 bits), AES128 would use 16 (= 128 bits) getting bytes from strings is easy, too by using StringName.GetBytes("UTF8") like any encryption, AES uses bytes to en-/decrypt I've added some subs to convert "all" types to strings Here these types are just associative arrays (key -> value) here we use maps (could be a list with maps) which are converted to JSON strings and back using strings is very easy but comes unhandy when you need structured data the goal is to send/receive a simple string in the end using the POSTSTRING method Private Sub GenerateSalt (l As Int) As Byte() Private Sub SplitSaltIVMessage (SaltIvMessage () As Byte) As Mapĭim Salt(32),IV (16), Message(SaltIvMessage.length-48) As ByteīC.Arra圜opy(SaltIvMessage,48,Message,0,Message.Length)ĭim m As Map=CreateMap("salt":Salt,"iv":IV,"message":Message) Private Sub AddSaltIVMessage (Salt() As Byte,IV() As Byte, Message () As Byte) As Byte()ĭim SaltIVMessageBytes (Salt.Length+ IV.Length + Message.Length) As ByteīC.Arra圜opy(Salt,0,SaltIVMessageBytes,0,32)īC.Arra圜opy(IV,0,SaltIVMessageBytes,32,16)īC.Arra圜opy(Message,0,SaltIVMessageBytes,48,Message.Length) Public Sub GetMessageSaltIVFromEncryptedMessage(EncryptedMessageAsBytes() As Byte) As Mapĭim m As Map = SplitSaltIVMessage(EncryptedMessageAsBytes) Return BytesToString(datas, 0, datas.Length, "UTF8") Public Sub AES_Decrypt(data() As Byte, passb() As Byte, ReturnString As Boolean) As Objectĭim MessageB() As Byte = m.Get("message")ĭim datas() As Byte = C.Decrypt(MessageB, kg.Key, True) 'Salt (32) and IV(16) is added at the beginning 'Input is a Base664 encoded string or a byte array, output a string or byte array Public Sub AES_Encrypt(data() As Byte, passB() As Byte, ReturnB64String As Boolean) As Objectĭim datas() As Byte = C.Encrypt(data, kg.Key, True)ĭim SaltIVMessage(SaltB.Length + datas.Length + IVb.Length) As Byte = AddSaltIVMessage(SaltB,IVb,datas)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |