Windows Phone 8中的AES CBC PKCS算法

时间:2013-08-14 11:58:43

标签: encryption windows-phone-8 aes

我正在Android,Windows 8平板电脑/台式机和Windows手机8中开发应用程序。我在Android和Windows 8平板电脑/桌面应用程序中使用AES CBC算法,并且能够正确加密和解​​密。我必须使用相同的Windows Phone 8中的算法。我尝试从Web上提供的示例,但问题是在Windows 8 Tab / Desktop应用程序中加密相同的字符串时,Windows Phone 8都不同。我确信Windows Tab / Desktop正在运行很好,因为它已经在appstore中,它适用于Android应用程序。

算法的Android代码

public static String encrypt(String plainText,String password)抛出异常{

    if (plainText == null || plainText.length() == 0)
        return "";

    // convert key to bytes
    byte[] keyBytes = password.getBytes("UTF-8");
    // Use the first 16 bytes (or even less if key is shorter)

    byte[] keyBytes16 = new byte[16];

    System.arraycopy(keyBytes, 0, keyBytes16, 0,
            Math.min(keyBytes.length, 16));

    // convert plain text to bytes
    byte[] plainBytes = plainText.getBytes("UTF-8");

    // setup cipher
    SecretKeySpec skeySpec = new SecretKeySpec(keyBytes16, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    byte[] iv = new byte[16]; // initialization vector with all 0
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv));

    // encrypt
    byte[] encrypted = cipher.doFinal(plainBytes);
    String encryptedString = Base64.encodeToString(
            cipher.doFinal(plainBytes), Base64.NO_WRAP);
    // encryptedString

    return Base64.encodeToString(encrypted, Base64.NO_WRAP);
}

Windows Phone 8

public static string Encrypt1(string dataToEncrypt, string password, string salt)


 {

AesManaged aes = null;
            MemoryStream memoryStream = null;
            CryptoStream cryptoStream = null;


try
            {
                byte[] b = new byte[16];
                byte[] pwd = System.Text.UTF8Encoding.UTF8.GetBytes(password);
                byte[] pwd1 = new byte[16];
                for (int i = 0; i < 16;i++ ) // take first 16 bits of the password
                {
                    pwd1[i] = pwd[i];
                }

String newPwd = System.Text.Encoding.UTF8.GetString(pwd1, 0 ,pwd1.Length);

                //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator 
                Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(newPwd, b);

                //Create AES algorithm with 256 bit key and 128-bit block size 
                aes = new AesManaged();
                //aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
                aes.KeySize = 128;
                aes.Key = rfc2898.GetBytes(128 / 8);
                aes.IV = rfc2898.GetBytes(128 / 8);
                //aes.IV = b;
                //Create Memory and Crypto Streams 
                memoryStream = new MemoryStream();
                cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
                //Encrypt Data 
                byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt);
                cryptoStream.Write(data, 0, data.Length);
                cryptoStream.FlushFinalBlock();
                //Return Base 64 String 
                return Convert.ToBase64String(memoryStream.ToArray());
            }
            finally
            {
                if (cryptoStream != null)
                    cryptoStream.Close();
                if (memoryStream != null)
                    memoryStream.Close();
                if (aes != null)
                    aes.Clear();
            }
        }

我知道我的win phone 8算法有问题。请帮助我找出它。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您正在使用Windows应用程序中的PBKDF2密钥派生方案Rfc2898DeriveBytes和Android代码中直接创建的密钥/ IV。请升级Android代码以使用PBKDF2(查看此站点的实现)和 - 如果您要通过网络发送密文 - 请不要忘记添加身份验证标记以防止中间人和填充oracle攻击。

请注意,只是从互联网上复制代码并不能为您提供大量防范攻击者的保护。在开始实现任何内容之前,您需要了解您正在保护的内容以及至少一些应用加密的基础知识。

开始实施加密时,请注意它对小错误不是很宽容。一个错误的位将完全弄乱你的密文 - 没有警告,没有特定的错误。因此,记录 all 输入/输出参数的所有十六进制表示形式 - 这会在早期向您显示不同的键和IV值。