Question
· Mar 4

I want to use AESEncode encryption, but it prompts a MAXSTRING error. How to solve it?

I want to use AESEncode encryption, but it prompts a MAXSTRING error. How to solve it?

Discussion (9)2
Log in or sign up to continue

ClassMethod AESECBPKCS5PaddingEncrypt(str As %String, key As %String = "") As %String
{
str = $zcvt(str, "O", "UTF8")
paddingLen = 16 - ($length(str) # 16)
list = $lb(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f")
paddingStr= "0" _ $lg(list, paddingLen)
padding = ..Repeat($c($zhex(paddingStr)) ,paddingLen)
ret  = str _ padding
ret = ##class(%SYSTEM.Encryption).AESEncode(ret, key)
ret = ##class(%SYSTEM.Encryption).Base64Encode(ret)
ret
}

you try try 

ClassMethod AESEncode(str As %String, key)
{
    Set text=$ZCONVERT(str,"O","UTF8")
    set len=$l(text)
    if (16-len#16)'=0{
        for ii=1:1:(16-len#16){
            set text=text_$c(16-len#16)
        }
    }else{
        for ii=1:1:16{
            set text=text_$c(16)
        }
    }
    s stream=##class(%GlobalCharacterStream).%New()
    d stream.Write(text)
    s textstr=""
    while 'stream.AtEnd{
        set subtx=stream.Read(16)
        Set textstr =textstr_$system.Encryption.AESEncode(subtx,key)
    }
    Set ret=$SYSTEM.Encryption.Base64Encode(textstr)
    s ret = $replace(ret,$c(13,10),"")
    q ret
}

/**
 * 解密
 */
ClassMethod AESDecode(str As %String, key)
{
    s ret = ##class(%SYSTEM.Encryption).Base64Decode(str)
    s ret = ##class(%SYSTEM.Encryption).AESDecode(ret, key)
    s ret = $zcvt(ret, "I", "UTF8")    
    set len=$l(ret)
    set lastch=$e(ret,len,len)
    set pos=0
    for index=1:1:16 {
        if $c(index)=lastch{
            set pos=index
            quit 
        }
    }
    s ret = $e(ret,1,len-pos)
    q ret
}