Android Question Encrybtion inquirey

Asim A Baki

Active Member
Licensed User
Longtime User
I am using B4xencrybtion library to encrypt a text
then I sent it to a dot net socket server and trying to decrypt it using AES algorithm example found on msdn web site but the dot net found error says that the legnth of byte array is mismatched to the received encrypted bytes .
the encryption code in dot net is attached to the thread
could any one help me please to solve the problem

the dot net code as below



class AES
{
public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;

// Set your salt here, change it to meet your flavor:
byte[] saltBytes = passwordBytes;
// Example:
//saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;

var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);

AES.Mode = CipherMode.CBC;

using (CryptoStream cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}

return encryptedBytes;
}

public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
// Set your salt here to meet your flavor:
byte[] saltBytes = passwordBytes;
// Example:
//saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;

var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);

AES.Mode = CipherMode.CBC;

using (CryptoStream cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}

return decryptedBytes;
}

public static string Encrypt(string text, byte[] passwordBytes)
{
byte[] originalBytes = Encoding.UTF8.GetBytes(text);
byte[] encryptedBytes = null;

// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

// Getting the salt size
int saltSize = GetSaltSize(passwordBytes);
// Generating salt bytes
byte[] saltBytes = GetRandomBytes(saltSize);

// Appending salt bytes to original bytes
byte[] bytesToBeEncrypted = new byte[saltBytes.Length + originalBytes.Length];
for (int i = 0; i < saltBytes.Length; i++)
{
bytesToBeEncrypted = saltBytes;
}
for (int i = 0; i < originalBytes.Length; i++)
{
bytesToBeEncrypted[i + saltBytes.Length] = originalBytes;
}

encryptedBytes = AES_Encrypt(bytesToBeEncrypted, passwordBytes);

return Convert.ToBase64String(encryptedBytes);
}

public static string Decrypt(string decryptedText, byte[] passwordBytes)
{
byte[] bytesToBeDecrypted = Convert.FromBase64String(decryptedText);

// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

byte[] decryptedBytes = AES_Decrypt(bytesToBeDecrypted, passwordBytes);

// Getting the size of salt
int saltSize = GetSaltSize(passwordBytes);

// Removing salt bytes, retrieving original bytes
byte[] originalBytes = new byte[decryptedBytes.Length - saltSize];
for (int i = saltSize; i < decryptedBytes.Length; i++)
{
originalBytes[i - saltSize] = decryptedBytes;
}

return Encoding.UTF8.GetString(originalBytes);
}

public static int GetSaltSize(byte[] passwordBytes)
{
var key = new Rfc2898DeriveBytes(passwordBytes, passwordBytes, 1000);
byte[] ba = key.GetBytes(2);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ba.Length; i++)
{
sb.Append(Convert.ToInt32(ba).ToString());
}
int saltSize = 0;
string s = sb.ToString();
foreach (char c in s)
{
int intc = Convert.ToInt32(c.ToString());
saltSize = saltSize + intc;
}

return saltSize;
}

public static byte[] GetRandomBytes(int length)
{
byte[] ba = new byte[length];
RNGCryptoServiceProvider.Create().GetBytes(ba);
return ba;
}
}
 

Asim A Baki

Active Member
Licensed User
Longtime User
B4XEncryption uses a very specific format. It is not the right tool if you want to communicate with a non-B4X platform. You should instead use Encryption library and configure it based on your requirements.
could you tell me which library you mentioned ? I can not find a link for cipher library in the forum
 
Upvote 0
Top