码迷,mamicode.com
首页 > 移动开发 > 详细

android和.net webservice中的DES加密算法

时间:2017-07-31 18:47:09      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:form   append   each   val   text   method   []   plain   bst   

 

也是看了一堆的例子,本身并不会写加密算法,好在只要会用就行了,我们把在app中使用的参数加密,然后在.net端的webservice中进行解密,本身并没有什么问题,但是android下和.net下的des加密算法有些不同,写下供以后使用.

android端的DES.

public class DES {
    private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
    //加密.
    public static String encryptDES(String encryptString, String encryptKey)
            throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        DESKeySpec desKeySpec = new DESKeySpec(encryptKey.getBytes("UTF-8"));

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(encryptKey.getBytes("UTF-8"));
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        return toHexString(cipher.doFinal(encryptString.getBytes("UTF-8")));

    }

    //解密.
    public static String decryptDES(String decryptString, String decryptKey)
            throws Exception {
        byte[] bytesrc =convertHexString(decryptString);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(decryptKey.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(decryptKey.getBytes("UTF-8"));

        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);

        byte[] retByte = cipher.doFinal(bytesrc);
        return new String(retByte);
    }

    public static String myKey(){  //这个可以不要,我是为了方便,获得加密解密的key
        return "12345678";
    }

    public static String toHexString(byte b[]) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }

        return hexString.toString();
    }

    public static byte[] convertHexString(String ss)
    {
        byte digest[] = new byte[ss.length() / 2];
        for(int i = 0; i < digest.length; i++)
        {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte)byteValue;
        }

        return digest;
    }
}

  

.net端的.

 Private sKey As String = "12345678"
    ‘====================说明=========================
    ‘开始于2017-6-15,用于测试android studio调用webservice的例子,用完可以删除....
    ‘=================================================
    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "测试成功,用于返回一个有用的结果值!"
    End Function

    ‘‘‘ <summary>
    ‘‘‘ DES加密算法
    ‘‘‘ </summary>
    ‘‘‘ <param name="pToEncrypt"></param>
    ‘‘‘ <param name="sKey"></param>
    ‘‘‘ <returns></returns>
    ‘‘‘ <remarks></remarks>

    Public Function Encrypt(ByVal pToEncrypt As String, ByVal sKey As String) As String
        Dim des As New DESCryptoServiceProvider()
        Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(pToEncrypt)

        des.Key = UTF8Encoding.UTF8.GetBytes(sKey)
        des.IV = UTF8Encoding.UTF8.GetBytes(sKey)
        Dim ms As New MemoryStream()
        Dim cs As New CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)

        cs.Write(inputByteArray, 0, inputByteArray.Length)
        cs.FlushFinalBlock()

        Dim ret As New StringBuilder()
        For Each b As Byte In ms.ToArray()
            ret.AppendFormat("{0:X2}", b)
        Next b
        ret.ToString()
        Return ret.ToString()
    End Function

    ‘‘‘ <summary>
    ‘‘‘ DES解密算法.
    ‘‘‘ </summary>
    ‘‘‘ <param name="pToDecrypt"></param>
    ‘‘‘ <param name="sKey"></param>
    ‘‘‘ <returns></returns>
    ‘‘‘ <remarks></remarks>
    Public Function Decrypt(ByVal pToDecrypt As String, ByVal sKey As String) As String
        Dim des As New DESCryptoServiceProvider()

        Dim inputByteArray((pToDecrypt.Length \ 2) - 1) As Byte
        For x As Integer = 0 To (pToDecrypt.Length \ 2) - 1
            Dim i As Integer = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16))
            inputByteArray(x) = CByte(i)
        Next x

        des.Key = UTF8Encoding.UTF8.GetBytes(sKey)
        des.IV = UTF8Encoding.UTF8.GetBytes(sKey)
        Dim ms As New MemoryStream()
        Dim cs As New CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)
        cs.Write(inputByteArray, 0, inputByteArray.Length)
        cs.FlushFinalBlock()
        Dim ret As New StringBuilder()
        Return Encoding.UTF8.GetString(ms.ToArray())
    End Function

  

经过使用,发现没有任何问题,可以相互加解密.

android和.net webservice中的DES加密算法

标签:form   append   each   val   text   method   []   plain   bst   

原文地址:http://www.cnblogs.com/wjbych/p/7264350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!