zoukankan      html  css  js  c++  java
  • .net 安卓IOS跨平台des加解密双向的(可以互相加解密)

      1 #region   跨平台加解密(c# 安卓 IOS)
      2 
      3       //  public static string sKey = "12345678";
      4 
      5       //  /// 
      6 
      7       //  /// 解密
      8 
      9       //  /// 
     10 
     11       //  /// 要解密的以Base64
     12 
     13       //  /// 密钥,且必须为8位
     14 
     15       //  /// 已解密的字符串
     16 
     17       //  public static string DesDecrypt(string pToDecrypt)
     18 
     19       //  {
     20 
     21       //      //转义特殊字符
     22 
     23       //      pToDecrypt = pToDecrypt.Replace("-", "+");
     24 
     25       //      pToDecrypt = pToDecrypt.Replace("_", "/");
     26 
     27       //      pToDecrypt = pToDecrypt.Replace("~", "=");
     28 
     29       //      byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
     30 
     31       //      using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
     32 
     33       //      {
     34 
     35       //          des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
     36 
     37       //          des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
     38 
     39       //          System.IO.MemoryStream ms = new System.IO.MemoryStream();
     40 
     41       //          using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
     42 
     43       //          {
     44 
     45       //              cs.Write(inputByteArray, 0, inputByteArray.Length);
     46 
     47       //              cs.FlushFinalBlock();
     48 
     49       //              cs.Close();
     50 
     51       //          }
     52 
     53       //          string str = Encoding.UTF8.GetString(ms.ToArray());
     54 
     55       //          ms.Close();
     56 
     57       //          return str;
     58 
     59       //      }
     60 
     61       //  }
     62 
     63  
     64 
     65       //  /// 
     66 
     67       //  /// 对字符串进行DES加密
     68 
     69       //  /// 
     70 
     71       //  /// 待加密的字符串
     72 
     73       //  /// 加密后的BASE64编码的字符串
     74 
     75       //  public string Encrypt(string sourceString)
     76 
     77       //{
     78 
     79       //   byte[] btKey = Encoding.UTF8.GetBytes(sKey);
     80 
     81       //   byte[] btIV = Encoding.UTF8.GetBytes(sKey);
     82 
     83       //    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     84 
     85       //    using (MemoryStream ms = new MemoryStream())
     86 
     87       //    {
     88 
     89       //        byte[] inData = Encoding.UTF8.GetBytes(sourceString);
     90 
     91       //        try
     92 
     93       //        {
     94 
     95       //            using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
     96 
     97       //            {
     98 
     99       //                cs.Write(inData, 0, inData.Length);
    100 
    101       //                cs.FlushFinalBlock();
    102 
    103       //            }
    104 
    105  
    106 
    107       //            return Convert.ToBase64String(ms.ToArray());
    108 
    109       //        }
    110 
    111       //        catch
    112 
    113       //        {
    114 
    115       //            throw;
    116 
    117       //        }
    118 
    119       //    }
    120 
    121       //}
    122 
    123         
    124 
    125         #endregion  
    126 
    127  
    128 
    129  
    130 
    131  
    132 
    133  
    134 
    135 安卓---------------------------------------------------------------------------
    136 
    137     //    // 加密
    138 
    139     //public static String DecryptDoNet(String message, String key)
    140 
    141     //        throws Exception {
    142 
    143     //    byte[] bytesrc = Base64.decode(message.getBytes(), Base64.DEFAULT);
    144 
    145     //    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    146 
    147     //    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
    148 
    149     //    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    150 
    151     //    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
    152 
    153     //    IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
    154 
    155     //    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    156 
    157     //    byte[] retByte = cipher.doFinal(bytesrc);
    158 
    159     //    return new String(retByte);
    160 
    161     //}
    162 
    163  
    164 
    165     //// 解密
    166 
    167     //public static String EncryptAsDoNet(String message, String key)
    168 
    169     //        throws Exception {
    170 
    171     //    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    172 
    173     //    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
    174 
    175     //    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    176 
    177     //    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
    178 
    179     //    IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
    180 
    181     //    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
    182 
    183     //    byte[] encryptbyte = cipher.doFinal(message.getBytes());
    184 
    185     //    return new String(Base64.encode(encryptbyte, Base64.DEFAULT));
    186 
    187     //}
    188 
    189  
    190 
    191  
    192 
    193  
    194 
    195 Ios --------------------------------------------------------------------------------------------------------------------
    196 
    197 static const char* encryptWithKeyAndType(const char *text,CCOperation encryptOperation,char *key)
    198 {
    199     NSString *textString=[[NSString alloc]initWithCString:text encoding:NSUTF8StringEncoding];
    200     //      NSLog(@"[[item.url description] UTF8String=%@",textString);
    201     const void *dataIn;
    202     size_t dataInLength;
    203     
    204     if (encryptOperation == kCCDecrypt)//传递过来的是decrypt 解码
    205     {
    206         //解码 base64
    207         NSData *decryptData = [GTMBase64 decodeData:[textString dataUsingEncoding:NSUTF8StringEncoding]];//转成utf-8并decode
    208         dataInLength = [decryptData length];
    209         dataIn = [decryptData bytes];
    210     }
    211     else  //encrypt
    212     {
    213         NSData* encryptData = [textString dataUsingEncoding:NSUTF8StringEncoding];
    214         dataInLength = [encryptData length];
    215         dataIn = (const void *)[encryptData bytes];
    216     }
    217     
    218     
    219     CCCryptorStatus ccStatus;
    220     uint8_t *dataOut = NULL; //可以理解位type/typedef 的缩写(有效的维护了代码,比如:一个人用int,一个人用long。最好用typedef来定义)
    221     size_t dataOutAvailable = 0; //size_t  是操作符sizeof返回的结果类型
    222     size_t dataOutMoved = 0;
    223     
    224     dataOutAvailable = (dataInLength + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
    225     dataOut = malloc( dataOutAvailable * sizeof(uint8_t));
    226     memset((void *)dataOut, 00, dataOutAvailable);//将已开辟内存空间buffer的首 1 个字节的值设为值 0
    227     
    228     //NSString *initIv = @"12345678";
    229     const void *vkey = key;
    230     const void *iv = (const void *) key; //[initIv UTF8String];
    231     
    232     //CCCrypt函数 加密/解密
    233     ccStatus = CCCrypt(encryptOperation,//  加密/解密
    234                        kCCAlgorithmDES,//  加密根据哪个标准(des,3des,aes。。。。)
    235                        kCCOptionPKCS7Padding,//  选项分组密码算法(des:对每块分组加一次密  3DES:对每块分组加三个不同的密)
    236                        vkey,  //密钥    加密和解密的密钥必须一致
    237                        kCCKeySizeDES,//   DES 密钥的大小(kCCKeySizeDES=8)
    238                        iv, //  可选的初始矢量
    239                        dataIn, // 数据的存储单元
    240                        dataInLength,// 数据的大小
    241                        (void *)dataOut,// 用于返回数据
    242                        dataOutAvailable,
    243                        &dataOutMoved);
    244     
    245     NSString *result = nil;
    246     
    247     if (encryptOperation == kCCDecrypt)//encryptOperation==1  解码
    248     {
    249         //得到解密出来的data数据,改变为utf-8的字符串
    250         result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)dataOut length:(NSUInteger)dataOutMoved] encoding:NSUTF8StringEncoding];
    251     }
    252     else //encryptOperation==0  (加密过程中,把加好密的数据转成base64的)
    253     {
    254         //编码 base64
    255         NSData *data = [NSData dataWithBytes:(const void *)dataOut length:(NSUInteger)dataOutMoved];
    256         result = [GTMBase64 stringByEncodingData:data];
    257     }
    258     
    259     return [result UTF8String];
    260     
    261 }
    262 +(NSString*)encryptWithContent:(NSString*)content type:(CCOperation)type key:(NSString*)aKey
    263 {
    264     const char * contentChar =[content UTF8String];
    265     char * keyChar =(char*)[aKey UTF8String];
    266     const char *miChar;
    267     miChar = encryptWithKeyAndType(contentChar, type, keyChar);
    268     return  [NSString stringWithCString:miChar encoding:NSUTF8StringEncoding];
    269 }
    270 
    271  
  • 相关阅读:
    JAVA SkipList 跳表 的原理和使用例子
    TreeMap、HashMap、ConcurrentSkipListMap之性能比较
    CompletionService 和ExecutorService的区别和用法
    1. java.util.concurrent
    JAVA Concurrent包 中的并发集合类
    并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
    Objective-C中 ==、isEqual、isEqualToString判断字符串相等
    设置UIButton的文字显示位置、字体的大小、字体的颜色
    9个完整android开源app项目
    android 开源项目集合
  • 原文地址:https://www.cnblogs.com/jiebo/p/4281828.html
Copyright © 2011-2022 走看看