设为首页 加入收藏

TOP

C#/iOS/Android通用加密解密方法(一)
2015-08-31 21:23:56 来源: 作者: 【 】 浏览:77
Tags:C#/iOS/Android 通用 加密解密 方法

C#代码


     #region? 跨平台加解密(c#)
? ? ? ?
? ? ? ? ///


? ? ? ? /// 对字符串进行DES加密
? ? ? ? ///

? ? ? ? /// 待加密的字符串
? ? ? ? /// 加密后的BASE64编码的字符串
? ? ? ? public string Encrypt(string sourceString, string sKey)
? ? ? ? {
? ? ? ? ? ? byte[] btKey = Encoding.UTF8.GetBytes(sKey);
? ? ? ? ? ? byte[] btIV = Encoding.UTF8.GetBytes(sKey);
? ? ? ? ? ? DESCryptoServiceProvider des = new DESCryptoServiceProvider();
? ? ? ? ? ? using (MemoryStream ms = new MemoryStream())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? byte[] inData = Encoding.UTF8.GetBytes(sourceString);
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? cs.Write(inData, 0, inData.Length);
? ? ? ? ? ? ? ? ? ? ? ? cs.FlushFinalBlock();
? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? return Convert.ToBase64String(ms.ToArray());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? throw;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }



? ? ? ? ///


? ? ? ? /// 解密
? ? ? ? ///

? ? ? ? /// 要解密的以Base64
? ? ? ? /// 密钥,且必须为8位
? ? ? ? /// 已解密的字符串
? ? ? ? public string Decrypt(string pToDecrypt, string sKey)
? ? ? ? {


? ? ? ? ? ? //转义特殊字符
? ? ? ? ? ? pToDecrypt = pToDecrypt.Replace("-", "+");
? ? ? ? ? ? pToDecrypt = pToDecrypt.Replace("_", "/");
? ? ? ? ? ? pToDecrypt = pToDecrypt.Replace("~", "=");
? ? ? ? ? ? byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
? ? ? ? ? ? using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
? ? ? ? ? ? ? ? des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
? ? ? ? ? ? ? ? System.IO.MemoryStream ms = new System.IO.MemoryStream();
? ? ? ? ? ? ? ? using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? cs.Write(inputByteArray, 0, inputByteArray.Length);
? ? ? ? ? ? ? ? ? ? cs.FlushFinalBlock();
? ? ? ? ? ? ? ? ? ? cs.Close();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? string str = Encoding.UTF8.GetString(ms.ToArray());
? ? ? ? ? ? ? ? ms.Close();
? ? ? ? ? ? ? ? return str;
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? #endregion


IOS代码


? ? ? ? static const char* encryptWithKeyAndType(const char *text,CCOperation encryptOperation,char *key)
{
? ? NSString *textString=[[NSString alloc]initWithCString:text encoding:NSUTF8StringEncoding];
? ? //? ? ? NSLog(@"[[item.url description] UTF8String=%@",textString);
? ? const void *dataIn;
? ? size_t dataInLength;
? ?
? ? if (encryptOperation == kCCDecrypt)//传递decrypt 解码
? ? {
? ? ? ? //解码 base64
? ? ? ? NSData *decryptData = [GTMBase64 decodeData:[textString dataUsingEncoding:NSUTF8StringEncoding]];//转utf-8并decode
? ? ? ? dataInLength = [decryptData length];
? ? ? ? dataIn = [decryptData bytes];
? ? }
? ? else? //encrypt
? ? {
? ? ? ? NSData* encryptData = [textString dataUsingEncoding:NSUTF8StringEncoding];
? ? ? ? dataInLength = [encryptData length];
? ? ? ? dataIn = (const void *)[encryptData bytes];
? ? }
? ?
? ?
? ? CCCryptorStatus ccStatus;
? ? uint8_t *dataOut = NULL; //理解位type/typedef 缩写(效维护代码比:用int用long用typedef定义)
? ? size_t dataOutAvailable = 0; //size_t? 操作符sizeof返结类型
? ? size_t dataOutMoved = 0;
? ?
? ? dataOutAvailable = (dataInLength + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
? ? dataOut = malloc( dataOutAvailable * sizeof(uint8_t));
? ? memset((void *)dataOut, 00, dataOutAvailable);//已辟内存空间buffer首 1 字节值设值 0
? ?
? ? //NSString *initIv = @"12345678";
? ? const void *vkey = key;
? ? const void *iv = (const void *) key; //[initIv UTF8String];
? ?
? ? //CCCrypt函数 加密/解密
? ? ccStatus = CCCrypt(encryptOperation,//? 加密/解密
? ? ? ? ? ? ? ? ? ? ? kCCAlgorithmDES,//? 加密根据哪标准(des3desaes)
? ? ? ? ? ? ? ? ? ? ? kCCOptionPKCS7Padding,//? 选

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android中POST方式发送HTTP请求 下一篇在Android用Get方式发送HTTP请求

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: