public static CipheredStringMessage EncryptString(this StringMessage stringMessage, KeyFactory keyFactory) { var hash = stringMessage.Hash ?? throw new NullReferenceException(); var keyP = keyFactory?.PublicKeyP?.KeyValue ?? throw new NullReferenceException(); var keyG = keyFactory?.PublicKeyG?.KeyValue ?? throw new NullReferenceException(); var keyA = keyFactory?.PrivateKeyA?.KeyValue ?? throw new NullReferenceException(); var keyH = keyFactory?.PublicKeyH?.KeyValue ?? throw new NullReferenceException(); var keyR = keyFactory?.RandomKeyR?.KeyValue ?? throw new NullReferenceException(); var cipheredStringMessage = new CipheredStringMessage(); var Message = new BigInteger(stringMessage.Hash); cipheredStringMessage.HashC1 = (keyG.modPow(keyR, keyP)) .getBytes(); cipheredStringMessage.HashC2 = (Message * keyH.modPow(keyR, keyP)) .getBytes(); return(cipheredStringMessage); }
public static StringMessage DecryptString(this CipheredStringMessage cipheredStringMessage, KeyFactory keyFactory) { var c1 = cipheredStringMessage.HashC1 ?? throw new NullReferenceException(); var c2 = cipheredStringMessage.HashC2 ?? throw new NullReferenceException(); var keyA = keyFactory?.PrivateKeyA?.KeyValue ?? throw new NullReferenceException(); var keyP = keyFactory?.PublicKeyP?.KeyValue ?? throw new NullReferenceException(); var stringMessage = new StringMessage(); BigInteger DecryptedFileNum = new BigInteger(0); BigInteger bufferC1 = new BigInteger(c1); BigInteger bufferC2 = new BigInteger(c2); DecryptedFileNum = (bufferC2 * 1 / bufferC1.modPow(keyA, keyP)); stringMessage.Hash = DecryptedFileNum.getBytes(); stringMessage.VisibleText = Encoding.ASCII.GetString(stringMessage.Hash); return(stringMessage); }