0

我在 C# .net 中使用以下类进行数据加密。java android中用于在android和web-service之间传输数据的等效库或算法是什么?

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for Crypto
/// </summary>

public enum SymmProvEnum : int
        {
            DES, RC2, Rijndael
        }
public class Crypto
{

    string Globalkey = "test";
        private SymmetricAlgorithm mobjCryptoService;

        /// <remarks>
        /// Constructor for using an intrinsic .Net SymmetricAlgorithm class.
        /// </remarks>
        public Crypto()
        {
            //switch (NetSelected)
            //{
            //    case SymmProvEnum.DES:
            //        mobjCryptoService = new DESCryptoServiceProvider();
            //        break;
            //    case SymmProvEnum.RC2:
            //        mobjCryptoService = new RC2CryptoServiceProvider();
            //        break;
            //    case SymmProvEnum.Rijndael:
            //        mobjCryptoService = new RijndaelManaged();
            //        break;
            //}
            mobjCryptoService = new RijndaelManaged();
        }

        /// <remarks>
        /// Constructor for using a customized SymmetricAlgorithm class.
        /// </remarks>
        //public Crypto(SymmetricAlgorithm ServiceProvider)
        //{
        //    mobjCryptoService = ServiceProvider;
        //}

        /// <remarks>
        /// Depending on the legal key size limitations of a specific CryptoService provider
        /// and length of the private key provided, padding the secret key with space character
        /// to meet the legal size of the algorithm.
        /// </remarks>
        private byte[] GetLegalKey(string Key)
        {
            string key = Globalkey;
            string sTemp;
            if (mobjCryptoService.LegalKeySizes.Length > 0)
            {
                int lessSize = 0, moreSize = mobjCryptoService.LegalKeySizes[0].MinSize;
                // key sizes are in bits
                while (Key.Length * 8 > moreSize)
                {
                    lessSize = moreSize;
                    moreSize += mobjCryptoService.LegalKeySizes[0].SkipSize;
                }
                sTemp = Key.PadRight(moreSize / 8, ' ');
            }
            else
                sTemp = Key;

            // convert the secret key to byte array
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }

        public string Encrypt(string Source)
        {
            string Key = Globalkey;
            byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
            // create a MemoryStream so that the process can be done without I/O files
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            byte[] bytKey = GetLegalKey(Key);

            // set the private key
            mobjCryptoService.Key = bytKey;
            mobjCryptoService.IV = bytKey;

            // create an Encryptor from the Provider Service instance
            ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();

            // create Crypto Stream that transforms a stream using the encryption
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);

            // write out encrypted content into MemoryStream
            cs.Write(bytIn, 0, bytIn.Length);
            cs.FlushFinalBlock();

            // get the output and trim the '\0' bytes
            byte[] bytOut = ms.GetBuffer();
            int i = 0;
            for (i = 0; i < bytOut.Length; i++)
                if (bytOut[i] == 0)
                    break;

            // convert into Base64 so that the result can be used in xml
            return System.Convert.ToBase64String(bytOut, 0, i);
        }

        public string Decrypt(string Source)
        {
            string Key = Globalkey;
            // convert from Base64 to binary
            byte[] bytIn = System.Convert.FromBase64String(Source);
            // create a MemoryStream with the input
            System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);

            byte[] bytKey = GetLegalKey(Key);

            // set the private key
            mobjCryptoService.Key = bytKey;
            mobjCryptoService.IV = bytKey;

            // create a Decryptor from the Provider Service instance
            ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();

            // create Crypto Stream that transforms a stream using the decryption
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);

            // read out the result from the Crypto Stream
            System.IO.StreamReader sr = new System.IO.StreamReader( cs );
            return sr.ReadToEnd();
        }
}
4

2 回答 2

2

我写了一个加密类,它应该与你的代码一起工作,给定正确的配置。

你可以在这里查看:https ://github.com/Untouchab1e/yubinotes/blob/master/Android/src/com/connectutb/yubinotes/util/Crypto.java

您在示例中使用的是 DES,这是一种有缺陷且过时的算法(或者使用 3DES),因此我建议转向提供更好加密的 AES。

于 2013-10-07T11:25:14.940 回答
-1

Android 和 C# 支持 AES。

这是一个使用 AES 的加密示例:

SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encrypted = cipher.doFinal(clearTextBytes);

以下是有关 Android Cipher 类的更多文档:http: //developer.android.com/reference/javax/crypto/Cipher.html

希望这可以帮助 :)

于 2013-10-07T11:24:42.987 回答