image加密【Java版】
private static final char[] LEGAL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
public static String encode(byte[] data) {
int start = 0;
int len = data.length;
StringBuilder buf = new StringBuilder(data.length * 3 / 2);
int end = len - 3;
int i = start;
int n = 0;
while (i <= end) {
int d = ((((int) data[i]) & 0x0ff) << 16)
| ((((int) data[i + 1]) & 0x0ff) << 8)
| (((int) data[i + 2]) & 0x0ff);
buf.append(LEGAL_CHARS[(d >> 18) & 63]);
buf.append(LEGAL_CHARS[(d >> 12) & 63]);
buf.append(LEGAL_CHARS[(d >> 6) & 63]);
buf.append(LEGAL_CHARS[d & 63]);
i += 3;
if (n++ >= 14) {
n = 0;
buf.append(" ");
}
}
if (i == start + len - 2) {
int d = ((((int) data[i]) & 0x0ff) << 16)
| ((((int) data[i + 1]) & 255) << 8);
buf.append(LEGAL_CHARS[(d >> 18) & 63]);
buf.append(LEGAL_CHARS[(d >> 12) & 63]);
buf.append(LEGAL_CHARS[(d >> 6) & 63]);
buf.append("=");
} else if (i == start + len - 1) {
int d = (((int) data[i]) & 0x0ff) << 16;
buf.append(LEGAL_CHARS[(d >> 18) & 63]);
buf.append(LEGAL_CHARS[(d >> 12) & 63]);
buf.append("==");
}
return buf.toString();
}
image加密【IOS版】
NSData *data = [UIImageJPEGRepresentation(image, 1) base64EncodedDataWithOptions:0];
NSString *base64 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
image加密【Android版】
public static String fileToBase64(File file) {
String base64 = null;
InputStream in = null;
try {
in = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
in.read(bytes);
base64 = Base64.encodeToString(bytes, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64;
}
身份证姓名和身份证号加密【Java版】
private static final String ALGORITHM = "RSA";
private static final String CIPHER_EN = "RSA";
private static final int MAX_ENCRYPT_BLOCK = 117;
public static String encryptByPublicKey(String fromStr, String publicKey) throws Exception {
return new String(Base64.getEncoder().encode(encryptByPublicKey(fromStr.getBytes(), publicKey)));
}
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
byte[] keyBytes = Base64.getDecoder().decode(publicKey.getBytes());
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
Key key = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance(CIPHER_EN);
cipher.init(Cipher.ENCRYPT_MODE, key);
int inputLength = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
while (inputLength - offset > 0) {
if (inputLength - offset > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data, offset, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data, offset, inputLength - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return encryptedData;
}
身份证姓名和身份证号加密【IOS版】
+(NSString *)encryptString:(NSString *)
str publicKey:(NSString *)
pubKey {
@synchronized (self) {
NSData * data = [CLCoreEncyption encryptData:[str dataUsingEncoding:NSUTF8StringEncoding]publicKey:
pubKey];
NSString * ret = CLCoreBase64_encode_data(data);
return ret;
}
}
+(NSData *)encryptData:(NSData *)
data publicKey:(NSString *)
pubKey {
if (!data || !pubKey) {
return nil;
}
SecKeyRef keyRef = [CLCoreEncyption addPublicKey:pubKey];
if (!keyRef) {
return [NSData data];
}
return [CLCoreEncyption encryptData:data withKeyRef:keyRef isSign:NO];
}
static NSString *
CLCoreBase64_encode_data(NSData *data) {
data = [data base64EncodedDataWithOptions:0];
NSString * ret = [[NSString alloc]initWithData:
data encoding:NSUTF8StringEncoding];
return ret;
}
+(SecKeyRef)addPublicKey:(NSString *)
key {
NSRange spos = [key rangeOfString:@ "-----BEGIN PUBLIC KEY-----"];
NSRange epos = [key rangeOfString:@ "-----END PUBLIC KEY-----"];
if (spos.location != NSNotFound && epos.location != NSNotFound) {
NSUInteger s = spos.location + spos.length;
NSUInteger e = epos.location;
NSRange range = NSMakeRange(s, e - s);
key = [key substringWithRange:range];
}
key = [key stringByReplacingOccurrencesOfString:@ "\r" withString:
@ ""];
key = [key stringByReplacingOccurrencesOfString:@ "\n" withString:
@ ""];
key = [key stringByReplacingOccurrencesOfString:@ "\t" withString:
@ ""];
key = [key stringByReplacingOccurrencesOfString:@ " " withString:
@ ""];
NSData * data = base64_decode(key);
data = [CLCoreEncyption stripPublicKeyHeader:data];
if (!data) {
return nil;
}
NSString * tag = @ "CL_CORE_SDK_RSA_PUBLIC_KEY";
NSData * d_tag = [NSData dataWithBytes:[tag UTF8String]length:[tag length]];
NSMutableDictionary * publicKey = [[NSMutableDictionary alloc]init];
[publicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass];
[publicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
[publicKey setObject:d_tag forKey:(__bridge id)kSecAttrApplicationTag];
SecItemDelete((__bridge CFDictionaryRef) publicKey);
[publicKey setObject:data forKey:(__bridge id)kSecValueData];
[publicKey setObject:(__bridge id)kSecAttrKeyClassPublic forKey:(__bridge id)
kSecAttrKeyClass];
[publicKey setObject:[NSNumber numberWithBool:YES]forKey:
(__bridge id)
kSecReturnPersistentRef];
CFTypeRef persistKey = nil;
OSStatus status = SecItemAdd((__bridge CFDictionaryRef) publicKey, &persistKey);
if (persistKey != nil) {
CFRelease(persistKey);
}
if ((status != noErr) && (status != errSecDuplicateItem)) {
return nil;
}
[publicKey removeObjectForKey:(__bridge id)kSecValueData];
[publicKey removeObjectForKey:(__bridge id)kSecReturnPersistentRef];
[publicKey setObject:[NSNumber numberWithBool:YES]forKey:
(__bridge id)kSecReturnRef];
[publicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
SecKeyRef keyRef = nil;
status = SecItemCopyMatching((__bridge CFDictionaryRef) publicKey, (CFTypeRef *) & keyRef);
if (status != noErr) {
return nil;
}
return keyRef;
}
+(NSData *)encryptData:(NSData *)
data withKeyRef:(SecKeyRef)
keyRef isSign:(BOOL)
isSign {
const uint8_t * srcbuf = (const uint8_t *)[data bytes];
size_t srclen = (size_t) data.length;
size_t block_size = SecKeyGetBlockSize(keyRef) * sizeof(uint8_t);
void *outbuf = malloc(block_size);
size_t src_block_size = block_size - 11;
NSMutableData * ret = [[NSMutableData alloc]init];
for (int idx = 0; idx < srclen; idx += src_block_size) {
size_t data_len = srclen - idx;
if (data_len > src_block_size) {
data_len = src_block_size;
}
size_t outlen = block_size;
OSStatus status = noErr;
if (isSign) {
status = SecKeyRawSign(keyRef,
kSecPaddingPKCS1,
srcbuf + idx,
data_len,
outbuf,
& outlen
);
} else {
status = SecKeyEncrypt(keyRef,
kSecPaddingPKCS1,
srcbuf + idx,
data_len,
outbuf,
& outlen
);
}
if (status != 0) {
ret = nil;
break;
} else {
[ret appendBytes:outbuf length:outlen];
}
}
free(outbuf);
CFRelease(keyRef);
return ret;
}
[
CLCoreEncyption encryptString:
name publicKey:rsa_pbuKey]forKey:
@"name"];
[
CLCoreEncyption encryptString:
idNumber publicKey:rsa_pbuKey]forKey:
@"id_num"];
身份证姓名和身份证号加密【Android版】
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher;
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
int inputLen = data.getBytes().length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return new String(Base64.encode(encryptedData, Base64.NO_WRAP));
}
public static PublicKey getPublicKey(String publicKey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] decodedKey = Base64.decode(publicKey.getBytes(), Base64.DEFAULT);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
return keyFactory.generatePublic(keySpec);
}
uuid加密【Java版】
import cn.hutool.core.codec.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
private final staticString secretKey =“请向运营人员申请”;
private final staticString iv =“12345678”;
private final staticString encoding =“utf-8”;
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);
}
uuid加密【IOS版】
+(NSString*)encrypt:(NSString*)
plainText {
NSData * data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
size_t plainTextBufferSize = [data length];
const void *vplainText = (const void *)[data bytes];
CCCryptorStatus ccStatus;
uint8_t * bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
bufferPtr = malloc(bufferPtrSize * sizeof(uint8_t));
memset(( void *)bufferPtr, 0x0, bufferPtrSize);
const void *vkey = (const void *) [gkey UTF8String];
const void *vinitVec = (const void *) [gIv UTF8String];
ccStatus = CCCrypt(kCCEncrypt,
kCCAlgorithm3DES,
kCCOptionPKCS7Padding,
vkey,
kCCKeySize3DES,
vinitVec,
vplainText,
plainTextBufferSize,
( void *)bufferPtr,
bufferPtrSize,
&movedBytes);
NSData * myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger) movedBytes];
NSString * result = [CLBase64 base64EncodedStringFrom:myData];
return result;
}
static const charencodingTable[] =“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;
+(NSString *)base64EncodedStringFrom:(NSData *)
data {
if ([data length] ==0)
return @ "";
char *characters = malloc((([data length]+2) /3) *4);
if (characters == NULL)
return nil;
NSUInteger length = 0;
NSUInteger i = 0;
while (i < [data length]){
char buffer[ 3] ={
0, 0, 0
} ;
short bufferLength = 0;
while (bufferLength < 3 && i < [data length])
buffer[bufferLength++] = (( char *)[data bytes])[i++];
characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2];
characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)];
if (bufferLength > 1)
characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)];
else characters[length++] = '=';
if (bufferLength > 2)
characters[length++] = encodingTable[buffer[2] & 0x3F];
else characters[length++] = '=';
}
return [[NSString alloc]initWithBytesNoCopy:
characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];
}
NSString *str =[
CLDES3EncryptUtil encrypt:uuid];
uuid加密【Android版】
private final static String SECRET_KEY = "请向运营人员申请";
private final static String IV = "12345678";
private final static String ENCODING = "utf-8";
public static String encode(String plainText) throws Exception {
Key desKey = null;
DESedeKeySpec spec = new DESedeKeySpec(SECRET_KEY.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.encodeToString(encryptData, Base64.DEFAULT);
}