在WIndows 8 Appstore应用程序中的AES CBC PKCS5解密

时间:2013-05-20 04:57:16

标签: android encryption windows-8 aes

尽管在网上有很多与此相关的文章,但是我很难尴尬地问这个问题,但是我无法理解。我很震惊这个。

我正在使用AES/CBC/PKCS5算法加密Android中的文本,但我无法在Windows 8 appstore应用程序中解密它。这是我的加密代码

public static String encrypt(String plainText,String password) throws Exception {

    // 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 8应用程序中的以下代码加密

public string AES_Encrypt(string input, string pass)
{
  SymmetricKeyAlgorithmProvider SAP = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);

  string encrypted = "";
  try
  {
    byte[] test1 = System.Text.Encoding.UTF8.GetBytes(pass);
    byte[] test2 = new byte[16];

    for (int i = 0; i < test1.Length;i++ )
    {
      test2[i] = test1[i];
    }

    CryptographicKey key =
    SAP.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(test2));

    IBuffer Buffer = CryptographicBuffer.CreateFromByteArray(System.Text.Encoding.UTF8.GetBytes(input));
    encrypted = CryptographicBuffer.EncodeToBase64String(CryptographicEngine.Encrypt(key, Buffer, null));

    return encrypted;
  }
  catch (Exception ex)
  {
    return null;
  }
}

以下是解密算法

public string AES_Decrypt(string input, string pass)
{

  SymmetricKeyAlgorithmProvider SAP = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);

  string decrypted = "";

  try
  {
    byte[] test1 = System.Text.Encoding.UTF8.GetBytes(pass);
    byte[] test2 = new byte[16];

    for (int i = 0; i < test1.Length;i++ )
    {
      test2[i] = test1[i];
    }

    CryptographicKey key = 
        SAP.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(test2));


    IBuffer Buffer = CryptographicBuffer.DecodeFromBase64String(input);
    byte[] Decrypted;

    CryptographicBuffer.CopyToByteArray(CryptographicEngine.Decrypt(key, Buffer, null), out Decrypted);
    decrypted = System.Text.Encoding.UTF8.GetString(Decrypted, 0, Decrypted.Length);
    return decrypted;
  }
  catch (Exception ex)
  {
    return null;
  }
}

我知道生成的IV有问题。如果我将Iv设为null,则解密算法会生成一些结果(尽管它是错误的),如果我为IV提供一些值,则会抛出异常,例如“值不在预期范围内范围。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我不确定从Android到Windows的转换,但我注意到在JAva中你使用的是PKCS5和C#PKCS7。

相关问题