public static string NDESEncrypt(string encryptString, string encKey, string number) { int[] divisors = NDes.Getdivisor(number); //int[] divisors = new int[] { 1, 3 }; int m = int.Parse(number); int i, j; string cipherresult = string.Empty; string t = string.Empty; string subkey = string.Empty; for (i = 1; i <= m; i++) { if (i <= divisors.Length) { if (i % 2 == 0) { subkey = rightmovekey(encKey, divisors[i - 1]); t = encryptString; cipherresult = DES.DEScipher.Encrypt(t, subkey); encryptString = cipherresult; } else { subkey = leftmovekey(encKey, divisors[i - 1]); t = encryptString; cipherresult = DES.DEScipher.Encrypt(t, subkey); encryptString = cipherresult; } } else { j = i; do { j = j - divisors.Length; } while (j > divisors.Length); if (i % 2 == 0) { subkey = NDes.rightmovekey(encKey, divisors[j - 1]); t = encryptString; cipherresult = DES.DEScipher.Encrypt(t, subkey); encryptString = cipherresult; } else { subkey = NDes.leftmovekey(encKey, divisors[j - 1]); t = encryptString; cipherresult = DES.DEScipher.Encrypt(t, subkey); encryptString = cipherresult; } } } return(encryptString); }
public static string NDESDecrypt(string decryptString, string encKey, string number) { int[] divisors = NDes.Getdivisor(number); int m = int.Parse(number); int i, j; string decipherresult = string.Empty; string t = string.Empty; string subkey = string.Empty; for (i = m; i > 0; i--) { if (i <= divisors.Length) { if (i % 2 == 0) { subkey = rightmovekey(encKey, divisors[i - 1]); t = decryptString; decipherresult = DES.DEScipher.Decrypt(t, subkey); decryptString = decipherresult; } else { subkey = leftmovekey(encKey, divisors[i - 1]); t = decryptString; decipherresult = DES.DEScipher.Decrypt(t, subkey); decryptString = decipherresult; } } else { j = i; do { j = j - divisors.Length; } while (j > divisors.Length); if (i % 2 == 0) { subkey = NDes.rightmovekey(encKey, divisors[j - 1]); t = decryptString; decipherresult = DES.DEScipher.Decrypt(t, subkey); decryptString = decipherresult; } else { subkey = NDes.leftmovekey(encKey, divisors[j - 1]); t = decryptString; decipherresult = DES.DEScipher.Decrypt(t, subkey); decryptString = decipherresult; } } } return(decryptString); }
public static string rightmovekey(string k1, int n) { string str1 = string.Empty; string str2 = string.Empty; string stringresult = string.Empty; byte[] byte1 = Encoding.UTF8.GetBytes(k1); string k = NDes.BytesToBit(byte1); str1 = k.Substring(0, k.Length - n); str2 = k.Substring(k.Length - n, n); stringresult = str2 + str1; string stringresult2 = NDes.BitToBytes(stringresult); return(stringresult2); }