使用Rijndael在Android上使用密钥256位进行加密/解密

时间:2012-07-19 15:57:19

标签: android web-services encryption rijndael

我正在开发一个消耗WS的Android应用程序。 要在android应用程序和WebService之间交换信息,必须使用带有256位密钥的算法加密/解密Rijndael。

也就是说,从WS返回的所有信息都将被加密,因此我使用该算法对它们进行解密。

同样,我发送给WS的所有信息都应加密。因此,我将使用加密算法。

我还没有发现Rijndael已准备好在Android平台上使用。但我在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


}

有人告诉我在哪里可以获得Android的算法?或者帮我翻译一下我有Java的算法?

谢谢!

1 个答案:

答案 0 :(得分:3)

Cipher.getInstance("AES/CBC/PKCS5Padding")应该做到这一点。您必须匹配(字符)-encodings,您可以使用SecretKeySpec(byte[32])创建256位AES密钥。