Recently, the data was uploaded to a government department system, and AES encryption is always unable to match the debugging software on the other side.
After several twists and turns, it is found that the encryption result is always 32 more than the result of the debugging software. For example:
| The code is as follows: |
Copy code |
|
EC4B4C4BD6C3BA3957C1833C9613336D
EC4B4C4BD6C3BA3957C1833C9613336DBF6417F3751BDF3844F7F889FE79E218 |
The length of the original data must be an integer multiple of 16.
The final cause is the influence of algorithm filling parameters:
| The code is as follows: |
Copy code |
Package com. grt. michael. network. communicate. server_zhujianbu;
Import javax. crypto. Cipher;
Import javax. crypto. spec. IvParameterSpec;
Import javax. crypto. spec. SecretKeySpec;
/*************************************** ****************************************
* AES encryption and decryption algorithm
*
* @ Author arix04
*
*/
Public class AES {
// Encryption
Public static String Encrypt (String sSrc, String sKey) throws Exception {
If (sKey = null ){
System. out. print ("Key is null ");
Return null;
}
// Determine whether the Key is 16 bits
If (sKey. length ()! = 16 ){
System. out. print ("The Key length is not 16 bits ");
Return null;
}
Byte [] raw = sKey. getBytes ();
SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES ");
Cipher cipher = Cipher. getInstance ("AES/CBC/NoPadding ");
IvParameterSpec iv = new IvParameterSpec ("0000000000123456". getBytes ());
Cipher. init (Cipher. ENCRYPT_MODE, skeySpec, iv );
Byte [] encrypted = cipher. doFinal (sSrc. getBytes ());
System. out. println ("TEXT =" + new String (encrypted ));
Return byte2hex (encrypted );
}
// Decrypt
Public static String Decrypt (String sSrc, String sKey) throws Exception {
Try {
// Determine whether the Key is correct
If (sKey = null ){
System. out. print ("Key is null ");
Return null;
}
// Determine whether the Key is 16 bits
If (sKey. length ()! = 16 ){
System. out. print ("The Key length is not 16 bits ");
Return null;
}
Byte [] raw = sKey. getBytes ("ASCII ");
SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES ");
Cipher cipher = Cipher. getInstance ("AES/CBC/NoPadding ");
IvParameterSpec iv = new IvParameterSpec ("0000000000123456". getBytes ());
Cipher. init (Cipher. DECRYPT_MODE, skeySpec, iv );
Byte [] encrypted1 = hex2byte (sSrc );
Try {
Byte [] original = cipher. doFinal (encrypted1 );
String originalString = new String (original );
Return originalString;
} Catch (Exception e ){
System. out. println (e. toString ());
Return null;
}
} Catch (Exception ex ){
System. out. println (ex. toString ());
Return null;
}
}
Public static byte [] hex2byte (String strhex ){
If (strhex = null ){
Return null;
}
Int l = strhex. length ();
If (l % 2 = 1 ){
Return null;
}
Byte [] B = new byte [l/2];
For (int I = 0; I! = L/2; I ++ ){
B [I] = (byte) Integer. parseInt (strhex. substring (I * 2, I * 2 + 2 ),
16 );
}
Return B;
}
Public static String byte2hex (byte [] B ){
String hs = "";
String stmp = "";
For (int n = 0; n <B. length; n ++ ){
Stmp = (java. lang. Integer. toHexString (B [n] & 0XFF ));
If (stmp. length () = 1 ){
Hs = hs + "0" + stmp;
} Else {
Hs = hs + stmp;
}
}
Return hs. toUpperCase ();
}
Public static void main (String [] args) throws Exception {
/*
* The encrypted Key can contain 26 letters and numbers. It is recommended that you do not retain the characters. Although it will not be wrong, the individual determines how to decide.
*/
String cKey = "0000000000123456 ";
// The string to be encrypted
String cSrc = "1111122222333334 ";
System. out. println (cSrc );
// Encryption
Long lStart = System. currentTimeMillis ();
String enString = AES. Encrypt (cSrc, cKey );
System. out. println ("The encrypted string is:" + enString );
Long lUseTime = System. currentTimeMillis ()-lStart;
System. out. println ("encryption time:" + lUseTime + "millisecond ");
// Decrypt
LStart = System. currentTimeMillis ();
String DeString = AES. Decrypt (enString, cKey );
System. out. println ("decrypted string:" + DeString );
LUseTime = System. currentTimeMillis ()-lStart;
System. out. println ("decryption time:" + lUseTime + "millisecond ");
}
}
|
References:
| The code is as follows: |
Copy code |
Algorithm/mode/fill in 16-byte encrypted data length less than 16 bytes encrypted length
AES/CBC/NoPadding 16 not supported
AES/CBC/PKCS5Padding 32 16
AES/CBC/ISO10126Padding 32 16
AES/CFB/NoPadding 16 raw data length
AES/CFB/PKCS5Padding 32 16
AES/CFB/ISO10126Padding 32 16
AES/ECB/NoPadding 16 not supported
AES/ECB/PKCS5Padding 32 16
AES/ECB/ISO10126Padding 32 16
AES/OFB/NoPadding 16 raw data length
AES/OFB/PKCS5Padding 32 16
AES/OFB/ISO10126Padding 32 16
AES/PCBC/NoPadding 16 not supported
AES/PCBC/PKCS5Padding 32 16
AES/PCBC/ISO10126Padding 32 16
|
We can see that when the length of the original data is an integer multiple of 16, if the length of the original data is equal to 16 * n, the encrypted data length is equal to 16 * n when NoPadding is used, in other cases, the length of the encrypted data is equal to 16 * (n + 1 ). If the length of the raw data is equal to 16 x n + m [where m is smaller than 16], in addition to NoPadding filling, the encrypted data length is equal to 16 * (n + 1). When NoPadding is filled, the CBC, ECB, and PCBC Modes are not supported, in CFB and OFB modes, the encrypted data length is equal to the original data length.