public virtual string ObjectCryptography( string secretKey, string data, TransformType type, AlgorithmType algType, AlgorithmKeyType algKeyType) { string str = (string)null; switch (algType) { case AlgorithmType.Rijndael: this.Encryptor = (Encryptor) new AlgorithmRijndael(secretKey, algKeyType); str = this.Encryptor.ObjectCryptography(data, type); break; case AlgorithmType.TripleDES: this.Encryptor = (Encryptor) new AlgorithmTripleDES(secretKey, algKeyType); str = this.Encryptor.ObjectCryptography(data, type); break; case AlgorithmType.DES: this.Encryptor = (Encryptor) new AlgorithmDES(secretKey, algKeyType); str = this.Encryptor.ObjectCryptography(data, type); break; } return(str); }
protected virtual byte[] WriteStream( LicenseInfo licInfo, byte version, Encryptor encryptor, AlgorithmType algType, AlgorithmKeyType algKeyType) { using (MemoryStream memoryStream = new MemoryStream()) { using (BinaryWriter binaryWriter = new BinaryWriter((Stream)memoryStream)) { try { binaryWriter.Write((short)5); binaryWriter.Write(version); binaryWriter.Write(Convert.ToByte((int)algType)); binaryWriter.Write(Convert.ToByte((int)algKeyType)); binaryWriter.Write(encryptor.ObjectCryptography(licInfo.Data, TransformType.ENCRYPT)); binaryWriter.Flush(); } catch (Exception ex) { throw ex; } finally { binaryWriter.Close(); memoryStream.Close(); } } return(memoryStream.ToArray()); } }
protected virtual byte[] WriteStream( LicenseInfo licInfo, byte version, AlgorithmType algType, AlgorithmKeyType algKeyType) { using (MemoryStream memoryStream = new MemoryStream()) { using (BinaryWriter binaryWriter = new BinaryWriter((Stream)memoryStream)) { try { binaryWriter.Write((short)5); binaryWriter.Write(version); binaryWriter.Write(Convert.ToByte((int)algType)); binaryWriter.Write(Convert.ToByte((int)algKeyType)); binaryWriter.Write(licInfo.ProductKey); binaryWriter.Flush(); } catch (IOException ex) { throw ex; } finally { binaryWriter.Close(); memoryStream.Close(); } } return(memoryStream.ToArray()); } }
protected virtual void WriteFile( LicenseInfo licInfo, byte version, Encryptor encryptor, AlgorithmType algType, AlgorithmKeyType algKeyType) { using (FileStream fileStream = new FileStream(this._fileName, FileMode.Create)) { using (BinaryWriter binaryWriter = new BinaryWriter((Stream)fileStream)) { try { binaryWriter.Write((short)5); binaryWriter.Write(version); binaryWriter.Write(Convert.ToByte((int)algType)); binaryWriter.Write(Convert.ToByte((int)algKeyType)); binaryWriter.Write(encryptor.ObjectCryptography(licInfo.Data, TransformType.ENCRYPT)); binaryWriter.Flush(); } catch (IOException ex) { throw ex; } finally { binaryWriter.Close(); fileStream.Close(); } } } }
public virtual byte[] SaveLicenseToStream( string secretKey, LicenseInfo licInfo, byte version, AlgorithmType algType, AlgorithmKeyType algKeyType) { byte[] numArray = (byte[])null; switch (algType) { case AlgorithmType.None: try { numArray = this.WriteStream(licInfo, version, AlgorithmType.None, AlgorithmKeyType.None); break; } catch (Exception ex) { throw ex; } case AlgorithmType.Rijndael: try { this.Encryptor = (Encryptor) new AlgorithmRijndael(secretKey, algKeyType); numArray = this.WriteStream(licInfo, version, this.Encryptor, AlgorithmType.Rijndael, algKeyType); break; } catch (Exception ex) { throw ex; } case AlgorithmType.TripleDES: try { this.Encryptor = (Encryptor) new AlgorithmTripleDES(secretKey, algKeyType); numArray = this.WriteStream(licInfo, version, this.Encryptor, AlgorithmType.TripleDES, algKeyType); break; } catch (Exception ex) { throw ex; } case AlgorithmType.DES: try { this.Encryptor = (Encryptor) new AlgorithmDES(secretKey, algKeyType); numArray = this.WriteStream(licInfo, version, this.Encryptor, AlgorithmType.DES, algKeyType); break; } catch (Exception ex) { throw ex; } } return(numArray); }
public virtual void SaveLicenseToFile( string secretKey, LicenseInfo licInfo, byte version, AlgorithmType algType, AlgorithmKeyType algKeyType) { switch (algType) { case AlgorithmType.None: try { this.WriteFile(licInfo, version, AlgorithmType.None, AlgorithmKeyType.None); break; } catch (Exception ex) { throw ex; } case AlgorithmType.Rijndael: try { this.Encryptor = (Encryptor) new AlgorithmRijndael(secretKey, algKeyType); this.WriteFile(licInfo, version, this.Encryptor, AlgorithmType.Rijndael, algKeyType); break; } catch (Exception ex) { throw ex; } case AlgorithmType.TripleDES: try { this.Encryptor = (Encryptor) new AlgorithmTripleDES(secretKey, algKeyType); this.WriteFile(licInfo, version, this.Encryptor, AlgorithmType.TripleDES, algKeyType); break; } catch (Exception ex) { throw ex; } case AlgorithmType.DES: try { this.Encryptor = (Encryptor) new AlgorithmDES(secretKey, algKeyType); this.WriteFile(licInfo, version, this.Encryptor, AlgorithmType.DES, algKeyType); break; } catch (Exception ex) { throw ex; } } }
protected virtual string ReadStream( short lHeader, byte version, out AlgorithmType algType, out AlgorithmKeyType algKeyType) { using (MemoryStream memoryStream = new MemoryStream(this._data)) { using (BinaryReader binaryReader = new BinaryReader((Stream)memoryStream)) { string str = (string)null; algType = AlgorithmType.None; algKeyType = AlgorithmKeyType.None; try { if ((int)binaryReader.ReadInt16() == (int)lHeader) { if ((int)binaryReader.ReadByte() == (int)version) { algType = (AlgorithmType)binaryReader.ReadByte(); algKeyType = (AlgorithmKeyType)binaryReader.ReadByte(); str = binaryReader.ReadString(); } } } catch (IOException ex) { throw ex; } finally { binaryReader.Close(); memoryStream.Close(); } return(str); } } }
public virtual bool IsValidStreamFormat( short lHeader, byte version, out AlgorithmType algType, out AlgorithmKeyType algKeyType) { using (MemoryStream memoryStream = new MemoryStream(this._data)) { using (BinaryReader binaryReader = new BinaryReader((Stream)memoryStream)) { algType = AlgorithmType.None; algKeyType = AlgorithmKeyType.None; try { if ((int)binaryReader.ReadInt16() == (int)lHeader) { if ((int)binaryReader.ReadByte() == (int)version) { algType = (AlgorithmType)binaryReader.ReadByte(); algKeyType = (AlgorithmKeyType)binaryReader.ReadByte(); return(true); } } } catch (IOException ex) { throw ex; } finally { binaryReader.Close(); memoryStream.Close(); } return(false); } } }
public virtual bool IsValidFileFormat( short lHeader, byte version, out AlgorithmType algType, out AlgorithmKeyType algKeyType) { using (FileStream fileStream = new FileStream(this._fileName, FileMode.Open)) { using (BinaryReader binaryReader = new BinaryReader((Stream)fileStream)) { algType = AlgorithmType.None; algKeyType = AlgorithmKeyType.None; try { if ((int)binaryReader.ReadInt16() == (int)lHeader) { if ((int)binaryReader.ReadByte() == (int)version) { algType = (AlgorithmType)binaryReader.ReadByte(); algKeyType = (AlgorithmKeyType)binaryReader.ReadByte(); return(true); } } } catch (IOException ex) { throw ex; } finally { binaryReader.Close(); fileStream.Close(); } return(false); } } }
/// <summary> /// protected virtual void WriteFile(byte[] data, Version version, AlgorithmType algType, AlgorithmKeyType algKeyType) /// </summary> /// <param name="data"></param> /// <param name="version"></param> /// <param name="algType"></param> /// <param name="algKeyType"></param> protected virtual void WriteFile(byte[] data, Version version, AlgorithmType algType, AlgorithmKeyType algKeyType) { using (FileStream f = new FileStream(_fileName, FileMode.Create)) { using (BinaryWriter w = new BinaryWriter(f)) { try { w.Write((Int16)5); w.Write((byte)version); w.Write(Convert.ToByte((int)algType)); w.Write(Convert.ToByte((int)algKeyType)); w.Write(data); w.Flush(); } catch (IOException e) { throw e; } finally { w.Close(); f.Close(); } } } }
/// <summary> /// protected virtual void WriteFile(string secretKey, byte[] data, Version version, Encryptor encryptor, AlgorithmType algType, AlgorithmKeyType algKeyType) /// </summary> /// <param name="secretKey"></param> /// <param name="data"></param> /// <param name="version"></param> /// <param name="encryptor"></param> /// <param name="algType"></param> /// <param name="algKeyType"></param> protected virtual void WriteFile(string secretKey, byte[] data, Version version, Encryptor encryptor, AlgorithmType algType, AlgorithmKeyType algKeyType) { using (FileStream f = new FileStream(_fileName, FileMode.Create)) { using (BinaryWriter w = new BinaryWriter(f)) { try { w.Write((Int16)5); w.Write((byte)version); w.Write(Convert.ToByte((int)algType)); w.Write(Convert.ToByte((int)algKeyType)); w.Write(encryptor.ObjectCryptography(data, TransformType.ENCRYPT)); w.Flush(); } catch (IOException e) { throw e; } finally { w.Close(); f.Close(); } } } }
/// <summary> /// public virtual bool SavePacket(string secretKey, byte[] data, Version version, AlgorithmType algType, AlgorithmKeyType algKeyType) /// </summary> /// <param name="secretKey"></param> /// <param name="data"></param> /// <param name="version"></param> /// <param name="algType"></param> /// <param name="algKeyType"></param> /// <returns></returns> public virtual bool SavePacket(string secretKey, byte[] data, Version version, AlgorithmType algType, AlgorithmKeyType algKeyType) { bool flag = false; switch (algType) { case AlgorithmType.DES: try { Encryptor = new AlgorithmDES(secretKey, algKeyType); WriteFile(secretKey, data, version, Encryptor, AlgorithmType.DES, algKeyType); flag = true; } catch { flag = false; } break; case AlgorithmType.Rijndael: try { Encryptor = new AlgorithmRijndael(secretKey, algKeyType); WriteFile(secretKey, data, version, Encryptor, AlgorithmType.Rijndael, algKeyType); flag = true; } catch { flag = false; } break; case AlgorithmType.TripleDES: try { Encryptor = new AlgorithmTripleDES(secretKey, algKeyType); WriteFile(secretKey, data, version, Encryptor, AlgorithmType.TripleDES, algKeyType); flag = true; } catch { flag = false; } break; case AlgorithmType.None: try { WriteFile(data, version, AlgorithmType.None, AlgorithmKeyType.None); flag = true; } catch { flag = false; } break; default: break; } return(flag); }
/// <summary> /// public virtual byte[] MemoryPacket(string secretKey, byte[] data, Version version, AlgorithmType algType, AlgorithmKeyType algKeyType) /// </summary> /// <param name="secretKey"></param> /// <param name="data"></param> /// <param name="version"></param> /// <param name="algType"></param> /// <param name="algKeyType"></param> /// <returns></returns> public virtual byte[] MemoryPacket(string secretKey, byte[] data, Version version, AlgorithmType algType, AlgorithmKeyType algKeyType) { byte[] value = null; switch (algType) { case AlgorithmType.DES: try { Encryptor = new AlgorithmDES(secretKey, algKeyType); value = WriteMenoryStream(secretKey, data, version, Encryptor, AlgorithmType.DES, algKeyType); } catch (Exception e) { throw e; } break; case AlgorithmType.Rijndael: try { Encryptor = new AlgorithmRijndael(secretKey, algKeyType); value = WriteMenoryStream(secretKey, data, version, Encryptor, AlgorithmType.Rijndael, algKeyType); } catch (Exception e) { throw e; } break; case AlgorithmType.TripleDES: try { Encryptor = new AlgorithmTripleDES(secretKey, algKeyType); value = WriteMenoryStream(secretKey, data, version, Encryptor, AlgorithmType.TripleDES, algKeyType); } catch (Exception e) { throw e; } break; case AlgorithmType.None: try { value = WriteMenoryStream(data, version, AlgorithmType.None, AlgorithmKeyType.None); } catch (Exception e) { throw e; } break; default: break; } return(value); }
public AlgorithmDES(string secretKey, AlgorithmKeyType AlgType) : base(secretKey, AlgType) { }
public Encryptor(string secretKey, AlgorithmKeyType AlgType) { GenerateKey(secretKey, AlgType); }
/// <summary> /// public override void GenerateKey(string secretKey, AlgorithmKeyType type) /// </summary> /// <param name="secretKey"></param> /// <param name="type"></param> public override void GenerateKey(string secretKey, AlgorithmKeyType type) { Key = new byte[32]; IV = new byte[16]; byte[] bKey = Encoding.UTF8.GetBytes(secretKey); switch (type) { case AlgorithmKeyType.MD5: //16 byte using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) { md5.ComputeHash(bKey); byte[] rmd5 = md5.Hash; for (int i = 0; i < 16; i++) { Key[i] = rmd5[i]; } for (int j = 15; j >= 0; j--) { IV[15 - j] = rmd5[j]; } } break; case AlgorithmKeyType.SHA1: //20 byte using (SHA1Managed sha1 = new SHA1Managed()) { sha1.ComputeHash(bKey); byte[] rsha1 = sha1.Hash; for (int i = 0; i < 20; i++) { Key[i] = rsha1[i]; } for (int j = 16; j > 0; j--) { IV[16 - j] = rsha1[j]; } } break; case AlgorithmKeyType.SHA256: //32 byte using (SHA256Managed sha256 = new SHA256Managed()) { sha256.ComputeHash(bKey); byte[] rsha256 = sha256.Hash; for (int i = 0; i < 32; i++) { Key[i] = rsha256[i]; } for (int j = 16; j > 0; j--) { IV[16 - j] = rsha256[j]; } } break; case AlgorithmKeyType.SHA384: //48 byte using (SHA384Managed sha384 = new SHA384Managed()) { sha384.ComputeHash(bKey); byte[] rsha384 = sha384.Hash; for (int i = 0; i < 32; i++) { Key[i] = rsha384[i]; } for (int j = 47; j > 31; j--) { IV[47 - j] = rsha384[j]; } } break; case AlgorithmKeyType.SHA512: //64 byte using (SHA512Managed sha512 = new SHA512Managed()) { sha512.ComputeHash(bKey); byte[] rsha512 = sha512.Hash; for (int i = 0; i < 32; i++) { Key[i] = rsha512[i]; } for (int j = 63; j > 47; j--) { IV[63 - j] = rsha512[j]; } } break; default: break; } }
public AlgorithmTripleDES(string secretkey, AlgorithmKeyType AlgType) : base(secretkey, AlgType) { }
/// <summary> /// public virtual void SaveLicense(string secretKey, string data, Version version, AlgorithmType algType, AlgorithmKeyType algKeyType) /// </summary> /// <param name="secretKey"></param> /// <param name="data"></param> /// <param name="version"></param> /// <param name="algType"></param> /// <param name="algKeyType"></param> public virtual void SaveLicense(string secretKey, string data, Version version, AlgorithmType algType, AlgorithmKeyType algKeyType) { switch (algType) { case AlgorithmType.DES: try { Encryptor = new AlgorithmDES(secretKey, algKeyType); WriteFile(secretKey, data, version, Encryptor, AlgorithmType.DES, algKeyType); } catch { return; } break; case AlgorithmType.Rijndael: try { Encryptor = new AlgorithmRijndael(secretKey, algKeyType); WriteFile(secretKey, data, version, Encryptor, AlgorithmType.Rijndael, algKeyType); } catch { return; } break; case AlgorithmType.TripleDES: try { Encryptor = new AlgorithmTripleDES(secretKey, algKeyType); WriteFile(secretKey, data, version, Encryptor, AlgorithmType.TripleDES, algKeyType); } catch { return; } break; case AlgorithmType.None: try { WriteFile(data, version, AlgorithmType.None, AlgorithmKeyType.None); } catch { return; } break; } }
public AlgorithmRijndael(string secretKey, AlgorithmKeyType AlgType) : base(secretKey, AlgType) { }
/// <summary> /// public virtual bool IsValidFormat(Int16 lHeader, Version version, out AlgorithmType algType, out AlgorithmKeyType algKeyType) /// </summary> /// <param name="lHeader"></param> /// <param name="version"></param> /// <param name="algType"></param> /// <param name="algKeyType"></param> /// <returns></returns> public virtual bool IsValidFormat(Int16 lHeader, Version version, out AlgorithmType algType, out AlgorithmKeyType algKeyType) { using (FileStream f = new FileStream(_fileName, FileMode.Open)) { using (BinaryReader r = new BinaryReader(f)) { algType = 0; algKeyType = 0; bool flag = false; try { if (r.ReadInt16() == lHeader && r.ReadByte() == (byte)version) { algType = (AlgorithmType)r.ReadByte(); algKeyType = (AlgorithmKeyType)r.ReadByte(); flag = true; } } catch (IOException e) { throw e; } finally { r.Close(); f.Close(); } return(flag); } } }
public abstract void GenerateKey(string secretKey, AlgorithmKeyType type);
/// <summary> /// protected virtual byte[] ReadFile(Int16 lHeader, Version version, out AlgorithmType algType, out AlgorithmKeyType algKeyType) /// </summary> /// <param name="lHeader"></param> /// <param name="version"></param> /// <param name="algType"></param> /// <param name="algKeyType"></param> /// <returns></returns> protected virtual byte[] ReadFile(Int16 lHeader, Version version, out AlgorithmType algType, out AlgorithmKeyType algKeyType) { using (FileStream f = new FileStream(_fileName, FileMode.Open)) { using (BinaryReader r = new BinaryReader(f)) { byte[] value = null; algType = 0; algKeyType = 0; try { if (r.ReadInt16() == lHeader && r.ReadByte() == (byte)version) { algType = (AlgorithmType)r.ReadByte(); algKeyType = (AlgorithmKeyType)r.ReadByte(); value = r.ReadBytes((int)f.Length - 5); } } catch (IOException e) { throw e; } finally { r.Close(); f.Close(); } return(value); } } }
/// <summary> /// public virtual string ObjectCryptography(string secretKey, string data, TransformType type, AlgorithmType algType, AlgorithmKeyType algKeyType) /// </summary> /// <param name="secretKey"></param> /// <param name="data"></param> /// <param name="type"></param> /// <param name="algType"></param> /// <param name="algKeyType"></param> /// <returns></returns> public virtual string ObjectCryptography(string secretKey, string data, TransformType type, AlgorithmType algType, AlgorithmKeyType algKeyType) { string value = null; switch (algType) { case AlgorithmType.DES: Encryptor = new AlgorithmDES(secretKey, algKeyType); value = Encryptor.ObjectCryptography(data, type); break; case AlgorithmType.Rijndael: Encryptor = new AlgorithmRijndael(secretKey, algKeyType); value = Encryptor.ObjectCryptography(data, type); break; case AlgorithmType.TripleDES: Encryptor = new AlgorithmTripleDES(secretKey, algKeyType); value = Encryptor.ObjectCryptography(data, type); break; default: break; } return(value); }
/// <summary> /// protected virtual byte[] WriteMenoryStream(string seretKey, byte[] data, Version version, Encryptor encryptor, AlgorithmType algType, AlgorithmKeyType algKeyType) /// </summary> /// <param name="seretKey"></param> /// <param name="data"></param> /// <param name="version"></param> /// <param name="encryptor"></param> /// <param name="algType"></param> /// <param name="algKeyType"></param> /// <returns></returns> protected virtual byte[] WriteMenoryStream(string seretKey, byte[] data, Version version, Encryptor encryptor, AlgorithmType algType, AlgorithmKeyType algKeyType) { byte[] value = null; using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter w = new BinaryWriter(ms)) { try { w.Write((Int16)5); w.Write((byte)version); w.Write(Convert.ToByte((int)algType)); w.Write(Convert.ToByte((int)algKeyType)); w.Write(encryptor.ObjectCryptography(data, TransformType.ENCRYPT)); w.Flush(); value = ms.ToArray(); } catch (Exception e) { throw e; } finally { w.Close(); ms.Close(); } } } return(value); }
public override void GenerateKey(string secretKey, AlgorithmKeyType type) { this.Key = new byte[8]; this.IV = new byte[8]; byte[] bytes = Encoding.UTF8.GetBytes(secretKey); switch (type) { case AlgorithmKeyType.SHA1: using (SHA1Managed shA1Managed = new SHA1Managed()) { shA1Managed.ComputeHash(bytes); byte[] hash = shA1Managed.Hash; for (int index = 0; index < 8; ++index) { this.Key[index] = hash[index]; } for (int index = 19; index > 11; --index) { this.IV[19 - index] = hash[index]; } break; } case AlgorithmKeyType.SHA256: using (SHA256Managed shA256Managed = new SHA256Managed()) { shA256Managed.ComputeHash(bytes); byte[] hash = shA256Managed.Hash; for (int index = 0; index < 8; ++index) { this.Key[index] = hash[index]; } for (int index = 31; index >= 24; --index) { this.IV[31 - index] = hash[index]; } break; } case AlgorithmKeyType.SHA384: using (SHA384Managed shA384Managed = new SHA384Managed()) { shA384Managed.ComputeHash(bytes); byte[] hash = shA384Managed.Hash; for (int index = 0; index < 8; ++index) { this.Key[index] = hash[index]; } for (int index = 47; index > 39; --index) { this.IV[47 - index] = hash[index]; } break; } case AlgorithmKeyType.SHA512: using (SHA512Managed shA512Managed = new SHA512Managed()) { shA512Managed.ComputeHash(bytes); byte[] hash = shA512Managed.Hash; for (int index = 0; index < 8; ++index) { this.Key[index] = hash[index]; } for (int index = 63; index > 55; --index) { this.IV[63 - index] = hash[index]; } break; } case AlgorithmKeyType.MD5: using (MD5CryptoServiceProvider cryptoServiceProvider = new MD5CryptoServiceProvider()) { cryptoServiceProvider.ComputeHash(bytes); byte[] hash = cryptoServiceProvider.Hash; for (int index = 0; index < 8; ++index) { this.Key[index] = hash[index]; } for (int index = 15; index >= 8; --index) { this.IV[15 - index] = hash[index]; } break; } } }
/// <summary> /// protected virtual byte[] WriteMenoryStream(byte[] data, Version version, AlgorithmType algType, AlgorithmKeyType algKeyType) /// </summary> /// <param name="data"></param> /// <param name="version"></param> /// <param name="algType"></param> /// <param name="algKeyType"></param> /// <returns></returns> protected virtual byte[] WriteMenoryStream(byte[] data, Version version, AlgorithmType algType, AlgorithmKeyType algKeyType) { byte[] value = null; using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter w = new BinaryWriter(ms)) { try { w.Write((Int16)5); w.Write((byte)version); w.Write(Convert.ToByte((int)algType)); w.Write(Convert.ToByte((int)algKeyType)); w.Write(data); w.Flush(); value = ms.ToArray(); } catch (Exception e) { throw e; } finally { w.Close(); ms.Close(); } } } return(value); }