Java AES 256 decryption

topic description

I use JAVA to decrypt and refer to the example of c-sharp. There is no problem with encryption. KEY and IV are both the same

.

sources of topics and their own ideas

related codes

Code of JAVA

public static String decrypt(String hashKey, String hashIv, String value) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(hashKey.getBytes("UTF-8"), "AES");
            IvParameterSpec iv = new IvParameterSpec(hashIv.getBytes("UTF-8"));
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(RemovePKCS7Padding(HexToBytes(value)));
            return new String(Base64.encodeBase64(encrypted), "UTF-8");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    private static byte[] RemovePKCS7Padding(byte[] data) {
        int ilength = data[data.length - 1];
        byte[] output = new byte[data.length - ilength];
        System.arraycopy(data, 0, output, 0, output.length);
        return output;
    }

    public static byte[] HexToBytes(String value) {
        int hexStringLength = value.length();
        byte[] b = new byte[hexStringLength / 2];
        for (int i = 0; i < hexStringLength; i += 2) {
            int topChar = (value.charAt(i) > 0x40 ? value.charAt(i) - 0x37 : value.charAt(i) - 0x30) << 4;
            int bottomChar = value.charAt(i + 1) > 0x40 ? value.charAt(i + 1) - 0x37 : value.charAt(i + 1) - 0x30;
            b[i / 2] = (byte) (topChar + bottomChar);
        }
        return b;
    }

c-sharp code

public string DecryptAES256(string encryptData)// 
 {   
 string sSecretKey = "Xgmz5mMUm7JdpPI7mRXIITSNjPEUtV7f";   
 string iv = "nxKLik2dMNPUqIJy";   
 var encryptBytes = HexStringToByteArray(encryptData.ToUpper());  
  var aes = new RijndaelManaged();   
  aes.Key = Encoding.UTF8.GetBytes(sSecretKey);   
  aes.IV = Encoding.UTF8.GetBytes(iv);  
   aes.Mode = CipherMode.CBC;  
    aes.Padding = PaddingMode.None;  
     ICryptoTransform transform = aes.CreateDecryptor();
        return  Encoding.UTF8.GetString(RemovePKCS7Padding(transform.TransformFinalBl ock(e  ncryptBytes, 0, encryptBytes.Length))); 
        }  
 private static byte[] RemovePKCS7Padding(byte[] data)  {  
  int iLength = data[data.Length - 1]; 
    var output = new byte[data.Length - iLength];  
     Buffer.BlockCopy(data, 0, output, 0, output.Length);  
      return output; 
       } 
private static byte[] HexStringToByteArray(string hexString)  {
  int hexStringLength = hexString.Length;   
  byte[] b = new byte[hexStringLength / 2];  
   for (int i = 0; i < hexStringLength; i += 2)   {   
   int topChar = (hexString[i] > 0x40 ? hexString[i] - 0x37 : hexString[i]  - 0x30) << 4;   
   int bottomChar = hexString[i + 1] > 0x40 ? hexString[i + 1] - 0x37 :  hexString[i + 1] - 0x30;   b[i / 2] = Convert.ToByte(topChar + bottomChar);  
    }   
    return b;
     }

what result do you expect? What is the error message actually seen?

c-sharp topics, official examples
Key: Xgmz5mMUm7JdpPI7mRXIITSNjPEUtV7f
IV: nxKLik2dMNPUqIJy
decryption data: fb7a19d840c9877d26d961f6a906602439260588e0e9db45cdc0d4d69a3b97fe22e00fda051ee90c7e987e62a717d409a45e4c04893caa90b31f86dc32929debb391145325f07068854efb5977e9aed0b684e7b0a1cb45a764bad9f4d9ab32cb1f634c66e315054b2d3589a1d9fc0ad3dfdb8dad102df281c306c25972047d4e

normal result: MerchantID=MS15295340&RespondType=JSON&TimeStamp=1485232229&Version=1.4&MerchantOrderNo=S_1485232288&Amt=40&ItemDesc=UnitTest

this error occurred Input length not multiple of 16 bytes

Nov.19,2021

requires self-writing padding, and the encrypted string length is padded to a multiple of 16.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf140b-31d3f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf140b-31d3f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?