public string decrypt(string CipherTxt, int[,] key) { string DecRes = ""; CipherTxt = Utilities.removeSpaces(CipherTxt); int det = Utilities.calculateDeterm(key, key.GetLength(0)); int temp = key.GetLength(0); det = ((det % 26) + 26) % 26; /*if (27 % (26 - det) != 0) * { * return ""; * }*/ int[,] InversedMat = CalculateCoFactorMat(key); InversedMat = Utilities.Transpose(InversedMat); int b = 26 - (27 / (26 - det)); InversedMat = Utilities.elemWiseMult(b, InversedMat); for (int i = 0; i < CipherTxt.Length; i += temp) { int[,] CiConverted = changeStrToVec(CipherTxt.Substring(i, temp)); int[,] PlainText = Utilities.multiply(InversedMat, CiConverted); for (int j = 0; j < PlainText.GetLength(0); j++) { PlainText[j, 0] = ((PlainText[j, 0] % 26) + 26) % 26; } DecRes = string.Concat(DecRes, changeVecToStr(PlainText)); } return(DecRes); }
public string encrypt(string PT, int[,] key) { PT = Utilities.removeSpaces(PT); int temp = key.GetLength(0); string EncRes = ""; for (int i = 0; i < PT.Length; i += temp) { int[,] PTconverted = changeStrToVec(PT.Substring(i, temp)); int[,] cipher = Utilities.multiply(key, PTconverted); for (int j = 0; j < cipher.GetLength(0); j++) { cipher[j, 0] = ((cipher[j, 0] % 26) + 26) % 26; } EncRes = string.Concat(EncRes, changeVecToStr(cipher)); } return(EncRes); }