/// <summary>Generate a SHA3 Hash/Key depend on password input as String.</summary> /// <remarks>Generate a SHA3 Hash/Key depend on password input as String.</remarks> /// <param name="keySize">Size of hash. Allowed are 224, 256, 384 and 512.</param> /// <param name="password">String password which will be hashed</param> /// <returns>SHA3 hash as Hex String</returns> public virtual string generateKey(int keySize, string password) { byte[] bytePW = Encoding.UTF8.GetBytes(password); KeyGenSHA3.init(keySize); KeyGenSHA3.update(bytePW, bytePW.Length * 8); string output = CryptobyHelper.bytesToHexString(KeyGenSHA3.getHash()); return(output); }
/// <summary>Generate a random SHA3 Hash/Key as String.</summary> /// <remarks>Generate a random SHA3 Hash/Key as String.</remarks> /// <param name="keySize">Size of hash. Allowed are 224, 256, 384 and 512.</param> /// <returns>SHA3 hash as Hex String</returns> public virtual string generateKey(int keySize) { SecureRandom scRandom = new SecureRandom(); byte[] randomPW = new byte[40]; scRandom.nextBytes(randomPW); KeyGenSHA3.init(keySize); KeyGenSHA3.update(randomPW, randomPW.Length * 8); string output = CryptobyHelper.bytesToHexString(KeyGenSHA3.getHash()); return(output); }
/// <summary>Generate RSA Private and Public Key with Size of keyBitSize.</summary> /// <remarks>Generate RSA Private and Public Key with Size of keyBitSize.</remarks> /// <param name="keyBitSize"> /// Size of RSA key which will be generated. Allowed are /// 1024, 2048 and 4096 /// </param> public virtual void initGenerator(int keyBitSize) { if (keyBitSize != 1024 && keyBitSize != 2048 && keyBitSize != 4096) { throw new ArgumentException("Just Keys with Size of 1024,2048 or 4096 are allowed!" ); } generateKeys(keyBitSize); // Generate Public Key to Hex String pubKeyByte = n.toByteArray(); pubKey = CryptobyHelper.bytesToHexString(pubKeyByte); byte[] dByte = d.toByteArray(); privKeyByte = new byte[dByte.Length + pubKeyByte.Length]; // Copy D ByteArray into first Part and N ByteArray into second Part Array.Copy(dByte, 0, privKeyByte, 0, dByte.Length); Array.Copy(pubKeyByte, 0, privKeyByte, dByte.Length, pubKeyByte.Length); // Generate Private Key to Hex String privKey = CryptobyHelper.bytesToHexString(privKeyByte); }
public virtual void testPutAndGetKey() { System.Console.Out.WriteLine("Put and Get Keys"); string publicKeyFilePath = "publicKey.pub"; string privateKeyFilePath = "privateKey.prv"; int keySize = 1024; CryptobyClient client = new CryptobyClient(); CryptobyCore core = new CryptobyCore(client); KeyGenRSA generator = new KeyGenRSA(core); generator.initGenerator(keySize); byte[] publicKeyByte = generator.getPublicKeyByte(); byte[] privateKeyByte = generator.getPrivateKeyByte(); string publicKey = generator.getPublicKey(); string privateKey = generator.getPrivateKey(); try { CryptobyFileManager.putKeyToFile(publicKeyFilePath, publicKey); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } try { CryptobyFileManager.putKeyToFile(privateKeyFilePath, privateKey); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } byte[] resultPublic = null; try { resultPublic = CryptobyFileManager.getKeyFromFile(publicKeyFilePath); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } byte[] resultPrivate = null; try { resultPrivate = CryptobyFileManager.getKeyFromFile(privateKeyFilePath); } catch (IOException ex) { Logger.getLogger(typeof(CryptobyFileManagerTest).FullName).log(Level.SEVERE, null , ex); } Assert.AreEqual(publicKeyByte, resultPublic); Assert.AreEqual(privateKeyByte, resultPrivate); Assert.AreEqual(publicKey, CryptobyHelper.bytesToHexString(resultPublic )); Assert.AreEqual(privateKey, CryptobyHelper.bytesToHexString(resultPrivate )); }