public void SaveKeyPair(KeyPair keyPair, string fileName) { XmlSerializer xmls = new XmlSerializer(typeof(KeyPair)); using (FileStream fs = File.Create(fileName)) { xmls.Serialize(fs, keyPair); } }
public KeyPair CreateEmptyKeyPair() { KeyPair keyPair = new KeyPair(); keyPair.PrivateKey = new byte[PrivateKeySize]; keyPair.PublicKey = new byte[PublicKeySize]; for (int i = 0; i < PrivateKeySize; i++) { keyPair.PrivateKey[i] = 0xFF; } for (int i = 0; i < PublicKeySize; i++) { keyPair.PublicKey[i] = 0xFF; } return keyPair; }
public KeyPair CreateKeyPair() { byte[] seed = new byte[RandomSeedSize + 2*sizeof(UInt16)]; byte[] keyPrivate = new byte[PrivateKeySize]; byte[] keyPublic = new byte[PublicKeySize]; ushort delta1, delta2; Random random = new Random(); for (int i = 0; i < 100; i++) { random.NextBytes(seed); //I'm not sure what this does, it was ported from MetaDataProcessor if (CryptoWrapper.Crypto_CreateZenithKey(seed, out delta1, out delta2) != (int)dotNetMFCrypto.CryptoWrapper.CRYPTO_RESULT.SUCCESS) continue; byte []d0 = BitConverter.GetBytes(delta1); byte []d1 = BitConverter.GetBytes(delta2); seed[RandomSeedSize ] = d0[0]; seed[RandomSeedSize + 1] = d0[1]; seed[RandomSeedSize + 2] = d1[0]; seed[RandomSeedSize + 3] = d1[1]; if (CryptoWrapper.Crypto_GeneratePrivateKey(seed, keyPrivate) != (int)dotNetMFCrypto.CryptoWrapper.CRYPTO_RESULT.SUCCESS) continue; if (CryptoWrapper.Crypto_PublicKeyFromPrivate(keyPrivate, keyPublic) != (int)dotNetMFCrypto.CryptoWrapper.CRYPTO_RESULT.SUCCESS) continue; KeyPair keyPair = new KeyPair(); keyPair.PrivateKey = keyPrivate; keyPair.PublicKey = keyPublic; return keyPair; } throw new ApplicationException("Could not generate key pair"); }
public string FormatPublicKey(KeyPair keyPair) { string retVal; int i = 0; int j = 0; retVal = "// exponent length\r\n"; foreach (byte b in keyPair.PublicKey) { if (i == 4) { retVal += "\r\n\r\n// module\r\n"; j = 0; } if (j != 0 && (0 == (j % 10))) retVal += "\r\n"; if (i == 132) { retVal += "\r\n\r\n// exponent\r\n"; j = 0; } retVal += string.Format("0x{0:x02}, ", b); j++; i++; } return retVal; }