最近在做支付项目,调接口调了一个多月,也蛋疼了一个多月。。。下面是积累的各种加密算法
JAVA:
签名方法:BASE64(RSA(MD5(原文)))
// MD5
String plainText = "XXXXXXXXXXXXXXX";
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(plainText.getBytes("utf-8"));
byte[] digestBytes = md5.digest();
// RSA
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, prikey);
byte[] rsaBytes = cipher.doFinal(digestBytes);
// BASE64
String sign = Base64.encode(rsaBytes);
IOS:
#pragma mark - 3DES
+ (NSData*)en3DESData:(NSData*)plainData withKeyData:(NSData*)keyData
{
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
uint8_t iv[kCCBlockSize3DES];
memset((void *) iv, 0x0, (size_t) sizeof(iv));
bufferPtrSize = ([plainData length] + kCCBlockSize3DES) & ~(kCCBlockSize3DES -1);
bufferPtr = malloc(bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x00, bufferPtrSize);
ccStatus = CCCrypt(kCCEncrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
[keyData bytes],
[keyData length],
iv,
[plainData bytes],
[plainData length],
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
NSLog(@"CCCryptorStatus is:%d",ccStatus);
return [NSData dataWithBytes:bufferPtr length:movedBytes];
}
+ (NSData*)de3DESData:(NSData*)encryptData withKeyData:(NSData*)keyData
{
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
uint8_t iv[kCCBlockSize3DES];
memset((void *) iv, 0x0, (size_t) sizeof(iv));
bufferPtrSize = ([encryptData length] + kCCBlockSize3DES) & ~(kCCBlockSize3DES -1);
bufferPtr = malloc(bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x00, bufferPtrSize);
ccStatus = CCCrypt(kCCDecrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
[keyData bytes],
[keyData length],
iv,
[encryptData bytes],
[encryptData length],
(void *)bufferPtr,
bufferPtrSize,
&movedBytes);
NSLog(@"CCCryptorStatus is:%d",ccStatus);
return [NSData dataWithBytes:bufferPtr length:movedBytes];
}