簡體   English   中英

在C#中使用AESManaged進行AES加密

[英]AES encryption using AESManaged in C#

伙計們我正在Windows Phone 8上做一個項目,我試圖使用key和iv將aes數組加密成字節數組的aes,並且在android和wp8中傳遞相同的key和iv,但是沒有得到相同的輸出。我的代碼??? 請幫助我解決這個問題。

我的C#aes加密代碼是:

public static byte[] Encrypt (byte[] data, byte[] key) 
{
    byte[] encrypted;

    using (AesManaged AESM = new AesManaged())
    {
        byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        AESM.KeySize = 128;
        AESM.BlockSize = AESM.LegalBlockSizes[0].MaxSize;
        AESM.Key = key;
        AESM.IV = iv;

        ICryptoTransform encryptor = AESM.CreateEncryptor();

        encrypted = encryptor.TransformFinalBlock(data, 0, data.Length);
    }
    return encrypted;
}

我的android aes加密代碼是:

public static byte[] encryptAESWithIV(byte[] key, byte[] clearText) throws 
NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 
InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, 
UnsupportedEncodingException 
{
    SecretKeySpec skeySpec = new SecretKeySpec(key, CIPHER_AES);

    Cipher cipher = Cipher.getInstance(CIPHER_AES_MODE);

    byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

    try 
    {
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv));
    }
    catch (InvalidAlgorithmParameterException e)
    {
        e.printStackTrace();
    }

    byte[] encrypted = cipher.doFinal(clearText);
    byte[] output = new byte[encrypted.length + iv.length];

    System.arraycopy(iv, 0, output, 0, iv.length);
    System.arraycopy(encrypted, 0, output, iv.length, encrypted.length);

    return output;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM