System.Security.Cryptography.DES.IsWeakKey C# (CSharp) Метод

IsWeakKey() публичный статический Метод

public static IsWeakKey ( byte rgbKey ) : bool
rgbKey byte
Результат bool
        public static bool IsWeakKey(byte[] rgbKey)
        {
            if (!IsLegalKeySize(rgbKey)) // Also checks for null; same exception
                throw new CryptographicException(SR.Cryptography_InvalidKeySize);

            byte[] rgbOddParityKey = rgbKey.FixupKeyParity();
            UInt64 key = QuadWordFromBigEndian(rgbOddParityKey);
            if ((key == 0x0101010101010101) ||
                (key == 0xfefefefefefefefe) ||
                (key == 0x1f1f1f1f0e0e0e0e) ||
                (key == 0xe0e0e0e0f1f1f1f1))
            {
                return true;
            }

            return false;
        }

Usage Example

Пример #1
0
/*		static DESTransform ()
 *              {
 *                      spBoxes = new uint [64 * 8];
 *
 *                      int [] pBox = new int [32];
 *
 *                      for (int p = 0; p < 32; p++) {
 *                              for (int i = 0; i < 32; i++) {
 *                                      if (p == pTab [i]) {
 *                                              pBox [p] = i;
 *                                              break;
 *                                      }
 *                              }
 *                      }
 *
 *                      for (int s = 0; s < 8; s++) { // for each S-box
 *                              int sOff = s << 6;
 *
 *                              for (int i = 0; i < 64; i++) { // inputs
 *                                      uint sp=0;
 *
 *                                      int indx = (i & 0x20) | ((i & 1) << 4) | ((i >> 1) & 0xF);
 *
 *                                      for (int j = 0; j < 4; j++) { // for each bit in the output
 *                                              if ((sBoxes [sOff + indx] & (8 >> j)) != 0) {
 *                                                      sp |= (uint) (1 << (31 - pBox [(s << 2) + j]));
 *                                              }
 *                                      }
 *
 *                                      spBoxes [sOff + i] = sp;
 *                              }
 *                      }
 *
 *                      leftRotTotal = new byte [leftRot.Length];
 *
 *                      for (int i = 0; i < leftRot.Length; i++) {
 *                              int r = 0;
 *                              for (int j = 0; j <= i; r += leftRot [j++]) {
 *                                      // no statement (confuse the compiler == warning)
 *                              }
 *                              leftRotTotal [i]  = (byte) r;
 *                      }
 *
 *                      InitPermutationTable (ipBits, out ipTab);
 *                      InitPermutationTable (fpBits, out fpTab);
 *              }
 */
        // Default constructor.
        internal DESTransform(SymmetricAlgorithm symmAlgo, bool encryption, byte[] key, byte[] iv)
            : base(symmAlgo, encryption, iv)
        {
            byte[] clonedKey = null;
#if NET_2_0
            if (key == null)
            {
                key       = GetStrongKey();
                clonedKey = key;                 // no need to clone
            }
#endif
            // note: checking (semi-)weak keys also checks valid key length
            if (DES.IsWeakKey(key) || DES.IsSemiWeakKey(key))
            {
                string msg = Locale.GetText("This is a known weak, or semi-weak, key.");
                throw new CryptographicException(msg);
            }

            if (clonedKey == null)
            {
                clonedKey = (byte[])key.Clone();
            }

            keySchedule = new byte [KEY_BYTE_SIZE * 16];
            byteBuff    = new byte [BLOCK_BYTE_SIZE];
            dwordBuff   = new uint [BLOCK_BYTE_SIZE / 4];
            SetKey(clonedKey);
        }
All Usage Examples Of System.Security.Cryptography.DES::IsWeakKey