DES.cs :  » Network-Clients » SharpPrivacyLibrary » SharpPrivacy » Cipher » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » SharpPrivacyLibrary 
SharpPrivacyLibrary » SharpPrivacy » Cipher » DES.cs
//
// System.Security.Cryptography.DES
//
// Author:
//   Sergey Chaban (serge@wildwestsoftware.com)
//   Sebastien Pouliot (spouliot@motus.com)
//
// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
//
// Modified by Daniel Fabian to fit SharpPrivacy's needs.
// This file is part of the SharpPrivacy source code contribution.
// Get get the original SymmetricAlgorithm class, please visit the
// mono project at http://www.go-mono.com.
//

using System;

// References:
// a.  FIPS PUB 46-3: Data Encryption Standard
//  http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf

namespace SharpPrivacy.Cipher{

public abstract class DES : SymmetricAlgorithm {

  private static int blockSizeByte = 8;

  public DES () {
    KeySizeValue = 64; 
    BlockSizeValue = 64; 
    FeedbackSizeValue = 64;

    LegalKeySizesValue = new KeySizes[1];
    LegalKeySizesValue[0] = new KeySizes(64, 64, 0);

    LegalBlockSizesValue = new KeySizes[1];
    LegalBlockSizesValue[0] = new KeySizes(64, 64, 0);
  }

  public static new DES Create () {
    return (DES)Activator.CreateInstance(Type.GetType("SharpPrivacy.Cipher.DESCryptoServiceProvider"), null);
  }

  internal static ulong PackKey (byte [] key) {
    ulong res = 0;
    for (int i = 0, sh = 8*blockSizeByte; (sh = sh - 8) >= 0; i++) {
      res |= (ulong) key [i] << sh;
    }
    return res;
  }

  internal static byte [] UnpackKey (ulong key) {
    byte [] res = new byte [blockSizeByte];
    for (int i = 0, sh = 8*blockSizeByte; (sh = sh - 8) >= 0; i++) {
      res [i] = (byte) (key >> sh);
    }
    return res;
  }

  // Ek(Ek(m)) = m
  internal static ulong [] weakKeys = {
    0x0101010101010101, /* 0000000 0000000 */
    0xFEFEFEFEFEFEFEFE, /* FFFFFFF FFFFFFF */
    0x1F1F1F1FE0E0E0E0, /* 0000000 FFFFFFF */
    0xE0E0E0E01F1F1F1F  /* FFFFFFF 0000000 */
  };

  // Ek1(Ek2(m)) = m
  internal static ulong [] semiweakKeys = {
    0x01FE01FE01FE01FE, 0xFE01FE01FE01FE01,
    0x1FE01FE00EF10EF1, 0xE01FE01FF10EF10E,
    0x01E001E001F101F1, 0xE001E001F101F101,
    0x1FFE1FFE0EFE0EFE, 0xFE1FFE1FFE0EFE0E,
    0x011F011F010E010E, 0x1F011F010E010E01,
    0xE0FEE0FEF1FEF1FE, 0xFEE0FEE0FEF1FEF1
  };

  public static bool IsWeakKey (byte [] rgbKey) {
    if (rgbKey.Length == (blockSizeByte >> 3))
      throw new System.Security.Cryptography.CryptographicException ("Wrong Key Length");

    ulong lk = PackKey (rgbKey);
    foreach (ulong wk in weakKeys) {
      if (lk == wk) return true;
    }
    return false;
  }

  public static bool IsSemiWeakKey (byte [] rgbKey) {
    if (rgbKey.Length == (blockSizeByte >> 3))
      throw new System.Security.Cryptography.CryptographicException ("Wrong Key Length");

    ulong lk = PackKey (rgbKey);
    foreach (ulong swk in semiweakKeys) {
      if (lk == swk) return true;
    }
    return false;
  }

  public override byte[] Key {
    get { return base.Key; }
    set {
      if (value == null)
        throw new ArgumentNullException ();
      if (IsWeakKey (value) || IsSemiWeakKey (value))
        throw new System.Security.Cryptography.CryptographicException ();
      base.Key = value;
    }
  }

} // DES

} // System.Security.Cryptography
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.