mirror of
https://github.com/openbullet/openbullet.git
synced 2023-10-21 07:33:42 -05:00
Readded RSA Pkcs1Pad2 function
This commit is contained in:
@@ -117,6 +117,9 @@ namespace RuriLib
|
||||
// <summary>Decrypts a string with RSA.</summary>
|
||||
// RSADecrypt,
|
||||
|
||||
/// <summary>Encrypts a string with RSA PKCS1PAD2.</summary>
|
||||
RSAPKCS1PAD2,
|
||||
|
||||
/// <summary>Waits a given amount of milliseconds.</summary>
|
||||
Delay,
|
||||
|
||||
@@ -423,14 +426,19 @@ namespace RuriLib
|
||||
LineParser.SetBool(ref input, this);
|
||||
break;
|
||||
|
||||
/*
|
||||
case Function.RSADecrypt:
|
||||
/*
|
||||
case Function.RSADecrypt:
|
||||
RsaN = LineParser.ParseLiteral(ref input, "Public Key Modulus");
|
||||
RsaD = LineParser.ParseLiteral(ref input, "Private Key Exponent");
|
||||
if (LineParser.Lookahead(ref input) == TokenType.Boolean)
|
||||
LineParser.SetBool(ref input, this);
|
||||
break;
|
||||
*/
|
||||
|
||||
case Function.RSAPKCS1PAD2:
|
||||
RsaN = LineParser.ParseLiteral(ref input, "Public Key Modulus");
|
||||
RsaD = LineParser.ParseLiteral(ref input, "Private Key Exponent");
|
||||
if (LineParser.Lookahead(ref input) == TokenType.Boolean)
|
||||
LineParser.SetBool(ref input, this);
|
||||
RsaE = LineParser.ParseLiteral(ref input, "Public Key Exponent");
|
||||
break;
|
||||
*/
|
||||
|
||||
case Function.GetRandomUA:
|
||||
if (LineParser.ParseToken(ref input, TokenType.Parameter, false, false) == "BROWSER")
|
||||
@@ -579,6 +587,12 @@ namespace RuriLib
|
||||
break;
|
||||
*/
|
||||
|
||||
case Function.RSAPKCS1PAD2:
|
||||
writer
|
||||
.Literal(RsaN)
|
||||
.Literal(RsaE);
|
||||
break;
|
||||
|
||||
case Function.GetRandomUA:
|
||||
if (UserAgentSpecifyBrowser)
|
||||
{
|
||||
@@ -792,6 +806,14 @@ namespace RuriLib
|
||||
break;
|
||||
*/
|
||||
|
||||
case Function.RSAPKCS1PAD2:
|
||||
outputString = Crypto.RSAPkcs1Pad2(
|
||||
localInputString,
|
||||
ReplaceValues(RsaN, data),
|
||||
ReplaceValues(RsaE, data)
|
||||
);
|
||||
break;
|
||||
|
||||
case Function.Delay:
|
||||
try { Thread.Sleep(int.Parse(localInputString)); } catch { }
|
||||
break;
|
||||
|
||||
@@ -4,6 +4,8 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.Numerics;
|
||||
|
||||
namespace RuriLib.Functions.Crypto
|
||||
{
|
||||
@@ -307,6 +309,68 @@ namespace RuriLib.Functions.Crypto
|
||||
oaep
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts a message using RSA with PKCS1PAD2 padding.
|
||||
/// </summary>
|
||||
/// <param name="message">The message as a UTF-8 string</param>
|
||||
/// <param name="modulus">The public key's modulus as a HEX string</param>
|
||||
/// <param name="exponent">The public key's exponent as a HEX string</param>
|
||||
/// <returns>The encrypted message.</returns>
|
||||
// Thanks to TheLittleTrain for this implementation.
|
||||
public static string RSAPkcs1Pad2(string message, string modulus, string exponent)
|
||||
{
|
||||
// Convert the public key components to numbers
|
||||
var n = HexToBigInteger(modulus);
|
||||
var e = HexToBigInteger(exponent);
|
||||
|
||||
// (modulus.ToByteArray().Length - 1) * 8
|
||||
//modulus has 256 bytes multiplied by 8 bits equals 2048
|
||||
var encryptedNumber = Pkcs1Pad2(message, (2048 + 7) >> 3);
|
||||
|
||||
// And now, the RSA encryption
|
||||
encryptedNumber = BigInteger.ModPow(encryptedNumber, e, n);
|
||||
|
||||
//Reverse number and convert to base64
|
||||
var encryptedString = Convert.ToBase64String(encryptedNumber.ToByteArray().Reverse().ToArray());
|
||||
|
||||
return encryptedString;
|
||||
}
|
||||
|
||||
private static BigInteger HexToBigInteger(string hex)
|
||||
{
|
||||
return BigInteger.Parse("00" + hex, NumberStyles.AllowHexSpecifier);
|
||||
}
|
||||
|
||||
private static BigInteger Pkcs1Pad2(string data, int keySize)
|
||||
{
|
||||
if (keySize < data.Length + 11)
|
||||
return new BigInteger();
|
||||
|
||||
var buffer = new byte[256];
|
||||
var i = data.Length - 1;
|
||||
|
||||
while (i >= 0 && keySize > 0)
|
||||
{
|
||||
buffer[--keySize] = (byte)data[i--];
|
||||
}
|
||||
|
||||
// Padding, I think
|
||||
var random = new Random();
|
||||
buffer[--keySize] = 0;
|
||||
while (keySize > 2)
|
||||
{
|
||||
buffer[--keySize] = (byte)random.Next(1, 256);
|
||||
//buffer[--keySize] = 5;
|
||||
}
|
||||
|
||||
buffer[--keySize] = 2;
|
||||
buffer[--keySize] = 0;
|
||||
|
||||
Array.Reverse(buffer);
|
||||
|
||||
return new BigInteger(buffer);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region KDF
|
||||
|
||||
Reference in New Issue
Block a user