AES简单解密

Posted on 2020-03-18 11:27  努力成长静待花开  阅读(653)  评论(0编辑  收藏  举报

某app返回内容进行了加密处理

其本地解密实现如下

    @NotNull
    public final String decrypt(@NotNull String str, @NotNull String str2) {
        Intrinsics.checkParameterIsNotNull(str, "input");
        Intrinsics.checkParameterIsNotNull(str2, "key");
        Cipher instance = Cipher.getInstance("AES/CBC/NoPadding");
        Object bytes = str2.getBytes(Charsets.UTF_8);
        Intrinsics.checkExpressionValueIsNotNull(bytes, "(this as java.lang.String).getBytes(charset)");
        SecretKeySpec secretKeySpec = new SecretKeySpec(bytes, "AES");
        Object bytes2 = str2.getBytes(Charsets.UTF_8);
        Intrinsics.checkExpressionValueIsNotNull(bytes2, "(this as java.lang.String).getBytes(charset)");
        instance.init(2, secretKeySpec, new IvParameterSpec(bytes2));
        Object doFinal = instance.doFinal(Base64.decode(str, 0));
        Intrinsics.checkExpressionValueIsNotNull(doFinal, "encrypt");
        return StringsKt.trim(new String(doFinal, Charsets.UTF_8), new char[]{'\u0000'});
    }

key值为一个写死的字符串

参数有两个 str //加密后的字符串  str2 //解密需要的 key

另偏移向量为 str2 utf8 的字节数组

实现如下 

function decrypt($str, $key)
{
    if([string]::IsNullOrEmpty($str)){return $null}
    $toEncryptArray=[Convert]::FromBase64String($str)
    $ivArray=[Text.Encoding]::UTF8.GetBytes($key)
    $rm=[System.Security.Cryptography.RijndaelManaged]::new()
        $rm.Key=[Text.Encoding]::UTF8.GetBytes($key)
        $rm.IV=$ivArray
        $rm.Mode=[System.Security.Cryptography.CipherMode]::CBC
        $rm.Padding=[System.Security.Cryptography.PaddingMode]::None
    $cTransform=$rm.CreateDecryptor()
    $resultArray=$cTransform.TransformFinalBlock($toEncryptArray,0,$toEncryptArray.length)
    return [Text.Encoding]::UTF8.GetString($resultArray).Trim()
}