当前位置: 代码迷 >> J2ME >> 用Java实现C#加密,该怎么处理
  详细解决方案

用Java实现C#加密,该怎么处理

热度:743   发布时间:2016-04-17 20:58:16.0
用Java实现C#加密
服务器端数据库保存的加密密码是别人用C#实现的,代码不能更改,我现在要做移动客户端,问下用Java怎么实现该加密?
C#加密代码如下所示:
using System;
using System.Security.Cryptography;  
using System.Text;

namespace GCIT.Common.DEncrypt
{
    public class DESEncrypt
    {
        public DESEncrypt()
        {
        }
        #region ========加密========
        /// <summary>
        /// 加密
        public static string Ecrypt()
        {
             return Encrypt("global");
        }

        public static string Encrypt(string Text)
        {
            return Encrypt(Text, "GCITSOFT");
        }

        /// <summary> 
        /// 加密数据 
        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray;
            inputByteArray = Encoding.Default.GetBytes(Text);
            des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }
        #endregion
    }
}

这应该是外面是一个DES加密,然后里面嵌套了MD5算法,这个用Java怎么实现?
主要这两段代码是啥意思?

des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.
HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));

------解决思路----------------------
其实很简单啊,就是自己写个加密算法,把数据库配置加密了,然后写个接解密算法,在项目启动注册数据信息时去解析加密数据,解析出来的配置直接加载进内存生效就好了
  相关解决方案