最近在考慮數據加密方面的需求,因此對數據加密簡單的看了一下,固然不是看的原理,只是看看怎麼可以實現。如今咱們須要實現的是移動端和後臺(java)數據加解密的配合,開始的時候考慮的使用RSA,由於RSA是非對稱加密,更加安全點,可是RSA加密的過程當中,ios公鑰加密的數據,後臺java是可以解密成功,可是後臺java私鑰加密的東西,前端ios,就沒有解密成功,實驗了不少方法,最終也沒有成功,因此就放棄了,轉向了安全性差一點的DES加密。前端
對於DES、RSA的介紹,本身百度去吧,由於我也說不明白。(上面我沒有提Android,由於Android使用的是java,因此應該跟後臺一致)java
下面廢話很少說,直接貼上代碼:ios
IOS,須要引入GTMBase64.h、GTMBase64.m、GTMDefines.h,這個github上面有我,本身搜搜吧,還有<CommonCrypto/CommonCryptor.h>。git
[objc] view plaincopy #import "ViewController.h" #import <CommonCrypto/CommonCryptor.h> #import "GTMBase64.h" @interface ViewController () @end @implementation ViewController -(void)viewDidLoad { [super viewDidLoad]; NSString *key = @"這是個key"; NSString *encryptedData = [self encryptUseDES:@"this is a text" key:key]; NSLog(@"加密後的數據是:%@", encryptedData); NSLog(@"解密後的數據是:%@", [self decryptUseDES:encryptedData key:key]); } -(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /*字符串加密 *參數 *plainText : 加密明文 *key : 密鑰 64位 */ -(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key { NSString *ciphertext = nil; const charchar *textBytes = [plainText UTF8String]; NSUInteger dataLength = [plainText length]; unsigned char buffer[1024]; memset(buffer, 0, sizeof(char)); Byte iv[] = {1,2,3,4,5,6,7,8}; size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding, [key UTF8String], kCCKeySizeDES, iv, textBytes, dataLength, buffer, 1024, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted]; ciphertext = [[NSString alloc] initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding]; } return ciphertext; } //解密 -(NSString *) decryptUseDES:(NSString*)cipherText key:(NSString*)key { NSData* cipherData = [GTMBase64 decodeString:cipherText]; unsigned char buffer[1024]; memset(buffer, 0, sizeof(char)); size_t numBytesDecrypted = 0; Byte iv[] = {1,2,3,4,5,6,7,8}; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding, [key UTF8String], kCCKeySizeDES, iv, [cipherData bytes], [cipherData length], buffer, 1024, &numBytesDecrypted); NSString* plainText = nil; if (cryptStatus == kCCSuccess) { NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted]; plainText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; } return plainText; } @end
java 代碼:須要本身引入sun.misc.BASE64Decoder.jargithub
[java] view plaincopy package com.yue; import java.io.IOException; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import Decoder.BASE64Decoder; import Decoder.BASE64Encoder; public class DesUtil { private final static String DES = "DES"; public static void main(String[] args) throws Exception { String data = "123 456"; String key = "abcdefgh"; System.err.println(encrypt(data, key)); System.err.println(decrypt(encrypt(data, key), key)); System.out.println(decrypt("JF5dX/TlOg529KAhh+vywjzIp5Msktmf", key)); } /** * Description 根據鍵值進行加密 * @param data * @param key 加密鍵byte數組 * @return * @throws Exception */ public static String encrypt(String data, String key) throws Exception { byte[] bt = encrypt(data.getBytes(), key.getBytes()); String strs = new BASE64Encoder().encode(bt); return strs; } /** * Description 根據鍵值進行解密 * @param data * @param key 加密鍵byte數組 * @return * @throws IOException * @throws Exception */ public static String decrypt(String data, String key) throws IOException, Exception { if (data == null) return null; BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf,key.getBytes()); return new String(bt); } /** * Description 根據鍵值進行加密 * @param data * @param key 加密鍵byte數組 * @return * @throws Exception */ private static byte[] encrypt(byte[] data, byte[] key) throws Exception { // 生成一個可信任的隨機數源 SecureRandom sr = new SecureRandom(); // 從原始密鑰數據建立DESKeySpec對象 DESKeySpec dks = new DESKeySpec(key); // 建立一個密鑰工廠,而後用它把DESKeySpec轉換成SecretKey對象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher對象實際完成加密操做 Cipher cipher = Cipher.getInstance(DES); // 用密鑰初始化Cipher對象 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); } /** * Description 根據鍵值進行解密 * @param data * @param key 加密鍵byte數組 * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws Exception { // 生成一個可信任的隨機數源 SecureRandom sr = new SecureRandom(); // 從原始密鑰數據建立DESKeySpec對象 DESKeySpec dks = new DESKeySpec(key); // 建立一個密鑰工廠,而後用它把DESKeySpec轉換成SecretKey對象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher對象實際完成解密操做 Cipher cipher = Cipher.getInstance(DES); // 用密鑰初始化Cipher對象 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); } }
代碼下載地址:http://download.csdn.net/detail/mengxiangyue/8028311數組