I am developing an Android application that consumes a WS. For exchange of information between the android application and WebService, you must use the algorithm encryption / decryption Rijndael with 256 bit key.
That is, all the information returned from the WS, will be encrypted, so I decrypts them using the algorithm.
Likewise, all the information I send to WS should be encrypted. Therefore, I will use the encryption algorithm.
I have not found the Rijndael ready to be used in the android platform. But I have the same algorithm in C #.
public class KeydKey
{
public KeydKey()
{
}
#region Metodos de Criptografia
#region key
public string key(string vstrTextToBeEncrypted, string vstrEncryptionKey)
{
byte[] bytValue;
byte[] bytKey;
byte[] bytEncoded;
byte[] bytIV = { 121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62 };
int intLength;
int intRemaining;
MemoryStream objMemoryStream = new MemoryStream();
CryptoStream objCryptoStream;
RijndaelManaged objRijndaelManaged;
//O valor deve estar dentro da tabela ASCII (i.e., no DBCS chars)
bytValue = Encoding.UTF32.GetBytes(vstrTextToBeEncrypted.ToCharArray());
intLength = vstrEncryptionKey.Length;
/*
******A chave cifrada será de 256 bits long (32 bytes)
****** Se for maior que 32 bytes então será truncado.
****** Se for menor que 32 bytes será alocado.
****** Usando upper-case Xs
*/
if (intLength >= 32)
{
vstrEncryptionKey = vstrEncryptionKey.Substring(0, 32);
}
else
{
intLength = vstrEncryptionKey.Length;
intRemaining = 32 - intLength;
string tmp = "";
vstrEncryptionKey = vstrEncryptionKey + tmp.PadRight(intRemaining, 'X');
}
bytKey = Encoding.ASCII.GetBytes(vstrEncryptionKey.ToCharArray());
objRijndaelManaged = new RijndaelManaged();
/* ****** Cria o valor a ser crifrado e depois escreve
****** Convertido em uma disposição do byte
*/
try
{
objCryptoStream = new CryptoStream(objMemoryStream, objRijndaelManaged.CreateEncryptor(bytKey, bytIV), CryptoStreamMode.Write);
objCryptoStream.Write(bytValue, 0, bytValue.Length);
objCryptoStream.FlushFinalBlock();
bytEncoded = objMemoryStream.ToArray();
objMemoryStream.Close();
objCryptoStream.Close();
return Convert.ToBase64String(bytEncoded);
}
catch (Exception ex)
{
return null;
}
}
#endregion
#region dkey
public string dkey(string vstrstringToBeDecrypted, string vstrDecryptionKey)
{
byte[] bytDataToBeDecrypted;
byte[] bytTemp = new byte[0];
byte[] bytIV = { 121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62 };
MemoryStream objMemoryStream = new MemoryStream();
CryptoStream objCryptoStream;
RijndaelManaged objRijndaelManaged;
byte[] bytDecryptionKey;
int intLength;
int intRemaining;
string strReturnstring = string.Empty;
//Convert base64 cifrada para byte array
bytDataToBeDecrypted = Convert.FromBase64String(vstrstringToBeDecrypted);
intLength = vstrDecryptionKey.Length;
/*
******A chave cifrada será de 256 bits long (32 bytes)
****** Se for maior que 32 bytes então será truncado.
****** Se for menor que 32 bytes será alocado.
****** Usando upper-case Xs
*/
if (intLength >= 32)
{
vstrDecryptionKey = vstrDecryptionKey.Substring(0, 32);
}
else
{
intLength = vstrDecryptionKey.Length;
intRemaining = 32 - intLength;
string tmp = "";
vstrDecryptionKey = vstrDecryptionKey + tmp.PadRight(intRemaining, 'X');
}
bytDecryptionKey = Encoding.ASCII.GetBytes(vstrDecryptionKey.ToCharArray());
objRijndaelManaged = new RijndaelManaged();
Array.Resize(ref bytTemp, bytDataToBeDecrypted.Length);
objMemoryStream = new MemoryStream(bytDataToBeDecrypted);
try
{
objCryptoStream = new CryptoStream(objMemoryStream, objRijndaelManaged.CreateDecryptor(bytDecryptionKey, bytIV), CryptoStreamMode.Read);
objCryptoStream.Read(bytTemp, 0, bytTemp.Length);
//objCryptoStream.FlushFinalBlock();
objMemoryStream.Close();
objCryptoStream.Close();
return Encoding.UTF32.GetString(bytTemp).Replace("\0", "");
}
catch (Exception ex)
{
return null;
}
}
#endregion
#endregion
}
Someone tell me where I can get the algorithm for android? Or help me translate the algorithm that I have Java?
Thank you!