Hi,
I am having trouble to decrypt a string encrypted by a C# code.
To decrypt in C# I use:
where keysize=256
I tried the following B4A code to decrypt:
I think the trouble is in:
which is not the same as:
Based on other thread I found the following Javacode:
But I have no knowledge of using Javacode directly in B4A and I don't know if the above Javacode is the solution.
Can someone help me?
Kind regards,
André
I am having trouble to decrypt a string encrypted by a C# code.
To decrypt in C# I use:
B4X:
//Decrypt
public static string DecryptString(string cipherText, string passPhrase)
{
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
byte[] keyBytes = password.GetBytes(keysize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
where keysize=256
I tried the following B4A code to decrypt:
B4X:
Sub Decryptcbc(enc As String)
Dim Bconv As ByteConverter
Dim key(0) As Byte
Dim data(0) As Byte
Dim iv(0) As Byte
Dim Lock As String
Dim Sleutel As String
Dim c As Cipher
Dim su As StringUtils
Dim kg As KeyGenerator
Lock="alhbcde8qwrtyz27"
iv = Lock.GetBytes("UTF8")
c.Initialize("AES/CBC/PKCS7Padding")
c.InitialisationVector = iv
Sleutel="Jaap123456789012"
kg.Initialize("AES")
kg.KeyFromBytes(Sleutel.GetBytes("UTF8"))
key = Sleutel.GetBytes("UTF8")
Msgbox(Bconv.HexFromBytes(key), "Key " & key.Length & " bytes")
data = su.DecodeBase64(enc)
Msgbox(Bconv.HexFromBytes(data), "Encrypted is " & data.Length & " bytes")
data = c.Decrypt(data, kg.Key, True)
Msgbox(Bconv.StringFromBytes(data, "UTF8"), "Decrypted")
End Sub
I think the trouble is in:
B4X:
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
byte[] keyBytes = password.GetBytes(keysize / 8);
which is not the same as:
B4X:
kg.Initialize("AES")
kg.KeyFromBytes(Sleutel.GetBytes("UTF8"))
Based on other thread I found the following Javacode:
B4X:
byte[] password = PassPhrase.getBytes("ASCII");
byte[] salt = PKCS5S1ParametersGenerator.PKCS5PasswordToBytes(SaltValue.toCharArray());
PKCS5S1ParametersGenerator generator = new PasswordDeriveBytes(new SHA1Digest());
generator.init(password, salt, PasswordIterations);
byte[] key = ((KeyParameter)generator.generateDerivedParameters(32*8)).getKey();
But I have no knowledge of using Javacode directly in B4A and I don't know if the above Javacode is the solution.
Can someone help me?
Kind regards,
André