0

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

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

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

我还没有找到可以在 android 平台上使用的 Rijndael。但我在 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的算法?

谢谢!

4

1 回答 1

3

Cipher.getInstance("AES/CBC/PKCS5Padding")应该做的伎俩。您将必须匹配(字符)编码,并且可以SecretKeySpec(byte[32])用来创建 256 位 AES 密钥。

于 2012-07-19T18:28:29.243 回答