android上傳圖片base64
⑴ AndroidKeyStore Cipher簡單使用
Android開發中,敏感數據存儲需求提升,Android提供了Cipher加密方式,確保數據安全性。
初始化Cipher生成iv,隨機數組作為解密鑰匙,每次init確保iv獨特性,增加私密性。
獲取Cipher的SecretKey,為初始化後的Cipher提供密鑰支持。
實現加密功能,針對json數據加密,iv數組進行Base64加密保存,增加數據安全。
解密過程,確保數據完整性。
加密數據保存到SharePerference,提供額外安全保障,相比於早期MD5、AES、DES等固定密鑰方法,Cipher方式更簡潔方便。
適合session、token、用戶信息等敏感數據加密,確保數據在手機存儲過程中的安全。
建議開發者在實際應用中參考此方法,提升數據存儲安全水平。
⑵ 跪求 DES跨(C# Android IOS)三個平台通用的加解密方法
#region跨平台加解密(c#安卓IOS)
//publicstaticstringsKey="12345678";
/////<summary>
/////解密
/////</summary>
/////<paramname="pToDecrypt">要解密的以Base64</param>
/////<paramname="sKey">密鑰,且必須為8位</param>
/////<returns>已解密的字元串</returns>
//publicstaticstringDesDecrypt(stringpToDecrypt)
//{
////轉義特殊字元
//pToDecrypt=pToDecrypt.Replace("-","+");
//pToDecrypt=pToDecrypt.Replace("_","/");
//pToDecrypt=pToDecrypt.Replace("~","=");
//byte[]inputByteArray=Convert.FromBase64String(pToDecrypt);
//using(DESCryptoServiceProviderdes=newDESCryptoServiceProvider())
//{
//des.Key=ASCIIEncoding.ASCII.GetBytes(sKey);
//des.IV=ASCIIEncoding.ASCII.GetBytes(sKey);
//System.IO.MemoryStreamms=newSystem.IO.MemoryStream();
//using(CryptoStreamcs=newCryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write))
//{
//cs.Write(inputByteArray,0,inputByteArray.Length);
//cs.FlushFinalBlock();
//cs.Close();
//}
//stringstr=Encoding.UTF8.GetString(ms.ToArray());
//ms.Close();
//returnstr;
//}
//}
/////<summary>
/////對字元串進行DES加密
/////</summary>
/////<paramname="sourceString">待加密的字元串</param>
/////<returns>加密後的BASE64編碼的字元串</returns>
//publicstringEncrypt(stringsourceString)
//{
//byte[]btKey=Encoding.UTF8.GetBytes(sKey);
//byte[]btIV=Encoding.UTF8.GetBytes(sKey);
//DESCryptoServiceProviderdes=newDESCryptoServiceProvider();
//using(MemoryStreamms=newMemoryStream())
//{
//byte[]inData=Encoding.UTF8.GetBytes(sourceString);
//try
//{
//using(CryptoStreamcs=newCryptoStream(ms,des.CreateEncryptor(btKey,btIV),CryptoStreamMode.Write))
//{
//cs.Write(inData,0,inData.Length);
//cs.FlushFinalBlock();
//}
//returnConvert.ToBase64String(ms.ToArray());
//}
//catch
//{
//throw;
//}
//}
//}
#endregion
安卓---------------------------------------------------------------------------
////加密
//(Stringmessage,Stringkey)
//throwsException{
//byte[]bytesrc=Base64.decode(message.getBytes(),Base64.DEFAULT);
//Ciphercipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
//DESKeySpecdesKeySpec=newDESKeySpec(key.getBytes("UTF-8"));
//SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance("DES");
//SecretKeysecretKey=keyFactory.generateSecret(desKeySpec);
//IvParameterSpeciv=newIvParameterSpec(key.getBytes("UTF-8"));
//cipher.init(Cipher.DECRYPT_MODE,secretKey,iv);
//byte[]retByte=cipher.doFinal(bytesrc);
//returnnewString(retByte);
//}
////解密
//(Stringmessage,Stringkey)
//throwsException{
//Ciphercipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
//DESKeySpecdesKeySpec=newDESKeySpec(key.getBytes("UTF-8"));
//SecretKeyFactorykeyFactory=SecretKeyFactory.getInstance("DES");
//SecretKeysecretKey=keyFactory.generateSecret(desKeySpec);
//IvParameterSpeciv=newIvParameterSpec(key.getBytes("UTF-8"));
//cipher.init(Cipher.ENCRYPT_MODE,secretKey,iv);
//byte[]encryptbyte=cipher.doFinal(message.getBytes());
//returnnewString(Base64.encode(encryptbyte,Base64.DEFAULT));
//}
Ios--------------------------------------------------------------------------------------------------------------------
staticconstchar*encryptWithKeyAndType(constchar*text,CCOperationencryptOperation,char*key)
{
NSString*textString=[[NSStringalloc]initWithCString:textencoding:NSUTF8StringEncoding];
//NSLog(@"[[item.urldescription]UTF8String=%@",textString);
constvoid*dataIn;
size_tdataInLength;
if(encryptOperation==kCCDecrypt)//傳遞過來的是decrypt解碼
{
//解碼base64
NSData*decryptData=[GTMBase64decodeData:[textStringdataUsingEncoding:NSUTF8StringEncoding]];//轉成utf-8並decode
dataInLength=[decryptDatalength];
dataIn=[decryptDatabytes];
}
else//encrypt
{
NSData*encryptData=[textStringdataUsingEncoding:NSUTF8StringEncoding];
dataInLength=[encryptDatalength];
dataIn=(constvoid*)[encryptDatabytes];
}
CCCryptorStatusccStatus;
uint8_t*dataOut=NULL;//可以理解位type/typedef的縮寫(有效的維護了代碼,比如:一個人用int,一個人用long。最好用typedef來定義)
size_tdataOutAvailable=0;//size_t是操作符sizeof返回的結果類型
size_tdataOutMoved=0;
dataOutAvailable=(dataInLength+kCCBlockSizeDES)&~(kCCBlockSizeDES-1);
dataOut=malloc(dataOutAvailable*sizeof(uint8_t));
memset((void*)dataOut,00,dataOutAvailable);//將已開辟內存空間buffer的首1個位元組的值設為值0
//NSString*initIv=@"12345678";
constvoid*vkey=key;
constvoid*iv=(constvoid*)key;//[initIvUTF8String];
//CCCrypt函數加密/解密
ccStatus=CCCrypt(encryptOperation,//加密/解密
kCCAlgorithmDES,//加密根據哪個標准(des,3des,aes。。。。)
kCCOptionPKCS7Padding,//選項分組密碼演算法(des:對每塊分組加一次密3DES:對每塊分組加三個不同的密)
vkey,//密鑰加密和解密的密鑰必須一致
kCCKeySizeDES,//DES密鑰的大小(kCCKeySizeDES=8)
iv,//可選的初始矢量
dataIn,//數據的存儲單元
dataInLength,//數據的大小
(void*)dataOut,//用於返回數據
dataOutAvailable,
&dataOutMoved);
NSString*result=nil;
if(encryptOperation==kCCDecrypt)//encryptOperation==1解碼
{
//得到解密出來的data數據,改變為utf-8的字元串
result=[[NSStringalloc]initWithData:[NSDatadataWithBytes:(constvoid*)dataOutlength:(NSUInteger)dataOutMoved]encoding:NSUTF8StringEncoding];
}
else//encryptOperation==0(加密過程中,把加好密的數據轉成base64的)
{
//編碼base64
NSData*data=[NSDatadataWithBytes:(constvoid*)dataOutlength:(NSUInteger)dataOutMoved];
result=[GTMBase64stringByEncodingData:data];
}
return[resultUTF8String];
}
+(NSString*)encryptWithContent:(NSString*)contenttype:(CCOperation)typekey:(NSString*)aKey
{
constchar*contentChar=[contentUTF8String];
char*keyChar=(char*)[aKeyUTF8String];
constchar*miChar;
miChar=encryptWithKeyAndType(contentChar,type,keyChar);
return[NSStringstringWithCString:miCharencoding:NSUTF8StringEncoding];
}
⑶ android 怎麼多圖上傳 okhttp
android上傳圖片是先將圖片文件轉換成流文件:可用以下代碼轉換流文件,imgPath為圖片的完整地址
//圖片轉化成base64字元串
public static String imgToBase64(String imgPath) {
InputStream in = null;
byte[] data = null;
//讀取圖片位元組數組
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e){
e.printStackTrace();
}
//對位元組數組Base64編碼
sun.misc.BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64編碼過的位元組數組字元串
}
然後圖片文件就成為一串字元串啦,傳遞方法和普通字元串一樣,多圖使用分號隔開即可,後台收到後直接將流文件轉換成圖片保存即可。