C# Java AES 协议加密

| 发布     | 分类 C#  | 标签 C#  Java  AES  Proto  协议加密 



dll反编译工具 (ilspy)

dll代码加密混淆工具 (ConfuserEx)

开源.net 混淆器ConfuserEx介绍



using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

public class de
{
    public class DE
    {


        private string keyStr   = "12345678123456781234567812345678";
        private string ivStr    = "12345678123456781234567812345678";

        public DE()
        {


            CreateDES(keyStr, ivStr);
        }

        SymmetricAlgorithm des;
        private void CreateDES(string keyStr, string ivStr)
        {
            byte[] key  = new byte[16];
            byte[] iv   = new byte[16];

            byte[] bytes = Encoding.UTF8.GetBytes(keyStr);
            for(int i = 0; i < key.Length; i ++)
            {
                key[i] = i < bytes.Length ? bytes[i] : (byte)0;
            }

            bytes = Encoding.UTF8.GetBytes(ivStr);
            for(int i = 0; i < iv.Length; i ++)
            {
                iv[i] = i < bytes.Length ? bytes[i] : (byte)0;
            }


            //分组加密算法   
            des = Rijndael.Create();    
            //设置密钥及密钥向量
            des.Key = key;
            des.IV = iv;
        }



        public byte[] encrypt(byte[] src)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(src, 0, src.Length);
                    cs.FlushFinalBlock();
                    byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组   
                    cs.Close();
                    ms.Close();                   
                    return cipherBytes;
                }
            }
        }


        public byte[] decrypt(byte[] src)
        {
            return decrypt(src, 0, src.Length);
        }

        public byte[] decrypt(byte[] src, int offest, int length)
        {
            byte[] decryptBytes = new byte[src.Length];
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(src, offest, length);
                ms.Position = 0;

                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read))
                {
                    cs.Read(decryptBytes, 0, decryptBytes.Length);
                    cs.Close();
                    ms.Close();
                }
            }
            return decryptBytes;
        }


    }

  


}







import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class ProtocolAES
{

    private static ProtocolAES _instanceLogin;
    public static ProtocolAES getInstanceLogin()
    {
        if(_instanceLogin == null)
        {
            _instanceLogin = new ProtocolAES();
            
            try
            {
                _instanceLogin.init();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return _instanceLogin;
    }
    
    
    private static ProtocolAES _instance;
    public static ProtocolAES getInstance()
    {
        if(_instance == null)
        {
            _instance = new ProtocolAES();
            try
            {
                _instance.keyStr = "12345678123456781234567812345678";
                _instance.init();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return _instance;
    }
    

    public String keyStr    = "12345678123456781234567812345678";
    public String ivStr     = "12345678123456781234567812345678";
    private Cipher encryptCipher;
    private Cipher decryptCipher;
    
    public void init() throws Exception
    {

        
        
        byte[] key = new byte[16];
        byte[] bytes = keyStr.getBytes("utf-8");
        for(int i = 0; i < key.length; i ++)
        {
            key[i] = i < bytes.length ? bytes[i] : (byte)0;
        }
        

        System.out.println("keyStr=" + keyStr);
        System.out.println("keyStr.length=" + keyStr.length());
        System.out.println("key.length=" + key.length);
        
        byte[] iv = new byte[16];
        bytes = ivStr.getBytes("utf-8");
        for(int i = 0; i < iv.length; i ++)
        {
            iv[i] = i < bytes.length ? bytes[i] : (byte)0;
        }
        

        System.out.println("ivStr.length=" + ivStr.length());
        System.out.println("iv.length=" + iv.length);
        
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        IvParameterSpec _iv = new IvParameterSpec(iv);// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
        
        
        encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
        encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec, _iv);
        
        

        decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        decryptCipher.init(Cipher.DECRYPT_MODE, skeySpec, _iv);
    }
    
    public byte[] encrypt(byte[] src) throws Exception 
    {
        return encryptCipher.doFinal(src);
    }
    
    public byte[] decrypt(byte[] src) throws Exception
    {
        return decryptCipher.doFinal(src);
    }
}



上一篇: unity GUID查看项目资源使用情况工具
下一篇: C#实现Zip压缩解压实例