Encryption windowsphone8中的AES-CBC-PKCS算法

Encryption windowsphone8中的AES-CBC-PKCS算法,encryption,windows-phone-8,aes,Encryption,Windows Phone 8,Aes,我正在用Android开发一个应用程序,windows 8 Tablet/Desktop和windows phone 8。我在Android和windows 8 Tablet/Desktop应用程序中使用AES CBC算法,并且能够正确加密和解密。我必须在windows phone 8中使用相同的算法。我在web上的一个示例中尝试了该算法,但问题是在windows 8 Tab/Desktop中加密相同的字符串时应用程序和Windows phone 8都是不同的。我确信Windows选项卡/桌面可

我正在用Android开发一个应用程序,windows 8 Tablet/Desktop和windows phone 8。我在Android和windows 8 Tablet/Desktop应用程序中使用AES CBC算法,并且能够正确加密和解密。我必须在windows phone 8中使用相同的算法。我在web上的一个示例中尝试了该算法,但问题是在windows 8 Tab/Desktop中加密相同的字符串时应用程序和Windows phone 8都是不同的。我确信Windows选项卡/桌面可以正常工作,因为它已经在appstore中,并且可以与Android应用程序正常工作

算法的Android代码

公共静态字符串加密字符串明文,字符串密码引发异常{

    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算法有问题。请帮我找出原因。非常感谢您的帮助。

您在Windows应用程序中使用的是PBKDF2密钥派生方案Rfc2898DeriveBytes,在android代码中使用的是直接创建的密钥/IV。请升级android代码以使用PBKDF2。请查看此网站以获取实现此外,如果您打算通过网络发送密文,请不要忘记添加身份验证标签,以防止中间人攻击和oracle攻击

请注意,仅从internet复制代码并不能为您提供很多针对攻击者的保护。在开始实施任何操作之前,您需要了解您要保护的内容,以及至少一些应用加密的基础知识


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

当然,别忘了在最终版本中删除或禁用登录功能,以普通方式显示密钥可能不会让任何用户感到幽默。Owlstead,感谢您的及时回复。Android应用程序已经上市,iOS和windows 8选项卡/桌面应用程序也已上市。我已经在所有这些平台上运行了。我不能更改windows phone 8中的代码吗使其与其他平台兼容。这是一个遗憾,因为上面的代码中有很多与安全相关的问题-静态盐、静态IV、不正确的密钥派生、没有身份验证标签…那么您在Windows和Windows phone 8中使用的是相同的代码吗?我不认为这会在相同的代码库中出错。通常情况下编码可能是一个问题。请注意,对于安全加密例程,您将使用唯一/随机IV,因此您也希望每个加密都不同。您当前正在泄漏数据。