欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AES CBC PKCS7 C# C++

程序员文章站 2022-09-10 15:15:46
c++算法见:https://blog.csdn.net/csdn49532/article/details/50686222 c#:https://gitee.com/koastal/codes/659traqginxjoskd8pylc27 注意 key 和 iv 都需要为16位字符串 代码实现 ......

  c++算法见:

 

 

c#:

        public static string encrypt(string toencrypt, string key, string iv)
        {
            byte[] keyarray = utf8encoding.utf8.getbytes(key);
            byte[] ivarray = utf8encoding.utf8.getbytes(iv);
            byte[] toencryptarray = utf8encoding.utf8.getbytes(toencrypt);

            rijndaelmanaged rdel = new rijndaelmanaged();
            rdel.key = keyarray;
            rdel.iv = ivarray;
            rdel.mode = ciphermode.cbc;
            rdel.padding = paddingmode.pkcs7;

            icryptotransform ctransform = rdel.createencryptor();
            byte[] resultarray = ctransform.transformfinalblock(toencryptarray, 0, toencryptarray.length);

            return convert.tobase64string(resultarray, 0, resultarray.length);
        }

        public static string decrypt(string todecrypt, string key, string iv)
        {
            byte[] keyarray = utf8encoding.utf8.getbytes(key);
            byte[] ivarray = utf8encoding.utf8.getbytes(iv);
            byte[] toencryptarray = convert.frombase64string(todecrypt);

            rijndaelmanaged rdel = new rijndaelmanaged();
            rdel.key = keyarray;
            rdel.iv = ivarray;
            rdel.mode = ciphermode.cbc;
            rdel.padding = paddingmode.pkcs7;

            icryptotransform ctransform = rdel.createdecryptor();
            byte[] resultarray = ctransform.transformfinalblock(toencryptarray, 0, toencryptarray.length);

            return utf8encoding.utf8.getstring(resultarray);
        }

 

注意 key 和 iv 都需要为16位字符串

 

代码实现:https://1drv.ms/u/s!at1zjv3_16acmb8aisvhmvdyecfqlw