Written by

Question 姚 鑫 · Mar 4, 2024

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?

Comments

Enrico Parisi · Mar 4, 2024

Can you provide a simple sample code to reproduce the error?

0
姚 鑫  Mar 4, 2024 to Enrico Parisi

 

0
姚 鑫 · Mar 4, 2024

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
}

0
姚 鑫  Mar 4, 2024 to Enrico Parisi

USER>w $zv
Cache for Windows (x86-64) 2016.2 (Build 736U) Fri Sep 30 2016 11:46:02 EDT

0
Enrico Parisi  Mar 4, 2024 to 姚 鑫

I'm sorry, I don't have a system that old to check/test.

The oldest I have is 2018 and there AESEncode() and AESDecode() are deprecated (use AESCBCEncrypt() and AESCBCDecrypt() instead).

Are AESCBCEncrypt() and AESCBCDecrypt() implemented in version 2016?
And AESCBCEncryptStream(), AESCBCDecryptStream()?

0
姚 鑫  Mar 4, 2024 to Enrico Parisi

 

0
姚 鑫  Mar 4, 2024 to Enrico Parisi

AESCBCEncrypt cannot satisfy AESEncode ECB. Is there an AESECBEncryptStream method?

0
li scott · Mar 6, 2024

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
}

0