static void doRecover() { if (!File.Exists(backupToRecover)) { Console.WriteLine($"Backup file {backupToRecover} does not exists. Quitting."); Environment.Exit(1); } Console.Clear(); displayRecoverInitialInfo(); Console.ReadLine(); displayShareHolderInvitation(quorum); Console.ReadLine(); Share[] shares = new Share[quorum]; for (var i = 0; i < shares.Length; i++) { var share = shares[i] = new Share(); share.n = quorum + 1; // just to satisfy validations in SharedSecretGenerator share.k = quorum; readShare(share, quorum, i + 1, true); } var secret = SharedSecretGenerator.joinShares(shares); var encryptedKey = File.ReadAllText(backupToRecover, Encoding.UTF8); var keyJson = SharedSecretGenerator.decryptKey(encryptedKey, shares); var key = JsonConvert.DeserializeObject <KeyGenerator.JwtRsaKey>(keyJson); //========================= Console.Clear(); displayKeyRecoveredInfo(); Console.ReadLine(); Console.Clear(); displayAzureVaultPrompt(); Console.ReadLine(); string token = null; if (!testModeFlag) { token = getToken(tenant).Result.AccessToken; } Console.WriteLine($"Token:\n{token}"); displayVaultImportConfirm(); Console.ReadLine(); if (!testModeFlag) { importKeyToVault(key, token, vaultUrl); } Console.WriteLine("Press [Enter] to continue"); Console.ReadLine(); Console.Clear(); displayFinishInfo(); Console.ReadLine(); }
public void Test1() { var n = 6; var k = 3; var shares = SharedSecretGenerator.generateSharedSecret(32, n, k); Assert.NotNull(shares); Assert.Equal(shares.Length, n); foreach (var share in shares) { Assert.NotNull(share); Assert.NotNull(share.shareValue); Console.WriteLine(String.Format("{0:x08}:{1}|{2}", share.shareIndex, share.shareValue, share.shareHash)); } }
public void Test2() { var key = SharedSecretGenerator.genKey("sec.oper"); var shares = SharedSecretGenerator.generateSharedSecret(32, 6, 3); var encryptedKey = SharedSecretGenerator.encryptKey(key, shares); var secret = PQ.bytes2hex(SharedSecretGenerator.joinShares(shares)); var secretMac = secret.Substring(0, secret.Length / 2); var secretEnc = secret.Substring(secret.Length / 2); Console.WriteLine("Secret:\n" + secret); Console.WriteLine("SecretMac:\n" + secretMac); Console.WriteLine("SecretEnc:\n" + secretEnc); Console.WriteLine("----"); Console.WriteLine("Encrypted key:\n" + encryptedKey); Console.WriteLine("----"); string[] parts = encryptedKey.Split('.'); Console.WriteLine("--Authenticated header:\n" + parts[0]); Console.WriteLine("--Encrypted key:\n" + parts[1]); Console.WriteLine("--IV:\n" + parts[2]); Console.WriteLine("--Cipher text:\n" + parts[3]); Console.WriteLine("--Auth Tag:\n" + parts[4]); Console.WriteLine("----"); var authenticatedHeader = Base64Url.Decode(parts[0]); byte[] iv = Base64Url.Decode(parts[2]); var cipherText = Base64Url.Decode(parts[3]); Console.WriteLine("You can test decryption using followinf openssl command."); Console.WriteLine(String.Format("echo -n {0} | xxd -r -p | openssl enc -aes-128-cbc -d -K {1} -iv {2} ", PQ.bytes2hex(cipherText), secretEnc, PQ.bytes2hex(iv))); var decrypted = SharedSecretGenerator.decryptKey(encryptedKey, shares); Console.WriteLine("Decrypted key: \n" + decrypted); Console.WriteLine("----"); }
static void readShare(Share share, int count, int current, bool withSecretHash = false) { Console.Clear(); Console.WriteLine($@" Share {current} of {count}: Enter your share information. Confirm every entry with [Enter]."); while (true) { while (true) { Console.Write("Enter shareIndex (a number):"); var input = Console.ReadLine(); if (Int32.TryParse(input, out share.shareIndex)) { break; } else { Console.WriteLine("..Bad fromat - enter decimal number"); } } while (true) { Console.Write("Enter shareValue (cvcvc-cvcvc...):"); share.shareValue = Console.ReadLine().ToLower(); if (Regex.IsMatch(share.shareValue, proquintPattern)) { break; } else { Console.WriteLine("Bad share value format ..."); } } while (true) { Console.Write("Enter shareHash (cvcvc-cvcvc):"); share.shareHash = Console.ReadLine().ToLower(); if (Regex.IsMatch(share.shareHash, proquintPattern)) { break; } else { Console.WriteLine("Bad share hash format ..."); } } // check hash of the share var shareHash = SharedSecretGenerator.computeShareHash(share.shareIndex, share.shareValue); if (shareHash.Equals(share.shareHash)) { break; } else { Console.WriteLine("Bad share hash - typing error ? Type all this share info again."); } } if (withSecretHash) { while (true) { Console.Write("Enter secretHash (cvcvc-cvcvc):"); share.secretHash = Console.ReadLine().ToLower(); if (Regex.IsMatch(share.secretHash, proquintPattern)) { break; } else { Console.WriteLine("Bad secret hash format ..."); } } } }
static void doGenerate() { if (File.Exists(output)) { Console.WriteLine($"Output exists ({output}). Quitting."); Environment.Exit(1); } Console.Clear(); displayGenerateInitialInfo(); Console.ReadLine(); var key = SharedSecretGenerator.genKey(kid); var shares = SharedSecretGenerator.generateSharedSecret(32, shareCount, quorum); var encryptedKey = SharedSecretGenerator.encryptKey(key, shares); displayShareHolderInvitation(shares.Length); Console.ReadLine(); var idx = 1; foreach (var share in shares) { var verified = false; while (!verified) { Console.Clear(); displayShare(idx++, share); // verification verified = readVerifyShare(share.shareIndex, share.shareValue, share.shareHash, shareCount, idx); if (!verified) { displayInvalidShare(); } } } Console.Clear(); displayStoreKeyStorePrompt(); Console.ReadLine(); if (!testModeFlag) { File.WriteAllText(output, encryptedKey, Encoding.UTF8); } Console.Clear(); displayAzureVaultPrompt(); Console.ReadLine(); string token = null; if (!testModeFlag) { token = getToken(tenant).Result.AccessToken; } Console.WriteLine($"Token:\n{token}"); displayVaultImportConfirm(); Console.ReadLine(); if (!testModeFlag) { importKeyToVault(key, token, vaultUrl); } Console.WriteLine("Press [Enter] to continue"); Console.ReadLine(); Console.Clear(); displayFinishInfo(); Console.ReadLine(); }
public void NetThread() { SessionIDRequest sessionID = new SessionIDRequest(Storage.Network.UserName, Storage.Network.Password); sessionID.SendRequest(); SharedSecretGenerator sharedSecret = new SharedSecretGenerator(); //random byte[16] gen Timer positionUpdater = new Timer(PositionSender, null, Timeout.Infinite, 50); //create position updater Socket networkSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); networkSocket.Connect(Storage.Network.Server, Storage.Network.Port); _stream = new EnhancedStream(networkSocket); Handshake handshake = new Handshake(_stream); handshake.Send(Storage.Network.UserName, Storage.Network.Server, Storage.Network.Port); // connect Storage.Network.IsConnected = true; while (Storage.Network.IsConnected) { switch (_packetIDbuffer = (byte)_stream.ReadByte()) { case 0x00: KeepAlive keepAlive = new KeepAlive(_stream); break; case 0x01: LoginRequest loginRequest = new LoginRequest(_stream); //positionUpdater.Change(0, 50); ClientSettings clientSettings = new ClientSettings(_stream); clientSettings.Send(); break; case 0x03: ChatMessage chatMessage = new ChatMessage(_stream); break; case 0x04: TimeUpdate timeUpdate = new TimeUpdate(_stream); break; case 0x05: EntityEquipment entityEquipment = new EntityEquipment(_stream); break; case 0x06: SpawnPosition spawnPosition = new SpawnPosition(_stream); break; case 0x08: UpdateHealth updateHealth = new UpdateHealth(_stream); break; case 0x09: RespawnPacket respawnPacket = new RespawnPacket(_stream); break; case 0x0D: _playerPositionLook = new PlayerPositionLook(_stream); break; case 0x10: HeldItemChange heldItemChange = new HeldItemChange(_stream); break; case 0x11: UseBed useBed = new UseBed(_stream); break; case 0x12: Animation animation = new Animation(_stream); break; case 0x14: SpawnNamedEntity spawnNamedEntity = new SpawnNamedEntity(_stream); break; case 0x16: CollectItem collectItem = new CollectItem(_stream); break; case 0x17: SpawnObjectVehicle spawnObjectVehicle = new SpawnObjectVehicle(_stream); break; case 0x18: SpawnMob spawnMob = new SpawnMob(_stream); break; case 0x19: SpawnPainting spawnPainting = new SpawnPainting(_stream); break; case 0x1A: SpawnExperienceOrb spawnExperienceOrb = new SpawnExperienceOrb(_stream); break; case 0x1C: EntityVelocity entityVelocity = new EntityVelocity(_stream); break; case 0x1D: DestroyEntity destroyEntity = new DestroyEntity(_stream); break; case 0x1E: Entity entity = new Entity(_stream); break; case 0x1F: EntityRelativeMove entityRelativeMove = new EntityRelativeMove(_stream); break; case 0x20: EntityLook entityLook = new EntityLook(_stream); break; case 0x21: EntityLookRelativeMove entityLookRelativeMove = new EntityLookRelativeMove(_stream); break; case 0x22: EntityTeleport entityTeleport = new EntityTeleport(_stream); break; case 0x23: EntityHeadLook entityHeadLook = new EntityHeadLook(_stream); break; case 0x26: EntityStatus entityStatus = new EntityStatus(_stream); break; case 0x27: AttachEntity attachEntity = new AttachEntity(_stream); break; case 0x28: EntityMetadata entityMetadata = new EntityMetadata(_stream); break; case 0x29: EntityEffect entityEffect = new EntityEffect(_stream); break; case 0x2A: RemoveEntityEffect removeEntityEffect = new RemoveEntityEffect(_stream); break; case 0x2B: SetExperience setExperience = new SetExperience(_stream); break; case 0x33: ChunkData mapChunk = new ChunkData(_stream); break; case 0x34: MultiBlockChange multiBlockChange = new MultiBlockChange(_stream); break; case 0x35: BlockChange blockChange = new BlockChange(_stream); break; case 0x36: BlockAction blockAction = new BlockAction(_stream); break; case 0x37: BlockBreakAnimation blockBreakAnimation = new BlockBreakAnimation(_stream); break; case 0x38: MapChunkBulk mapChunkBulk = new MapChunkBulk(_stream); break; case 0x3C: Explosion explosion = new Explosion(_stream); break; case 0x3D: SoundParticleEffect soundParticleEffect = new SoundParticleEffect(_stream); break; case 0x3E: NamedSoundEffect namedSoundEffect = new NamedSoundEffect(_stream); break; case 0x46: ChangeGameState changeGameState = new ChangeGameState(_stream); break; case 0x47: Thunderbolt thunderbolt = new Thunderbolt(_stream); break; case 0x64: OpenWindow openWindow = new OpenWindow(_stream); break; case 0x65: CloseWindow closeWindow = new CloseWindow(_stream); break; case 0x67: SetSlot setSlot = new SetSlot(_stream); break; case 0x68: SetWindowItems setWindowItems = new SetWindowItems(_stream); break; case 0x69: UpdateWindowProperty updateWindowProperty = new UpdateWindowProperty(_stream); break; case 0x6A: ConfirmTransaction confirmTransaction = new ConfirmTransaction(_stream); break; case 0x6B: CreativeInventoryAction creativeInventoryAction = new CreativeInventoryAction(_stream); break; case 0x6C: EnchantItem enchantItem = new EnchantItem(_stream); break; case 0x82: UpdateSign updateSign = new UpdateSign(_stream); break; case 0x83: ItemData itemData = new ItemData(_stream); break; case 0x84: UpdateTileEntity updateTileEntity = new UpdateTileEntity(_stream); break; case 0xC8: IncrementStatistic incrementStatistic = new IncrementStatistic(_stream); break; case 0xC9: PlayerListItem playerListItem = new PlayerListItem(_stream); break; case 0xCA: PlayerAbilities playerAbilities = new PlayerAbilities(_stream); break; case 0xCB: TabComplete tabcomplete = new TabComplete(_stream); break; case 0xFA: PluginMessage pluginMessage = new PluginMessage(_stream); break; case 0xFC: EncryptionKeyResponse encryptionKeyResponse = new EncryptionKeyResponse(_stream); encryptionKeyResponse.Get(); _stream = new AesStream(networkSocket, _stream, sharedSecret.Get); ClientStatuses clientStatuses = new ClientStatuses(_stream); clientStatuses.Send(0); break; case 0xFD: EncryptionKeyRequest encryptionKeyRequest = new EncryptionKeyRequest(_stream, sharedSecret.Get, sessionID.GetID(), Storage.Network.UserName); // encryptionKeyResponse = new EncryptionKeyResponse(_stream); encryptionKeyResponse.Send(encryptionKeyRequest.GetEncSharedSecret(), encryptionKeyRequest.GetEncToken()); break; case 0xFF: positionUpdater = null; DisconnectKick disconnectKick = new DisconnectKick(_stream); networkSocket.Disconnect(false); break; default: throw new Exception("We got a Unknown Packet (" + _packetIDbuffer + ")from the Server. This should not happen: Error in Packet parser"); } } }