AzDG可逆加密演算法 for Dart

Google 2011 年時所推出的語言,據說是要取代現有的Javascript,先前已有稍為看了一下,因為沒有專案在使用這門語言,所以就沒有進一步的瞭解,

據聞公司明年的合作夥伴採用Dart來作為專案的主要撰寫語言,所以再度把AzDG拿來練手,

和google 另一門語言 Go 一樣還滿易學的。

import 'package:crypto/crypto.dart' show MD5, CryptoUtils;
import 'package:utf/utf.dart' ;

class AzDG {
  String _cipher = 'Private key';  
  AzDG({String cipher}) {   
    if (cipher != null)
    {
      _cipher = cipher;
    }
  }
  
  List<int> _cipherEncode(List<int> inputData)
  {
    var md5 = new MD5(); 
    md5.add(this._cipher.codeUnits);
    var cipherHash = CryptoUtils.bytesToHex(md5.close()).codeUnits; 
    var outData = new List<int>(inputData.length); 
    var loopCount = inputData.length;
    for (var i = 0; i < loopCount; i++) {
      outData[i] =  inputData[i] ^ cipherHash[i%32];
    }  
    return outData;
  }
  
  String Decrypt(String sourceText)
  {
    var decodeSourceText = _cipherEncode(CryptoUtils.base64StringToBytes(sourceText));   
    var size = (decodeSourceText.length ~/ 2).toInt();
    var outData = new List<int>(size);
    for (int j = 0; j < size; ++j) {     
      outData[j] = (decodeSourceText[(j*2)] ^ decodeSourceText[(j*2)+1]);
    }
    return decodeUtf8(outData);
  }
  
  String Crypt(String sourceText)
  {
    var now = new DateTime.now();
    var md5 = new MD5();   
    md5.add(now.toString().codeUnits);
    var noise = CryptoUtils.bytesToHex(md5.close()).codeUnits;
    var inputData = encodeUtf8(sourceText);
    var outData = new List<int>(inputData.length*2);
    for (var j = 0; j < inputData.length ; j++) {
        outData[(j*2)] = noise[j%32];      
        outData[(j*2)+1] = inputData[j] ^ noise[j%32];
    }
    return CryptoUtils.bytesToBase64(_cipherEncode(outData));
  }
}


转载于:https://my.oschina.net/eddie/blog/188397

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值