• C# AESCBC256 与 java AESCBC256 加解密


    和某上市公司对接接口,他们试用 java AES CBC PKCS5 256 加解密。网上C# 基本不合适。

    注意:C# PKCS7 对应 java PKCS5

     /// <summary>  
            /// AES加密
            /// </summary>  
            /// <param name="encryptStr">明文</param>  
            /// <param name="key">密钥</param>  
            /// <returns></returns>
            protected string Encrypt(string encryptStr, string key)
            {
                var _aes = new AesCryptoServiceProvider();
                _aes.BlockSize = 128;
                _aes.KeySize = 256;
                _aes.Key = Encoding.UTF8.GetBytes(key);
                _aes.IV = (byte[])(object)new sbyte[16];//Encoding.UTF8.GetBytes(IV);
                _aes.Padding = PaddingMode.PKCS7;
                _aes.Mode = CipherMode.CBC;
    
                var _crypto = _aes.CreateEncryptor(_aes.Key, _aes.IV);
                byte[] encrypted = _crypto.TransformFinalBlock(Encoding.UTF8.GetBytes(encryptStr), 0, Encoding.UTF8.GetBytes(encryptStr).Length);
    
                _crypto.Dispose();
    
                return System.Convert.ToBase64String(encrypted);
            }

     /// <summary>  
            /// AES解密
            /// </summary>  
            /// <param name="decryptStr">密文</param>  
            /// <param name="key">密钥</param>  
            /// <returns></returns>  
            protected string Decrypt(string decryptStr, string key)
            {
                var _aes = new AesCryptoServiceProvider();
                _aes.BlockSize = 128;
                _aes.KeySize = 256;
                _aes.Key = Encoding.UTF8.GetBytes(key);
                _aes.IV = (byte[])(object)new sbyte[16];//Encoding.UTF8.GetBytes(IV);
                _aes.Padding = PaddingMode.PKCS7;
                _aes.Mode = CipherMode.CBC;
    
                var _crypto = _aes.CreateDecryptor(_aes.Key, _aes.IV);
                byte[] decrypted = _crypto.TransformFinalBlock(
                    System.Convert.FromBase64String(decryptStr), 0, System.Convert.FromBase64String(decryptStr).Length);
                _crypto.Dispose();
                return Encoding.UTF8.GetString(decrypted);
            }
  • 相关阅读:
    java 显示当前的时间
    java RandomAccessFile 向文件中写入数据,怎么样不覆盖原来的数据
    NCARG 6.2.0 安装方法
    【转】在Fedora 9下用IFC安装MM5
    Linux下GrADS的安装
    我的博客园
    nyist 2 括号配对问题
    nyist 100 1的个数
    进制(堆栈)
    进制
  • 原文地址:https://www.cnblogs.com/mycing/p/8277693.html
Copyright © 2020-2023  润新知