finished session encrypt/decrypt implementation
This commit is contained in:
parent
915653b035
commit
ccf406a0b3
@ -20,21 +20,51 @@ namespace sessionTestEncrypt
|
|||||||
public long getFutureTime(int seconds){return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (long) seconds;}
|
public long getFutureTime(int seconds){return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + (long) seconds;}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestEncrypt(){
|
public void TestDecrypt(){
|
||||||
var pair1 = PublicKeyBox.GenerateKeyPair();
|
var us = PublicKeyBox.GenerateKeyPair();
|
||||||
byte[] publicK = pair1.PublicKey;
|
var them = PublicKeyBox.GenerateKeyPair();
|
||||||
byte[] privateK = pair1.PrivateKey;
|
|
||||||
var pair = PublicKeyBox.GenerateKeyPair();
|
|
||||||
byte[] privKey = pair.PrivateKey;
|
|
||||||
byte[] pubKey = pair.PublicKey;
|
|
||||||
byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend");
|
byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend");
|
||||||
Session session = new Session(privateK, publicK, true, 5);
|
Session session = new Session(us.PrivateKey, them.PublicKey, true, 5);
|
||||||
session.setMinimumKeyExpireSeconds(10);
|
var ourNew = PublicKeyBox.GenerateKeyPair();
|
||||||
session.setMessageDelay((long) 25);
|
session.addPrivate(ourNew.PrivateKey, getFutureTime(1000));
|
||||||
session.addPublic(pubKey, getFutureTime(100));
|
byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message);
|
||||||
|
Assert.AreEqual(
|
||||||
|
SessionCrypto.decrypt(session, encrypted),
|
||||||
|
message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestDecryptOlderKey(){
|
||||||
|
var us = PublicKeyBox.GenerateKeyPair();
|
||||||
|
var them = PublicKeyBox.GenerateKeyPair();
|
||||||
|
byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend");
|
||||||
|
Session session = new Session(us.PrivateKey, them.PublicKey, true, 5);
|
||||||
|
var ourNew = PublicKeyBox.GenerateKeyPair();
|
||||||
|
var ourNew2 = PublicKeyBox.GenerateKeyPair();
|
||||||
|
session.addPrivate(ourNew.PrivateKey, getFutureTime(1000));
|
||||||
|
byte[] encrypted = Curve25519.encrypt(them.PrivateKey, ourNew.PublicKey, message);
|
||||||
|
session.addPrivate(ourNew2.PrivateKey, getFutureTime(1005));
|
||||||
|
Assert.AreEqual(
|
||||||
|
SessionCrypto.decrypt(session, encrypted),
|
||||||
|
message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestEncrypt(){
|
||||||
|
// Test ephemeral encrypt
|
||||||
|
var us = PublicKeyBox.GenerateKeyPair();
|
||||||
|
var them = PublicKeyBox.GenerateKeyPair();
|
||||||
|
var ephemeral = PublicKeyBox.GenerateKeyPair();
|
||||||
|
byte[] message = UTF8Encoding.UTF8.GetBytes("Hello friend");
|
||||||
|
Session session = new Session(us.PrivateKey, them.PublicKey, true, 5);
|
||||||
|
session.addPublic(ephemeral.PublicKey, getFutureTime(1000));
|
||||||
byte[] encrypted = SessionCrypto.encrypt(session, message);
|
byte[] encrypted = SessionCrypto.encrypt(session, message);
|
||||||
byte[] decrypted = Curve25519.decrypt(privKey, publicK, encrypted);
|
Assert.AreEqual(
|
||||||
Assert.AreEqual(decrypted, message);
|
Curve25519.decrypt(ephemeral.PrivateKey, us.PublicKey, encrypted),
|
||||||
|
message
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,21 @@ namespace chestcrypto.session.crypto{
|
|||||||
return Curve25519.encrypt(privateKey, publicKey, message);
|
return Curve25519.encrypt(privateKey, publicKey, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] decrypt(Session activeSession, byte[] ciphertext){
|
||||||
|
byte[] publicKey = activeSession.getTheirMasterPublic();
|
||||||
|
byte[] decrypted;
|
||||||
|
byte[] privateKey;
|
||||||
|
foreach (var privKey in activeSession.getAllPrivateKeys()){
|
||||||
|
try{
|
||||||
|
privateKey = privKey.Item2;
|
||||||
|
decrypted = Curve25519.decrypt(privateKey, publicKey, ciphertext);
|
||||||
|
return decrypted;
|
||||||
|
}
|
||||||
|
catch(System.Security.Cryptography.CryptographicException){}
|
||||||
|
}
|
||||||
|
throw new System.Security.Cryptography.CryptographicException();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -99,6 +99,8 @@ namespace chestcrypto{
|
|||||||
return key.Item2;
|
return key.Item2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public (long, byte[])[] getAllPrivateKeys(){return ourPrivateKeys.ToArray();}
|
||||||
|
|
||||||
public void addPrivate(byte[] privateKey, long timestamp){
|
public void addPrivate(byte[] privateKey, long timestamp){
|
||||||
validateKeyLength(privateKey);
|
validateKeyLength(privateKey);
|
||||||
validateTimestamp(timestamp);
|
validateTimestamp(timestamp);
|
||||||
|
Loading…
Reference in New Issue
Block a user