Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Fixed RSA unpadding function
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAmazingAussie committed Sep 23, 2017
1 parent 4a71ecc commit 0f0ec9a
Showing 1 changed file with 50 additions and 8 deletions.
58 changes: 50 additions & 8 deletions Icarus/src/org/alexdev/icarus/encryption/RSA.java
Expand Up @@ -3,7 +3,7 @@
import java.math.BigInteger;

public class RSA {

public BigInteger Exponent;
public BigInteger n;
public BigInteger Private;
Expand All @@ -13,7 +13,7 @@ public class RSA {

private String privateKey;
private String publicKey;

private String modulus;
private boolean canEncrypt = false;
private boolean canDecrypt = false;
Expand Down Expand Up @@ -163,7 +163,7 @@ public String decrypt(String ctext) {
return null;
}

byte[] bytes = this.pkcs1unpad2(m, this.getBlockSize());
byte[] bytes = this.pkcs1unpad2(m);

if (bytes == null) {
return null;
Expand All @@ -174,14 +174,52 @@ public String decrypt(String ctext) {

/**
* Pkcs 1 unpad 2.
*
* Fixed by Joopie. https://github.com/Joopie1994/habbo-encryption/blob/master/HabboEncryption/Security/Cryptography/RSACrypto.cs#L173
* Context: http://forum.ragezone.com/f331/icarus-production-java-server-mysql-1087933-post8823721/#post8823721
*
* @param src the src
* @param n the n
* @return the byte[]
*/
private byte[] pkcs1unpad2(BigInteger src, int n) {
byte[] bytes = src.toByteArray();
byte[] out;
private byte[] pkcs1unpad2(BigInteger bigInteger) {

byte[] src = bigInteger.toByteArray();
byte[] dst = null;

if (src[0] == 2) {

byte[] temp = new byte[src.length + 1];

temp[0] = 0;

for (int i = 0; i < src.length; i++) {
temp[i + 1] = src[i];
}

src = temp;
}

if (src[0] == 0 && src[1] == 2) {

int startIndex = 2;

do
{
if (src.length < startIndex) {
return null;
}
}
while (src[startIndex++] != 0);

dst = new byte[src.length - startIndex];

for (int i = 0; i < dst.length; i++) {
dst[i] = src[i + startIndex];
}
}

/*byte[] out;
int i = 0;
while (i < bytes.length && bytes[i] == 0) {
Expand All @@ -206,10 +244,12 @@ private byte[] pkcs1unpad2(BigInteger src, int n) {
while (++i < bytes.length) {
out[p++] = (bytes[i]);
}
return out*/

return out;
return dst;
}

/**
* Gets the exponent.
*
Expand All @@ -228,6 +268,8 @@ public void setExponent(BigInteger exponent) {
Exponent = exponent;
}



/**
* Gets the n.
*
Expand Down

0 comments on commit 0f0ec9a

Please sign in to comment.