public RSAConfig(byte[] modulus, byte[] exponent, int modulusSize) { _ModulusOctets = modulusSize / 8; _E = RSAHelper.OS2IP(exponent, false); _N = RSAHelper.OS2IP(modulus, false); _Has_PUBLIC_Info = true; }
private byte[] GenerateMask(byte[] seed, int outputLength) { if (outputLength > (Math.Pow(2, 32))) { throw new ArgumentException("Mask cannot exceed 2^32."); } else { var result = new List <byte>(); for (var i = 0; i <= outputLength / _rsaConfig.hLen; i++) { var data = new List <byte>(); data.AddRange(seed); data.AddRange(RSAHelper.I2OSP(i, 4, false)); result.AddRange(_rsaConfig.ComputeHash(data.ToArray())); } if (outputLength <= result.Count) { return(result.GetRange(0, outputLength).ToArray()); } else { throw new ArgumentException("mask length invalid."); } } }
public byte[] CrunchRSA(byte[] plainText, bool usePrivate) { if (usePrivate && (!_rsaConfig.Has_PRIVATE_Info)) { throw new CryptographicException("No private key"); } if ((usePrivate == false) && (!_rsaConfig.Has_PUBLIC_Info)) { throw new CryptographicException("No public key"); } BigInteger e; if (usePrivate) { e = _rsaConfig.D; } else { e = _rsaConfig.E; } var PT = RSAHelper.OS2IP(plainText, false); var M = BigInteger.ModPow(PT, e, _rsaConfig.N); return(M.Sign == -1 ? RSAHelper.I2OSP(M + _rsaConfig.N, _rsaConfig.OctetsInModulus, false) : RSAHelper.I2OSP(M, _rsaConfig.OctetsInModulus, false)); }
public RSAConfig(byte[] modulus, byte[] exponent, byte[] d, int modulusSize) { // _rsaConfig; _ModulusOctets = modulusSize / 8; _E = RSAHelper.OS2IP(exponent, false); _N = RSAHelper.OS2IP(modulus, false); _D = RSAHelper.OS2IP(d, false); _Has_PUBLIC_Info = true; _Has_PRIVATE_Info = true; }
public RSAConfig(byte[] modulus, byte[] exponent, byte[] d, byte[] P, byte[] Q, byte[] DP, byte[] DQ, byte[] inverseQ, int modulusSize) { _ModulusOctets = modulusSize / 8; _E = RSAHelper.OS2IP(exponent, false); _N = RSAHelper.OS2IP(modulus, false); _D = RSAHelper.OS2IP(d, false); _P = RSAHelper.OS2IP(P, false); _Q = RSAHelper.OS2IP(Q, false); _DP = RSAHelper.OS2IP(DP, false); _DQ = RSAHelper.OS2IP(DQ, false); _InverseQ = RSAHelper.OS2IP(inverseQ, false); _Has_CRT_Info = true; _Has_PUBLIC_Info = true; _Has_PRIVATE_Info = true; }
public RSAConfig(RSAParameters rsaParams, int ModulusSize) { _ModulusOctets = ModulusSize / 8; _E = RSAHelper.OS2IP(rsaParams.Exponent, false); _D = RSAHelper.OS2IP(rsaParams.D, false); _N = RSAHelper.OS2IP(rsaParams.Modulus, false); _P = RSAHelper.OS2IP(rsaParams.P, false); _Q = RSAHelper.OS2IP(rsaParams.Q, false); _DP = RSAHelper.OS2IP(rsaParams.DP, false); _DQ = RSAHelper.OS2IP(rsaParams.DQ, false); _InverseQ = RSAHelper.OS2IP(rsaParams.InverseQ, false); _Has_CRT_Info = true; _Has_PUBLIC_Info = true; _Has_PRIVATE_Info = true; }
public byte[] PrivateDecryptFast(byte[] data) { if (!_rsaConfig.Has_PRIVATE_Info || !_rsaConfig.HasCRTInfo) { throw new CryptographicException("No private key"); } else { var c = RSAHelper.OS2IP(data, false); var m1 = BigInteger.ModPow(c, _rsaConfig.DP, _rsaConfig.P); var m2 = BigInteger.ModPow(c, _rsaConfig.DQ, _rsaConfig.Q); var h = ((m1 - m2) * _rsaConfig.InverseQ) % _rsaConfig.P; var m = (m2 + (_rsaConfig.Q * h)); return(m.Sign == -1 ? RSAHelper.I2OSP(m + _rsaConfig.N, _rsaConfig.OctetsInModulus, false) : RSAHelper.I2OSP(m, _rsaConfig.OctetsInModulus, false)); } }
private byte[] EncodeOAEP(byte[] message, byte[] p, bool usePrivate) { var mLen = message.Length; if (mLen > _rsaConfig.OctetsInModulus - 2 * _rsaConfig.hLen - 2) { throw new ArgumentException("message too long."); } else { var ps = new byte[_rsaConfig.OctetsInModulus - mLen - 2 * _rsaConfig.hLen - 2]; var pHash = _rsaConfig.ComputeHash(p); var db = new List <byte>(); db.AddRange(pHash); db.AddRange(ps); db.Add(0x01); db.AddRange(message); var dbArr = db.ToArray(); var seed = new byte[_rsaConfig.hLen]; _randomGen.GetBytes(seed); var dbMask = GenerateMask(seed, _rsaConfig.OctetsInModulus - _rsaConfig.hLen - 1); var maskedDB = RSAHelper.XOR(dbArr, dbMask); var seedMask = GenerateMask(maskedDB, _rsaConfig.hLen); var maskedSeed = RSAHelper.XOR(seed, seedMask); var result = new List <byte>(); result.Add(0x00); result.AddRange(maskedSeed); result.AddRange(maskedDB); return(CrunchRSA(result.ToArray(), usePrivate)); } }
private byte[] Decrypt(byte[] message, byte[] parameters, bool usePrivate, bool fOAEP) { var EM = new byte[0]; try { if ((usePrivate == true) && (FastPrivateDecrypt) && (_rsaConfig.HasCRTInfo)) { EM = PrivateDecryptFast(message); } else { EM = CrunchRSA(message, usePrivate); } } catch (CryptographicException ex) { throw new CryptographicException("Exception while Decryption: " + ex.Message); } catch { throw new Exception("Exception while Decryption: "); } try { if (fOAEP) { if ((EM.Length == _rsaConfig.OctetsInModulus) && (EM.Length > (2 * _rsaConfig.hLen + 1))) { byte[] maskedSeed; byte[] maskedDB; byte[] pHash = _rsaConfig.ComputeHash(parameters); if (EM[0] == 0) { maskedSeed = EM.ToList().GetRange(1, _rsaConfig.hLen).ToArray(); maskedDB = EM.ToList().GetRange(1 + _rsaConfig.hLen, EM.Length - _rsaConfig.hLen - 1).ToArray(); var seedMask = GenerateMask(maskedDB, _rsaConfig.hLen); var seed = RSAHelper.XOR(maskedSeed, seedMask); var dbMask = GenerateMask(seed, _rsaConfig.OctetsInModulus - _rsaConfig.hLen - 1); var DB = RSAHelper.XOR(maskedDB, dbMask); if (DB.Length >= (_rsaConfig.hLen + 1)) { var _pHash = DB.ToList().GetRange(0, _rsaConfig.hLen).ToArray(); var PS_M = DB.ToList().GetRange(_rsaConfig.hLen, DB.Length - _rsaConfig.hLen); var pos = PS_M.IndexOf(0x01); if (pos >= 0 && (pos < PS_M.Count)) { var _01_M = PS_M.GetRange(pos, PS_M.Count - pos); byte[] M; if (_01_M.Count > 1) { M = _01_M.GetRange(1, _01_M.Count - 1).ToArray(); } else { M = new byte[0]; } var success = true; for (var i = 0; i < _rsaConfig.hLen; i++) { if (_pHash[i] != pHash[i]) { success = false; break; } } if (success) { return(M); } else { M = new byte[_rsaConfig.OctetsInModulus]; throw new CryptographicException("OAEP Decode Error"); } } else { throw new CryptographicException("OAEP Decode Error"); } } else { throw new CryptographicException("OAEP Decode Error"); } } else { throw new CryptographicException("OAEP Decode Error"); } } else { throw new CryptographicException("OAEP Decode Error"); } } else { if (EM.Length >= 11) { if ((EM[0] == 0x00) && (EM[1] == 0x02)) { var startIndex = 2; var PS = new List <byte>(); for (var i = startIndex; i < EM.Length; i++) { if (EM[i] != 0) { PS.Add(EM[i]); } else { break; } } if (PS.Count >= 8) { var DecodedDataIndex = startIndex + PS.Count + 1; if (DecodedDataIndex < (EM.Length - 1)) { var DATA = new List <byte>(); for (int i = DecodedDataIndex; i < EM.Length; i++) { DATA.Add(EM[i]); } return(DATA.ToArray()); } else { return(new byte[0]); } } else { throw new CryptographicException("PKCS v1.5 Decode Error"); } } else { throw new CryptographicException("PKCS v1.5 Decode Error"); } } else { throw new CryptographicException("PKCS v1.5 Decode Error"); } } } catch (CryptographicException ex) { throw new CryptographicException("Exception while decoding: " + ex.Message); } catch { throw new CryptographicException("Exception while decoding"); } }
public RSACrypto(String keyInfo, int ModulusSize) { this._rsaConfig = RSAHelper.GetRSAConfig(keyInfo, ModulusSize); FastPrivateDecrypt = true; }