Des encryption algorithms in Android and. NET WebService

Source: Internet
Author: User

Also looked at a bunch of examples, itself does not write encryption algorithm, fortunately, as long as it will be used on the line, we put the parameters used in the app encryption, and then decryption in the. NET side of the webservice, there is no problem in itself, but the DES encryption algorithms under Android and. NET are somewhat different , write down for later use.

Des on the Android side.

public class DES {private static byte[] IV = {1, 2, 3, 4, 5, 6, 7, 8};    Encryption. public static string Encryptdes (String encryptstring, String encryptkey) throws Exception {Cipher Ciphe        r = 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"));    }//decryption. public static string Decryptdes (String decryptstring, String decryptkey) throws Exception {byte[] bytes        RC =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 () {//This can not, I am for convenience, get encrypted decryption 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-side.

 Private SKey as String = "12345678" ' ==================== description ========================= ' started in 2017-6-15 for testing Android Stud    IO Call WebService example, run out can delete ....        ' ================================================= <webmethod () > _ Public Function HelloWorld () as String    Return "Test succeeded, used to return a useful result value!" End Function ' <summary> ' des encryption algorithm ' </summary> ' ' <param name= ' Ptoencrypt ' ></para M> "<param name=" SKey "></param>" <returns></returns> "<remarks></rem Arks> public Function Encrypt (ByVal ptoencrypt As String, ByVal SKey as String) As String Dim des as New DESC Ryptoserviceprovider () 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 decryption algorithm.    "</summary>" <param name= "Ptodecrypt" ></param> "<param name=" SKey "></param> ' <returns></returns> ' <remarks></remarks> public Function Decrypt (ByVal ptodecrypt A s string, ByVal SKey as String) As String Dim des as New DESCryptoServiceProvider () Dim Inputbytearray (Ptod Ecrypt. 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)) 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

  

After use, found that there are no problems, can be added to each other decryption.

Des encryption algorithms in Android and. NET WebService

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.