数据加密通常分为两种,即对称加密和非对称加密。
对称加密:
所谓对称,就是采用同一个密钥同时用作信息的加密和解密,加密的双方使用方式用同样的密钥进行加密和解密。由于其速度,对称性加密通常在消息发送 方需要加密大量数据时使用。密钥是控制加密及解密过程的指令。算法是一组规则,规定如何进行加密和解密。对称式加密本身不是安全的。
常用的对称加密有:DES、IDEA、RC2、RC4、SKIPJACK、RC5、AES算法等。
非对称加密:
编写一对唯一对应的密钥:公开密钥(简称公钥)和私人密钥(简称私钥),公钥对外公开,私钥由个人秘密保存;用其中一把密钥加密,就只能用另一把 密钥解密。非对称密钥加密算法的典型代表是RSA。
因为公钥是公开对外发布的,所以想给私钥持有者发送信息的人都可以取得公钥,用公钥加密后,发送给私钥持有者,即使被拦截或窃取,没有私钥的攻 击 者也无法获得加密后的信息,可以保证信息的安全传输;另外,先用私钥加密,再用公钥解密,可以完成对私钥持有者的身份认证,因为公钥只能解开有私 钥加密后的信息。虽然公钥和私钥是一对互相关联的密钥,但是并不能从两者中的任何一把,推断出另一把。
由于公钥是公开的,而私钥则由用户自己保存,所以对于非对称密钥,其保密管理相对比较简单;但是。因为复杂的加密算法,使的非对称密钥加密速度 慢,成本高。
支付宝用的就是非对称加密,所以安全性很高。
下面具体看个实现:
转载自:http://blog.csdn.net/mengxiangyue/article/details/40015727
IOS,需要引入GTMBase64.h、GTMBase64.m、GTMDefines.h,这个github上面有我,自己搜搜吧,还有<CommonCrypto/CommonCryptor.h>。
-
#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];
-
-
}
-
-
-
-
-
-
-
- (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.jar
-
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));
-
-
}
-
-
-
-
-
-
-
-
-
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;
-
}
-
-
-
-
-
-
-
-
-
-
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);
-
}
-
-
-
-
-
-
-
-
-
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
-
-
SecureRandom sr = new SecureRandom();
-
-
-
DESKeySpec dks = new DESKeySpec(key);
-
-
-
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
-
SecretKey securekey = keyFactory.generateSecret(dks);
-
-
-
Cipher cipher = Cipher.getInstance(DES);
-
-
-
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
-
-
return cipher.doFinal(data);
-
}
-
-
-
-
-
-
-
-
-
-
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
-
-
SecureRandom sr = new SecureRandom();
-
-
-
DESKeySpec dks = new DESKeySpec(key);
-
-
-
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
-
SecretKey securekey = keyFactory.generateSecret(dks);
-
-
-
Cipher cipher = Cipher.getInstance(DES);
-
-
-
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
-
-
return cipher.doFinal(data);
-
}
-
}
工程下载:http://download.csdn.net/download/mengxiangyue/8028311
数据加密
原文:http://blog.csdn.net/hnjyzqq/article/details/43128055