android和.net webservice中的DES加密算法
阿新 • • 發佈:2017-07-31
brush row world parseint 加密算法 發現 mark odin 參數
也是看了一堆的例子,本身並不會寫加密算法,好在只要會用就行了,我們把在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加密算法