Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the consistent encryption method for Android, iPhone and Java?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what is the consistent encryption method for Android, iPhone and Java. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

I have been working on Android before, but I am going to develop an iPhone client recently. The most troublesome question encountered is the inconsistent encryption and decryption of Java, Android and iPhone. Because the mobile phone backstage usually uses Web Service,Android and iPhone clients developed by JAVA to call the same Web Service interface, for the sake of data security, it is necessary to encrypt the data. The headache comes, it is difficult to write a set of encryption programs, encryption and decryption results are the same among the three platforms, can not write a set of Web Service interfaces for Android and iPhone clients, right? I believe there will be many friends confused about this, here to share a set of 3DES encryption procedures, can achieve Java, Android and iPhone platform encryption and decryption.

The first is the encryption tool class on the Java side, which is also applicable to the Android side. Without any modification, it can ensure that the encryption and decryption of Java and Android are the same, and the Chinese characters will not be garbled.

Double-click the code to select all

Package org.liuyq.des3; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec / * 3DES encryption tool class * * @ author liufeng * @ date 2012-10-11 * / public class Des3 {/ / key private final static String secretKey = "liuyunqiang@lx100 $# 365encryption $"; / / Vector private final static String iv = "01234567"; / / uniform encryption and decryption private final static String encoding = "utf-8" / * * 3DES encryption * * @ param plainText plain text * @ return * @ throws Exception * / public static String encode (String plainText) throws Exception {Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec (secretKey.getBytes ()); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance ("desede") Deskey = keyfactory.generateSecret (spec); Cipher cipher = Cipher.getInstance ("desede/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec (iv.getBytes ()); cipher.init (Cipher.ENCRYPT_MODE, deskey, ips); byte [] encryptData = cipher.doFinal (plainText.getBytes (encoding)); return Base64.encode (encryptData) } / * * 3DES decryption * * @ param encryptText encrypted text * @ return * @ throws Exception * / public static String decode (String encryptText) throws Exception {Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec (secretKey.getBytes ()); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance ("desede") Deskey = keyfactory.generateSecret (spec); Cipher cipher = Cipher.getInstance ("desede/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec (iv.getBytes ()); cipher.init (Cipher.DECRYPT_MODE, deskey, ips); byte [] decryptData = cipher.doFinal (Base64.decode (encryptText)); return new String (decryptData, encoding);}}

The above encryption utility class uses the Base64 class, whose source code is as follows:

Double-click the code to select all

Package org.liuyq.des3; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; / * Base64 coding tool class * * @ author liufeng * @ date 2012-10-11 * / public class Base64 {private static final char [] legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" .toCharArray (); public static String encode (byte [] data) {int start = 0 Int len = data.length; StringBuffer buf = new StringBuffer (data.length * 3 / 2); int end = len-3; int I = start; int n = 0; while (I > 12) & 63]); buf.append (legalChars [(d > 6) & 63]); buf.append (legalChars [d & 63]) I + = 3; if (nasty + > = 14) {n = 0; buf.append ("") }} if (I = = start + len-2) {int d = (int) data [I]) & 0x0ff) 18) & 63]); buf.append (legalChars [(d > 12) & 63]); buf.append (legalChars [(d > > 6) & 63]) Buf.append ("=");} else if (I = = start + len-1) {int d = (int) data [I]) & 0x0ff) > 18) & 63]); buf.append (legalChars [(d > 12) & 63]); buf.append ("= =") } return buf.toString ();} private static int decode (char c) {if (c > ='A'& & c ='a'& & c ='0' & & c

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report