簡體   English   中英

Android / Java和.Net的AES加密僅部分相同

[英]AES encryption for Android/Java and .Net only partially identical

我正在使用.Net和Java(在Android上)使用密碼和初始化向量的AES加密對字符串進行加密(然后解密)。 我在各方面的加密和解密分別運行良好,但是相比之下,我得到的結果卻一直不同,因此不允許在一個系統上進行加密和解密。 我特別不明白為什么加密字節數組的前半部分相同,而后半部分不同(而所有其他字節數組完全相同)?

這是我使用的兩個加密代碼的字節數組形式:

] 的Java(為便於比較在其上已經負值加入256):202,147,148,168,圖 ]

] .Net:[ 202、147、148、168、9、104、213、176、174、157、124、160、54、33、151、246、109、127、84、129、168、106、21 ]

我在下面發布了用於加密的兩個代碼片段。 盡管我一直在尋找幾個小時內我做錯了什么的提示,但我無法弄清楚哪里出了問題。 如果有人可以指出我看不到的東西,那將是非常友好的。

這是我從兩個代碼版本中打印所有相關值時得到的結果:

Android-Output:
    Key (String): 6543210987654321
    IV (String): 1234567890123456
    Input (String): Encrypt_this_text

    Key (Bytes): [54, 53, 52, 51, 50, 49, 48, 57, 56, 55, 54, 53, 52, 51, 50, 49]
    IV (Bytes): [49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54]
    Input (Bytes): [69, 110, 99, 114, 121, 112, 116, 95, 116, 104, 105, 115, 95, 116, 101, 120, 116]

    Encrypted: [-54, -109, -108, -88, 9, 104, -43, -80, -82, -99, 124, -96, 54, 33, -105, -10, 70, 63, -72, 118, -28, 50, -14, -32, -25, 37, 34, 16, 123, 9, -113, 81]
    Encrypted (+256 if <0): [202, 147, 148, 168, 9, 104, 213, 176, 174, 157, 124, 160, 54, 33, 151, 246, 70, 63, 184, 118, 228, 50, 242, 224, 231, 37, 34, 16, 123, 9, 143, 81]

VBNet-Output:
    Key (String)=6543210987654321
    IV (String)=1234567890123456
    Input (String)=Encrypt_this_text

    Key (Byte)=[54, 53, 52, 51, 50, 49, 48, 57, 56, 55, 54, 53, 52, 51, 50, 49]
    IV (Byte)=[49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54]
    Input (Byte)=[69, 110, 99, 114, 121, 112, 116, 95, 116, 104, 105, 115, 95, 116, 101, 120, 116]

    Encrypted=[202, 147, 148, 168, 9, 104, 213, 176, 174, 157, 124, 160, 54, 33, 151, 246, 109, 127, 84, 129, 168, 106, 21, 159, 131, 67, 75, 209, 166, 221, 190, 243]

這是用於生成加密字節數組的Java代碼:

String skey = "6543210987654321";
String siv = "1234567890123456";
String sinput = "Encrypt_this_text";

byte[] key = skey.getBytes("UTF8");
byte[] iv = siv.getBytes("UTF8");
byte[] input = sinput.getBytes("UTF8");

Cipher cipher = Cipher.getInstance("AES/CBC/ZeroBytePadding");
SecretKeySpec keyspec = new SecretKeySpec(key, "AES" );
IvParameterSpec ivparams = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivparams);

byte[] encrypted = cipher.doFinal(input);

...這是我的Net(在本例中為VB)測試代碼:

Dim rm As New System.Security.Cryptography.AesManaged

Dim skey As String = "6543210987654321"
Dim siv As String = "1234567890123456"
Dim sinput As String = "Encrypt_this_text"

Dim key() As Byte = System.Text.Encoding.ASCII.GetBytes(skey)
Dim IV() As Byte = System.Text.Encoding.ASCII.GetBytes(siv)
Dim input() As Byte = System.Text.Encoding.ASCII.GetBytes(sinput)

Dim ict As System.Security.Cryptography.ICryptoTransform = rm.CreateEncryptor(key, IV)

Dim encrypted() As Byte = ict.TransformFinalBlock(input, 0, input.Length)

這是由於.NET的AesManaged.CreateEncryptor中的默認填充為PKCS7。

在Java中,您可以使用Zeros填充進行初始化。

您可以通過將rm的Padding屬性更改為PaddingMode.Zeros來覆蓋填充。

有關MSDN的文章: MSDN

暫無
暫無

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

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