123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
- using System;
- namespace Org.BouncyCastle.Crypto.Parameters
- {
- public class DesParameters
- : KeyParameter
- {
- public DesParameters(
- byte[] key)
- : base(key)
- {
- if (IsWeakKey(key))
- throw new ArgumentException("attempt to create weak DES key");
- }
- public DesParameters(
- byte[] key,
- int keyOff,
- int keyLen)
- : base(key, keyOff, keyLen)
- {
- if (IsWeakKey(key, keyOff))
- throw new ArgumentException("attempt to create weak DES key");
- }
-
- public const int DesKeyLength = 8;
-
- private const int N_DES_WEAK_KEYS = 16;
- private static readonly byte[] DES_weak_keys =
- {
-
- (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01, (byte)0x01,(byte)0x01,(byte)0x01,(byte)0x01,
- (byte)0x1f,(byte)0x1f,(byte)0x1f,(byte)0x1f, (byte)0x0e,(byte)0x0e,(byte)0x0e,(byte)0x0e,
- (byte)0xe0,(byte)0xe0,(byte)0xe0,(byte)0xe0, (byte)0xf1,(byte)0xf1,(byte)0xf1,(byte)0xf1,
- (byte)0xfe,(byte)0xfe,(byte)0xfe,(byte)0xfe, (byte)0xfe,(byte)0xfe,(byte)0xfe,(byte)0xfe,
-
- (byte)0x01,(byte)0xfe,(byte)0x01,(byte)0xfe, (byte)0x01,(byte)0xfe,(byte)0x01,(byte)0xfe,
- (byte)0x1f,(byte)0xe0,(byte)0x1f,(byte)0xe0, (byte)0x0e,(byte)0xf1,(byte)0x0e,(byte)0xf1,
- (byte)0x01,(byte)0xe0,(byte)0x01,(byte)0xe0, (byte)0x01,(byte)0xf1,(byte)0x01,(byte)0xf1,
- (byte)0x1f,(byte)0xfe,(byte)0x1f,(byte)0xfe, (byte)0x0e,(byte)0xfe,(byte)0x0e,(byte)0xfe,
- (byte)0x01,(byte)0x1f,(byte)0x01,(byte)0x1f, (byte)0x01,(byte)0x0e,(byte)0x01,(byte)0x0e,
- (byte)0xe0,(byte)0xfe,(byte)0xe0,(byte)0xfe, (byte)0xf1,(byte)0xfe,(byte)0xf1,(byte)0xfe,
- (byte)0xfe,(byte)0x01,(byte)0xfe,(byte)0x01, (byte)0xfe,(byte)0x01,(byte)0xfe,(byte)0x01,
- (byte)0xe0,(byte)0x1f,(byte)0xe0,(byte)0x1f, (byte)0xf1,(byte)0x0e,(byte)0xf1,(byte)0x0e,
- (byte)0xe0,(byte)0x01,(byte)0xe0,(byte)0x01, (byte)0xf1,(byte)0x01,(byte)0xf1,(byte)0x01,
- (byte)0xfe,(byte)0x1f,(byte)0xfe,(byte)0x1f, (byte)0xfe,(byte)0x0e,(byte)0xfe,(byte)0x0e,
- (byte)0x1f,(byte)0x01,(byte)0x1f,(byte)0x01, (byte)0x0e,(byte)0x01,(byte)0x0e,(byte)0x01,
- (byte)0xfe,(byte)0xe0,(byte)0xfe,(byte)0xe0, (byte)0xfe,(byte)0xf1,(byte)0xfe,(byte)0xf1
- };
-
- public static bool IsWeakKey(
- byte[] key,
- int offset)
- {
- if (key.Length - offset < DesKeyLength)
- throw new ArgumentException("key material too short.");
-
- for (int i = 0; i < N_DES_WEAK_KEYS; i++)
- {
- bool unmatch = false;
- for (int j = 0; j < DesKeyLength; j++)
- {
- if (key[j + offset] != DES_weak_keys[i * DesKeyLength + j])
- {
-
- unmatch = true;
- break;
- }
- }
- if (!unmatch)
- {
- return true;
- }
- }
- return false;
- }
- public static bool IsWeakKey(
- byte[] key)
- {
- return IsWeakKey(key, 0);
- }
- public static byte SetOddParity(byte b)
- {
- uint parity = b ^ 1U;
- parity ^= (parity >> 4);
- parity ^= (parity >> 2);
- parity ^= (parity >> 1);
- parity &= 1U;
- return (byte)(b ^ parity);
- }
-
- public static void SetOddParity(byte[] bytes)
- {
- for (int i = 0; i < bytes.Length; i++)
- {
- bytes[i] = SetOddParity(bytes[i]);
- }
- }
- public static void SetOddParity(byte[] bytes, int off, int len)
- {
- for (int i = 0; i < len; i++)
- {
- bytes[off + i] = SetOddParity(bytes[off + i]);
- }
- }
- }
- }
- #endif
|