带有PKCS7填充的AES 256

问题描述

如果公司营业额大于50亿,GST委员会已批准实施企业对企业(B2B)发票的“电子发票”或“电子发票”。 gst门户API的参考:einv-apisandbox.nic.in/index.html

我必须使用APP密钥对加密的SEK进行解密,并使用解密的SEK对json数据进行编码以发布到Einvoice生成,并且我找到了Java和C#的示例代码,并且我已经在PHP中进行了转换,但不幸的是未能获得所需的输出

就我而言,加密的SEK为:oRvKfBtmgNTSuk/oXUhiLOjXi45jiWA2oKNxhhQM3UH2o/32YWGLbUjK1/dohPe3

APP键:fao1PoKaLgd11xMrWTiL2cggAfx9QMwM

对称解密(AES)(在Java中)

public static String decrptyBySyymetricKey(String encryptedSek,byte[] appKey)
{
    Key aesKey = new SecretKeySpec(appKey,"AES"); // converts bytes(32 byte random generated) to key
    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // encryption type = AES with padding PKCS5
        cipher.init(Cipher.DECRYPT_MODE,aesKey); // initiate decryption type with the key
        byte[] encryptedSekBytes = Base64.decodeBase64(encryptedSek); // decode the base64 encryptedSek to bytes
        byte[] decryptedSekBytes = cipher.doFinal(encryptedSekBytes); // decrypt the encryptedSek with the initialized cipher containing the key(Results in bytes)
        String decryptedSek = Base64.encodeBase64String(decryptedSekBytes); // convert the decryptedSek(bytes) to Base64 StriNG
        return decryptedSek; // return results in base64 string
    }catch(Exception e) {
        return "Exception; "+e;
    }
}

对称加密(AES)(在Java中)

public static string EncryptBySymmetricKey(string text,string sek)
    {
    //Encrypting SEK
    try
    {
      byte[] dataToEncrypt = Convert.FromBase64String(text);
      var keyBytes = Convert.FromBase64String(sek);
      AesManaged tdes = new AesManaged();
      tdes.KeySize = 256;
      tdes.BlockSize = 128;
      tdes.Key = keyBytes;
      tdes.Mode = CipherMode.ECB;
      tdes.Padding = PaddingMode.PKCS7;
      pICryptoTransform encrypt__1 = tdes.CreateEncryptor();
      byte[] deCipher = encrypt__1.TransformFinalBlock(dataToEncrypt,dataToEncrypt.Length);
      tdes.Clear();
      string EK_result = Convert.ToBase64String(deCipher);
      return EK_result;
   }
   catch (Exception ex)
    {
      throw ex;
   }
 }

对称加密(AES)(在PHP中)

function encrypt($data,$key)
{
    $padding = 16 - (strlen($data) % 16);
    $data .= str_repeat(chr($padding),$padding);
    return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,hash('SHA256',$key,true),$data,MCRYPT_MODE_ECB));
 }

对称解密(AES)(在PHP中)

function decrypt($key,$str) 
{
    $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$str,MCRYPT_MODE_ECB);
    $pad = ord($str[($len = strlen($str)) - 1]);
    $len = strlen($str);
    $pad = ord($str[$len-1]);
    
    return base64_encode( substr($str,strlen($str) - $pad));
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)