break stuff
This commit is contained in:
parent
4017fbc580
commit
90813e96ba
|
@ -37,12 +37,12 @@ dependencies {
|
|||
teavm {
|
||||
|
||||
compileScopes = null;
|
||||
minifying = true;
|
||||
minifying = false;
|
||||
maxTopLevelNames = 10000;
|
||||
properties = null;
|
||||
debugInformationGenerated = false;
|
||||
sourceMapsGenerated = true;
|
||||
sourceFilesCopied = false;
|
||||
sourceFilesCopied = true;
|
||||
incremental = false;
|
||||
transformers = null;
|
||||
|
||||
|
|
409521
javascript/classes.js
409521
javascript/classes.js
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -2,6 +2,7 @@
|
|||
<head>
|
||||
<title>eagler</title>
|
||||
<script type="text/javascript" src="classes.js"></script>
|
||||
<!--script src="https://unpkg.com/@peculiar/x509"></script-->
|
||||
<script type="text/javascript">
|
||||
window.addEventListener("load", function(){ window.minecraftOpts = [
|
||||
"game_frame","assets.epk",
|
||||
|
|
|
@ -5,25 +5,24 @@ import java.io.OutputStream;
|
|||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.Key;
|
||||
import me.ayunami2000.ayuncraft.java.security.Key;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import me.ayunami2000.ayuncraft.java.security.PrivateKey;
|
||||
import me.ayunami2000.ayuncraft.java.security.PublicKey;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.Security;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.Arrays;
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import me.ayunami2000.ayuncraft.javax.crypto.SecretKey;
|
||||
import me.ayunami2000.ayuncraft.javax.crypto.spec.SecretKeySpec;
|
||||
import org.bouncycastle.crypto.BufferedBlockCipher;
|
||||
import org.bouncycastle.crypto.CipherKeyGenerator;
|
||||
import org.bouncycastle.crypto.KeyGenerationParameters;
|
||||
|
@ -47,7 +46,7 @@ public class CryptManager
|
|||
{
|
||||
CipherKeyGenerator var0 = new CipherKeyGenerator();
|
||||
var0.init(new KeyGenerationParameters(new SecureRandom(), 128));
|
||||
return new SecretKeySpec(var0.generateKey(), "AES");
|
||||
return (SecretKey) new SecretKeySpec(var0.generateKey(), "AES");
|
||||
}
|
||||
|
||||
public static KeyPair createNewKeyPair()
|
||||
|
@ -117,7 +116,7 @@ public class CryptManager
|
|||
{
|
||||
X509EncodedKeySpec var1 = new X509EncodedKeySpec(par0ArrayOfByte);
|
||||
KeyFactory var2 = KeyFactory.getInstance("RSA");
|
||||
return var2.generatePublic(var1);
|
||||
return (PublicKey) var2.generatePublic(var1);
|
||||
}
|
||||
catch (NoSuchAlgorithmException var3)
|
||||
{
|
||||
|
@ -137,7 +136,7 @@ public class CryptManager
|
|||
*/
|
||||
public static SecretKey decryptSharedKey(PrivateKey par0PrivateKey, byte[] par1ArrayOfByte)
|
||||
{
|
||||
return new SecretKeySpec(decryptData(par0PrivateKey, par1ArrayOfByte), "AES");
|
||||
return (SecretKey) new SecretKeySpec(decryptData((Key) par0PrivateKey, par1ArrayOfByte), "AES");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -218,12 +217,12 @@ public class CryptManager
|
|||
|
||||
public static OutputStream encryptOuputStream(SecretKey par0SecretKey, OutputStream par1OutputStream)
|
||||
{
|
||||
return new CipherOutputStream(par1OutputStream, createBufferedBlockCipher(true, par0SecretKey));
|
||||
return new CipherOutputStream(par1OutputStream, createBufferedBlockCipher(true, (Key) par0SecretKey));
|
||||
}
|
||||
|
||||
public static InputStream decryptInputStream(SecretKey par0SecretKey, InputStream par1InputStream)
|
||||
{
|
||||
return new CipherInputStream(par1InputStream, createBufferedBlockCipher(false, par0SecretKey));
|
||||
return new CipherInputStream(par1InputStream, createBufferedBlockCipher(false, (Key) par0SecretKey));
|
||||
}
|
||||
|
||||
public static OutputStream encryptOuputStream(BufferedBlockCipher bufferedBlockCipher, OutputStream par1OutputStream)
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
package me.ayunami2000.ayuncraft;
|
||||
|
||||
import me.ayunami2000.ayuncraft.java.security.PublicKey;
|
||||
|
||||
import java.security.Key;
|
||||
|
||||
public class ModifiablePublicKey implements PublicKey, Key {
|
||||
private String algorithm;
|
||||
private String format;
|
||||
private byte[] encoded;
|
||||
|
||||
@Override
|
||||
public String getAlgorithm() {
|
||||
return algorithm;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getEncoded() {
|
||||
return encoded;
|
||||
}
|
||||
|
||||
public ModifiablePublicKey(String algo,String form,byte[] data){
|
||||
algorithm=algo;
|
||||
format=form;
|
||||
encoded=data;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package me.ayunami2000.ayuncraft.java.security;
|
||||
|
||||
public interface Key {
|
||||
public String getAlgorithm();
|
||||
|
||||
public String getFormat();
|
||||
|
||||
public byte[] getEncoded();
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package me.ayunami2000.ayuncraft.java.security;
|
||||
|
||||
import me.ayunami2000.ayuncraft.javax.security.auth.Destroyable;
|
||||
|
||||
public interface PrivateKey extends Key, Destroyable {
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package me.ayunami2000.ayuncraft.java.security;
|
||||
|
||||
public interface PublicKey extends Key {
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package me.ayunami2000.ayuncraft.java.security.spec;
|
||||
|
||||
public interface KeySpec { }
|
|
@ -0,0 +1,6 @@
|
|||
package me.ayunami2000.ayuncraft.javax.crypto;
|
||||
|
||||
import me.ayunami2000.ayuncraft.java.security.Key;
|
||||
|
||||
public interface SecretKey extends Key {
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
package me.ayunami2000.ayuncraft.javax.crypto.spec;
|
||||
|
||||
import me.ayunami2000.ayuncraft.java.security.spec.KeySpec;
|
||||
import me.ayunami2000.ayuncraft.javax.crypto.SecretKey;
|
||||
|
||||
public class SecretKeySpec implements KeySpec, SecretKey {
|
||||
private String algorithm;
|
||||
private byte[] key;
|
||||
|
||||
public String getAlgorithm() {
|
||||
return this.algorithm;
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return "RAW";
|
||||
}
|
||||
|
||||
public byte[] getEncoded() {
|
||||
byte[] tmp = new byte[this.key.length];
|
||||
System.arraycopy(this.key, 0, tmp, 0, tmp.length);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
int code = this.algorithm.toUpperCase().hashCode();
|
||||
|
||||
for(int i = 0; i != this.key.length; ++i) {
|
||||
code ^= this.key[i] << 8 * (i % 4);
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (obj != null && obj instanceof SecretKeySpec) {
|
||||
SecretKeySpec spec = (SecretKeySpec)obj;
|
||||
if (!this.algorithm.equalsIgnoreCase(spec.algorithm)) {
|
||||
return false;
|
||||
} else if (this.key.length != spec.key.length) {
|
||||
return false;
|
||||
} else {
|
||||
for(int i = 0; i != this.key.length; ++i) {
|
||||
if (this.key[i] != spec.key[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public SecretKeySpec(byte[] key, String algorithm) {
|
||||
if (key == null) {
|
||||
throw new IllegalArgumentException("null key passed");
|
||||
} else if (algorithm == null) {
|
||||
throw new IllegalArgumentException("null algorithm passed");
|
||||
} else {
|
||||
this.key = new byte[key.length];
|
||||
System.arraycopy(key, 0, this.key, 0, key.length);
|
||||
this.algorithm = algorithm;
|
||||
}
|
||||
}
|
||||
|
||||
public SecretKeySpec(byte[] key, int offset, int len, String algorithm) {
|
||||
if (key == null) {
|
||||
throw new IllegalArgumentException("Null key passed");
|
||||
} else if (key.length - offset < len) {
|
||||
throw new IllegalArgumentException("Bad offset/len");
|
||||
} else if (algorithm == null) {
|
||||
throw new IllegalArgumentException("Null algorithm string passed");
|
||||
} else {
|
||||
this.key = new byte[len];
|
||||
System.arraycopy(key, offset, this.key, 0, len);
|
||||
this.algorithm = algorithm;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package me.ayunami2000.ayuncraft.javax.security.auth;
|
||||
|
||||
public class DestroyFailedException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = -7790152857282749162L;
|
||||
|
||||
public DestroyFailedException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public DestroyFailedException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package me.ayunami2000.ayuncraft.javax.security.auth;
|
||||
|
||||
public interface Destroyable {
|
||||
public default void destroy() throws DestroyFailedException {
|
||||
throw new DestroyFailedException();
|
||||
}
|
||||
|
||||
public default boolean isDestroyed() {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -4,6 +4,7 @@ import java.io.*;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import me.ayunami2000.ayuncraft.java.security.Key;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.src.*;
|
||||
import me.ayunami2000.ayuncraft.CryptManager;
|
||||
|
@ -58,6 +59,7 @@ public class WebsocketNetworkManager implements INetworkManager {
|
|||
//yee=this.encryptOuputStream(yee);
|
||||
}
|
||||
Packet.writePacket(var1, yee);
|
||||
if(logpackets)System.out.println("SENDING: "+var1);
|
||||
yee.flush();
|
||||
EaglerAdapter.writePacket(sendBuffer.toByteArray());
|
||||
} catch (IOException e) {
|
||||
|
@ -184,9 +186,9 @@ public class WebsocketNetworkManager implements INetworkManager {
|
|||
if(logpackets)System.out.println("RECEIVING: " + pkt);
|
||||
pkt.processPacket(this.netHandler);
|
||||
if(change){
|
||||
//processReadPackets();
|
||||
//return;
|
||||
break;
|
||||
processReadPackets();
|
||||
return;
|
||||
//break;
|
||||
}
|
||||
}
|
||||
} catch (EOFException e) {
|
||||
|
@ -219,7 +221,7 @@ public class WebsocketNetworkManager implements INetworkManager {
|
|||
{
|
||||
this.isInputBeingDecrypted = true;
|
||||
if(this.inputBufferedBlockCipher==null){
|
||||
this.inputBufferedBlockCipher = CryptManager.createBufferedBlockCipher(false,this.sharedKeyForEncryption);
|
||||
this.inputBufferedBlockCipher = CryptManager.createBufferedBlockCipher(false, (Key) this.sharedKeyForEncryption);
|
||||
}
|
||||
return new DataInputStream(CryptManager.decryptInputStream(this.inputBufferedBlockCipher, var1));
|
||||
}
|
||||
|
@ -237,7 +239,7 @@ public class WebsocketNetworkManager implements INetworkManager {
|
|||
private DataOutputStream encryptOuputStream() throws IOException
|
||||
{
|
||||
if(this.outputBufferedBlockCipher==null){
|
||||
this.outputBufferedBlockCipher = CryptManager.createBufferedBlockCipher(true,this.sharedKeyForEncryption);
|
||||
this.outputBufferedBlockCipher = CryptManager.createBufferedBlockCipher(true, (Key) this.sharedKeyForEncryption);
|
||||
}
|
||||
BufferedOutputStream var1 = new BufferedOutputStream(CryptManager.encryptOuputStream(this.outputBufferedBlockCipher, sendBuffer), 5120);
|
||||
return new DataOutputStream(var1);
|
||||
|
|
|
@ -5,6 +5,8 @@ import me.ayunami2000.ayuncraft.CryptManager;
|
|||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import me.ayunami2000.ayuncraft.java.security.Key;
|
||||
import me.ayunami2000.ayuncraft.java.security.PrivateKey;
|
||||
import me.ayunami2000.ayuncraft.java.security.PublicKey;
|
||||
import me.ayunami2000.ayuncraft.javax.crypto.SecretKey;
|
||||
|
@ -24,8 +26,8 @@ public class Packet252SharedKey extends Packet
|
|||
public Packet252SharedKey(SecretKey par1SecretKey, PublicKey par2PublicKey, byte[] par3ArrayOfByte)
|
||||
{
|
||||
this.sharedKey = par1SecretKey;
|
||||
this.sharedSecret = CryptManager.encryptData(par2PublicKey, par1SecretKey.getEncoded());
|
||||
this.verifyToken = CryptManager.encryptData(par2PublicKey, par3ArrayOfByte);
|
||||
this.sharedSecret = CryptManager.encryptData((Key) par2PublicKey, par1SecretKey.getEncoded());
|
||||
this.verifyToken = CryptManager.encryptData((Key) par2PublicKey, par3ArrayOfByte);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,6 +85,6 @@ public class Packet252SharedKey extends Packet
|
|||
*/
|
||||
public byte[] getVerifyToken(PrivateKey par1PrivateKey)
|
||||
{
|
||||
return par1PrivateKey == null ? this.verifyToken : CryptManager.decryptData(par1PrivateKey, this.verifyToken);
|
||||
return par1PrivateKey == null ? this.verifyToken : CryptManager.decryptData((Key) par1PrivateKey, this.verifyToken);
|
||||
}
|
||||
}
|
15
src/teavm/java/com/rhg/rsa/RSABaseInterface.java
Normal file
15
src/teavm/java/com/rhg/rsa/RSABaseInterface.java
Normal file
|
@ -0,0 +1,15 @@
|
|||
package com.rhg.rsa;
|
||||
|
||||
/**
|
||||
* Write a description of interface RSABaseInterface here.
|
||||
*
|
||||
* @author Rob
|
||||
* @version 06/01/2010
|
||||
*/
|
||||
|
||||
public interface RSABaseInterface
|
||||
{
|
||||
byte PUBLIC_KEY = 1;
|
||||
byte PRIVATE_KEY = 2;
|
||||
byte COMPLETE_KEY = 3;
|
||||
}
|
209
src/teavm/java/com/rhg/rsa/RSACompleteKey.java
Normal file
209
src/teavm/java/com/rhg/rsa/RSACompleteKey.java
Normal file
|
@ -0,0 +1,209 @@
|
|||
package com.rhg.rsa;
|
||||
|
||||
import java.math.BigInteger;
|
||||
/**
|
||||
* Class representing full private and public RSA keys.
|
||||
*
|
||||
* @author Rob
|
||||
* @version 05/31/2010
|
||||
*/
|
||||
public class RSACompleteKey extends RSAPrivateKey
|
||||
{
|
||||
/** The first CRT exponent. */
|
||||
private BigInteger dP;
|
||||
|
||||
/** The second CRT exponent. */
|
||||
private BigInteger dQ;
|
||||
|
||||
/** The public exponent. */
|
||||
private BigInteger e;
|
||||
|
||||
/** The larger prime factor of the modulus. */
|
||||
private BigInteger p;
|
||||
|
||||
/** The LCM of the primes. */
|
||||
private BigInteger phi;
|
||||
|
||||
/** The smaller prime factor of the modulus. */
|
||||
private BigInteger q;
|
||||
|
||||
/** The CRT coefficient. */
|
||||
private BigInteger qInv;
|
||||
|
||||
|
||||
|
||||
/** Default constructor. */
|
||||
public RSACompleteKey() {
|
||||
super(null, null);
|
||||
setPubExp(null);
|
||||
setPrimes(null, null);
|
||||
setCRTExpOne(null);
|
||||
setCRTExpTwo(null);
|
||||
setCRTCoeff(null);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Main constructor. */
|
||||
public RSACompleteKey(BigInteger prime1,
|
||||
BigInteger prime2,
|
||||
BigInteger modulus,
|
||||
BigInteger pubExp,
|
||||
BigInteger priExp,
|
||||
BigInteger crtExp1,
|
||||
BigInteger crtExp2,
|
||||
BigInteger crtCoeff) {
|
||||
super(modulus, priExp);
|
||||
setPubExp(pubExp);
|
||||
setPrimes(prime1, prime2);
|
||||
setCRTExpOne(crtExp1);
|
||||
setCRTExpTwo(crtExp2);
|
||||
setCRTCoeff(crtCoeff);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Computes the LCM of the primes. */
|
||||
protected void computePhi() {
|
||||
phi = lcm(pMinusOne(), qMinusOne());
|
||||
}
|
||||
|
||||
/** Performs the classical RSA computation. */
|
||||
protected BigInteger decrypt(BigInteger c) {
|
||||
BigInteger m1, m2, dm, h;
|
||||
m1 = c.modPow(getCRTExpOne(), getPrimeOne());
|
||||
m2 = c.modPow(getCRTExpTwo(), getPrimeTwo());
|
||||
dm = m1.subtract(m2).abs();
|
||||
h = getCRTCoeff().multiply(dm).mod(getPrimeOne());
|
||||
return m2.add(getPrimeTwo().multiply(h));
|
||||
}
|
||||
|
||||
/** Returns the first CRT exponent. */
|
||||
public BigInteger getCRTExpOne() {
|
||||
return dP;
|
||||
}
|
||||
|
||||
/** Returns the second CRT exponent. */
|
||||
public BigInteger getCRTExpTwo() {
|
||||
return dQ;
|
||||
}
|
||||
|
||||
/** Returns the CRT coefficient. */
|
||||
public BigInteger getCRTCoeff() {
|
||||
return qInv;
|
||||
}
|
||||
|
||||
/** Returns phi. */
|
||||
public BigInteger getPhi() {
|
||||
return phi;
|
||||
}
|
||||
|
||||
/** Returns the larger prime factor. */
|
||||
public BigInteger getPrimeOne() {
|
||||
return p;
|
||||
}
|
||||
|
||||
/** Returns the smaller prime factor. */
|
||||
public BigInteger getPrimeTwo() {
|
||||
return q;
|
||||
}
|
||||
|
||||
/** Returns the public exponent. */
|
||||
public BigInteger getPubExp() {
|
||||
return e;
|
||||
}
|
||||
|
||||
/** Returns true when key is valid. */
|
||||
public boolean isValid() {
|
||||
if (noValuesNull() && isPrime(p) && isPrime(q)) {
|
||||
computePhi();
|
||||
return p.multiply(q).equals(getModulus()) &&
|
||||
e.compareTo(THREE) >= 0 &&
|
||||
e.compareTo(getModulus()) < 0 &&
|
||||
e.gcd(phi).equals(ONE) &&
|
||||
getPriExp().compareTo(getModulus()) < 0 &&
|
||||
getPriExp().equals(e.modInverse(phi)) &&
|
||||
dP.compareTo(p) < 0 &&
|
||||
dQ.compareTo(q) < 0 &&
|
||||
dP.equals(e.modInverse(pMinusOne())) &&
|
||||
dQ.equals(e.modInverse(qMinusOne())) &&
|
||||
qInv.compareTo(p) < 0 &&
|
||||
qInv.equals(q.modInverse(p));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns true when no fields are null. */
|
||||
private boolean noValuesNull() {
|
||||
return !(isNull(p) || isNull(q) || isNull(getModulus()) ||
|
||||
isNull(e) || isNull(getPriExp()) || isNull(dP) ||
|
||||
isNull(dQ) || isNull(qInv));
|
||||
}
|
||||
|
||||
/** Returns p minus one. */
|
||||
protected BigInteger pMinusOne() {
|
||||
if (isNull(p)) {
|
||||
return null;
|
||||
} else {
|
||||
return p.subtract(ONE);
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns q minus one. */
|
||||
protected BigInteger qMinusOne() {
|
||||
if (isNull(q)) {
|
||||
return null;
|
||||
} else {
|
||||
return q.subtract(ONE);
|
||||
}
|
||||
}
|
||||
/** Sets the CRT exponent. */
|
||||
public void setCRTCoeff(BigInteger crtCoeff) {
|
||||
qInv = weedOut(crtCoeff);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Sets the first CRT exponent. */
|
||||
public void setCRTExpOne(BigInteger crtExp1) {
|
||||
dP = weedOut(crtExp1);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Sets the second CRT exponent. */
|
||||
public void setCRTExpTwo(BigInteger crtExp2) {
|
||||
dQ = weedOut(crtExp2);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Sets phi. */
|
||||
public void setPhi(BigInteger phi) {
|
||||
this.phi = weedOut(phi);
|
||||
}
|
||||
|
||||
/** Sets the prime factors. */
|
||||
public void setPrimes(BigInteger prime1, BigInteger prime2) {
|
||||
if (isNull(prime1 = weedOut(prime1)) || isNull(prime2 = weedOut(prime2))) {
|
||||
return;
|
||||
} else {
|
||||
if (isPositive(prime1.subtract(prime2))) {
|
||||
p = prime1;
|
||||
q = prime2;
|
||||
} else if (isPositive(prime2.subtract(prime1))) {
|
||||
p = prime2;
|
||||
q = prime1;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/** Sets the public exponent. */
|
||||
public void setPubExp(BigInteger pubExp) {
|
||||
e = weedOut(pubExp);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
18
src/teavm/java/com/rhg/rsa/RSAConstants.java
Normal file
18
src/teavm/java/com/rhg/rsa/RSAConstants.java
Normal file
|
@ -0,0 +1,18 @@
|
|||
package com.rhg.rsa;
|
||||
|
||||
import java.math.BigInteger;
|
||||
/**
|
||||
* Interface providing often used BigInteger constants.
|
||||
*
|
||||
* @author Rob
|
||||
* @version 05/30/2010
|
||||
*/
|
||||
|
||||
public interface RSAConstants
|
||||
{
|
||||
BigInteger ZERO = BigInteger.ZERO;
|
||||
BigInteger ONE = BigInteger.ONE;
|
||||
BigInteger TWO = BigInteger.valueOf(2);
|
||||
BigInteger THREE = BigInteger.valueOf(3);
|
||||
BigInteger TWO_FIFTY_SIX = BigInteger.valueOf(256);
|
||||
}
|
203
src/teavm/java/com/rhg/rsa/RSAKey.java
Normal file
203
src/teavm/java/com/rhg/rsa/RSAKey.java
Normal file
|
@ -0,0 +1,203 @@
|
|||
package com.rhg.rsa;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.io.*;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Abstract class representing common features of RSA keys.
|
||||
*
|
||||
* @author Rob
|
||||
* @version 06/01/2010
|
||||
*/
|
||||
public abstract class RSAKey implements RSAConstants, RSABaseInterface
|
||||
{
|
||||
/** The modulus. */
|
||||
private BigInteger n;
|
||||
|
||||
/** Default constructor. */
|
||||
protected RSAKey() {
|
||||
setModulus(null);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Main constructor. */
|
||||
protected RSAKey(BigInteger modulus) {
|
||||
setModulus(modulus);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Returns the contents of a file as a byte array. */
|
||||
/*
|
||||
protected byte[] getBytes(String fileName) {
|
||||
File fIn = new File(fileName);
|
||||
if (!fIn.canRead()) {
|
||||
System.err.println("Can't read " + fileName);
|
||||
return null;
|
||||
}
|
||||
|
||||
FileInputStream in = null;
|
||||
byte[] bytes = null;
|
||||
try {
|
||||
in = new FileInputStream(fIn);
|
||||
|
||||
long fileSize = fIn.length();
|
||||
if (fileSize > Integer.MAX_VALUE) {
|
||||
System.out.println("Sorry, file was too large!");
|
||||
}
|
||||
|
||||
bytes = new byte[(int) fileSize];
|
||||
|
||||
int offset = 0;
|
||||
int numRead = 0;
|
||||
while (offset < bytes.length
|
||||
&& (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
|
||||
offset += numRead;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
} finally {
|
||||
try {
|
||||
if (in != null) in.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
|
||||
return bytes;
|
||||
}
|
||||
*/
|
||||
|
||||
/** Returns the modulus. */
|
||||
public BigInteger getModulus() {
|
||||
return n;
|
||||
}
|
||||
|
||||
/** Returns the number of bytes required to store the modulus. */
|
||||
protected int getModulusByteSize() {
|
||||
return (int) Math.ceil(getModulus().bitLength() / 8.0);
|
||||
}
|
||||
|
||||
/** Returns a portion of the array argument. */
|
||||
protected byte[] getSubArray(byte[] inBytes, int start, int end) {
|
||||
if (start >= inBytes.length) {
|
||||
return null;
|
||||
}
|
||||
if (end > inBytes.length) {
|
||||
end = inBytes.length;
|
||||
}
|
||||
int bytesToGet = end - start;
|
||||
if (bytesToGet < 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[] outBytes = new byte[bytesToGet];
|
||||
for (int i = start; i < end; i++) {
|
||||
outBytes[i - start] = inBytes[i];
|
||||
}
|
||||
|
||||
return outBytes;
|
||||
}
|
||||
|
||||
/** Returns true when the argument is null. */
|
||||
public final boolean isNull(Object obj) {
|
||||
return !(obj != null);
|
||||
}
|
||||
|
||||
/** Returns true when the argument is greater than zero. */
|
||||
public final boolean isPositive(BigInteger number) {
|
||||
return (number.compareTo(ZERO) > 0);
|
||||
}
|
||||
|
||||
/** Returns true when the argument is prime. */
|
||||
public boolean isPrime(BigInteger number) {
|
||||
return number.isProbablePrime(100);
|
||||
}
|
||||
|
||||
/** Computes the least common multiple. */
|
||||
public BigInteger lcm(BigInteger a, BigInteger b) {
|
||||
return (a.multiply(b).divide(a.gcd(b)));
|
||||
}
|
||||
|
||||
/** Generates an array of pseudo-random nonzero bytes. */
|
||||
protected byte[] makePaddingString(int len) {
|
||||
if (len < 8) return null;
|
||||
Random rndm = new Random();
|
||||
|
||||
byte[] PS = new byte[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
PS[i] = (byte)(rndm.nextInt(255) + 1);
|
||||
}
|
||||
|
||||
return PS;
|
||||
}
|
||||
|
||||
/** Reshapes a byte array into an array of byte arrays. */
|
||||
protected byte[][] reshape(byte[] inBytes, int colSize) {
|
||||
if (colSize < 1) {
|
||||
colSize = 1;
|
||||
}
|
||||
|
||||
int rowSize = (int) Math.ceil((double)inBytes.length / (double)colSize);
|
||||
|
||||
if (rowSize == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
byte[][] outBytes = new byte[rowSize][];
|
||||
|
||||
for (int i = 0; i < rowSize; i++) {
|
||||
outBytes[i] = getSubArray(inBytes, i * colSize, (i + 1) * colSize);
|
||||
}
|
||||
return outBytes;
|
||||
}
|
||||
|
||||
/** Sets the modulus. */
|
||||
public void setModulus(BigInteger modulus) {
|
||||
n = weedOut(modulus);
|
||||
if (isNull(n) || n.bitLength() < 96) {
|
||||
n = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/** Converts a BigInteger into a byte array of the specified length. */
|
||||
protected byte[] toByteArray(BigInteger x, int numBytes) {
|
||||
if (x.compareTo(TWO_FIFTY_SIX.pow(numBytes)) >= 0) {
|
||||
return null; // number is to big to fit in the byte array
|
||||
}
|
||||
|
||||
byte[] ba = new byte[numBytes--];
|
||||
BigInteger[] divAndRem = new BigInteger[2];
|
||||
|
||||
for (int power = numBytes; power >= 0; power--) {
|
||||
divAndRem = x.divideAndRemainder(TWO_FIFTY_SIX.pow(power));
|
||||
ba[numBytes - power] = (byte) divAndRem[0].intValue();
|
||||
x = divAndRem[1];
|
||||
}
|
||||
|
||||
return ba;
|
||||
}
|
||||
|
||||
/** Converts a byte array into a BigInteger. */
|
||||
protected BigInteger toInteger(byte[] X) {
|
||||
BigInteger x = ZERO;
|
||||
|
||||
for (int i = 0; i < X.length; i++) {
|
||||
x = x.add(BigInteger.valueOf(X[i]).multiply(TWO_FIFTY_SIX.pow(X.length - 1 - i)));
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
/** Uses the key and returns true if use was successful. */
|
||||
//public abstract boolean use(String source, String destination);
|
||||
public abstract boolean use(byte[] sourceBytes, ByteArrayOutputStream baos);
|
||||
|
||||
/** Weeds out bad inputs. */
|
||||
public final BigInteger weedOut(BigInteger arg) {
|
||||
if (!isNull(arg) && isPositive(arg)) {
|
||||
return arg;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
104
src/teavm/java/com/rhg/rsa/RSAKeyGenerator.java
Normal file
104
src/teavm/java/com/rhg/rsa/RSAKeyGenerator.java
Normal file
|
@ -0,0 +1,104 @@
|
|||
package com.rhg.rsa;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.SecureRandom;
|
||||
/**
|
||||
* Class representing private and public keys with the ability to generate new keys.
|
||||
*
|
||||
* @author Rob
|
||||
* @version 05/31/2010
|
||||
*/
|
||||
public class RSAKeyGenerator extends RSACompleteKey
|
||||
{
|
||||
/** Minimum number of bits in the modulus allowed. */
|
||||
private static final int MIN_BIT_LENGTH = 1024;
|
||||
|
||||
/** Source of securely (pseudo-) random bits. */
|
||||
SecureRandom rand = new SecureRandom();
|
||||
|
||||
/** The number of bits required in the modulus. */
|
||||
private int bitLength;
|
||||
|
||||
/** Default constructor. */
|
||||
public RSAKeyGenerator() {
|
||||
super();
|
||||
setBitLength(MIN_BIT_LENGTH);
|
||||
generateNewKeys();
|
||||
return;
|
||||
}
|
||||
|
||||
/** Main constructor. */
|
||||
public RSAKeyGenerator(int bitLength) {
|
||||
super();
|
||||
setBitLength(bitLength);
|
||||
generateNewKeys();
|
||||
return;
|
||||
}
|
||||
|
||||
/** Generates new private and public keys. */
|
||||
public void generateNewKeys() {
|
||||
setPrimes(BigInteger.probablePrime(75 * bitLength / 100, rand),
|
||||
BigInteger.probablePrime(25 * bitLength / 100, rand));
|
||||
setModulus(getPrimeOne().multiply(getPrimeTwo()));
|
||||
computePhi();
|
||||
|
||||
BigInteger i;
|
||||
for (i = BigInteger.probablePrime((bitLength / 10), rand);
|
||||
i.compareTo(getModulus()) < 0;
|
||||
i = i.nextProbablePrime()) {
|
||||
if (i.gcd(getPhi()).equals(ONE)) {
|
||||
setPubExp(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
setPriExp(getPubExp().modInverse(getPhi()));
|
||||
setCRTExpOne(getPubExp().modInverse(pMinusOne()));
|
||||
setCRTExpTwo(getPubExp().modInverse(qMinusOne()));
|
||||
setCRTCoeff(getPrimeTwo().modInverse(getPrimeOne()));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/** Makes a new RSA key. */
|
||||
public RSAKey makeKey(byte whichKey) {
|
||||
switch (whichKey) {
|
||||
case PUBLIC_KEY:
|
||||
return makePublicKey();
|
||||
case PRIVATE_KEY:
|
||||
return makePrivateKey();
|
||||
case COMPLETE_KEY:
|
||||
return makeCompleteKey();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Makes complete RSA key objects. */
|
||||
private RSACompleteKey makeCompleteKey() {
|
||||
return new RSACompleteKey(getPrimeOne(),
|
||||
getPrimeTwo(),
|
||||
getModulus(),
|
||||
getPubExp(),
|
||||
getPriExp(),
|
||||
getCRTExpOne(),
|
||||
getCRTExpTwo(),
|
||||
getCRTCoeff());
|
||||
}
|
||||
|
||||
/** Makes private RSA key objects. */
|
||||
private RSAPrivateKey makePrivateKey() {
|
||||
return new RSAPrivateKey(getModulus(), getPriExp());
|
||||
}
|
||||
|
||||
/** Makes public RSA key objects. */
|
||||
private RSAPublicKey makePublicKey() {
|
||||
return new RSAPublicKey(getModulus(), getPubExp());
|
||||
}
|
||||
|
||||
/** Sets the number of bits in the modulus. */
|
||||
public void setBitLength(int bitLength) {
|
||||
this.bitLength = (bitLength >= MIN_BIT_LENGTH ? bitLength : MIN_BIT_LENGTH);
|
||||
return;
|
||||
}
|
||||
}
|
137
src/teavm/java/com/rhg/rsa/RSAPrivateKey.java
Normal file
137
src/teavm/java/com/rhg/rsa/RSAPrivateKey.java
Normal file
|
@ -0,0 +1,137 @@
|
|||
package com.rhg.rsa;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Class representing a private RSA key.
|
||||
*
|
||||
* @author Rob
|
||||
* @version 05/31/2010
|
||||
*/
|
||||
public class RSAPrivateKey extends RSAKey
|
||||
{
|
||||
/** The private exponent. */
|
||||
private BigInteger d;
|
||||
|
||||
/** Default constructor. */
|
||||
public RSAPrivateKey() {
|
||||
super();
|
||||
setPriExp(null);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Main constructor. */
|
||||
public RSAPrivateKey(BigInteger modulus, BigInteger priExp) {
|
||||
super(modulus);
|
||||
setPriExp(priExp);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Performs the classical RSA computation. */
|
||||
protected BigInteger decrypt(BigInteger c) {
|
||||
return c.modPow(getPriExp(), getModulus());
|
||||
}
|
||||
|
||||
/** Extracts the data portion of the byte array. */
|
||||
protected byte[] extractData(byte[] EB) {
|
||||
if (EB.length < 12 || EB[0] != 0x00 || EB[1] != 0x02) {
|
||||
return null;
|
||||
}
|
||||
int index = 2;
|
||||
do {} while (EB[index++] != 0x00);
|
||||
|
||||
return getSubArray(EB, index, EB.length);
|
||||
}
|
||||
|
||||
/** Returns the private exponent. */
|
||||
public BigInteger getPriExp() {
|
||||
return d;
|
||||
}
|
||||
|
||||
/** Sets the private exponent. */
|
||||
public void setPriExp(BigInteger priExp)
|
||||
{
|
||||
d = weedOut(priExp);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Uses key and returns true if decryption was successful. */
|
||||
/*
|
||||
public boolean use(String source, String destination) {
|
||||
byte[] sourceBytes = getBytes(source);
|
||||
if (isNull(sourceBytes)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int k = getModulusByteSize();
|
||||
BigInteger c, m;
|
||||
byte[] EB, M;
|
||||
byte[][] C = reshape(sourceBytes, k);
|
||||
BufferedOutputStream out = null;
|
||||
|
||||
try {
|
||||
out = new BufferedOutputStream(new FileOutputStream(destination));
|
||||
for (int i = 0; i < C.length; i++) {
|
||||
if (C[i].length != k) return false;
|
||||
c = new BigInteger(C[i]);
|
||||
m = decrypt(c);
|
||||
EB = toByteArray(m, k);
|
||||
M = extractData(EB);
|
||||
out.write(M);
|
||||
}
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (isNull(out)) out.close();
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
public boolean use(byte[] sourceBytes, ByteArrayOutputStream out) {
|
||||
System.out.println(1);
|
||||
int k = getModulusByteSize();
|
||||
System.out.println(2);
|
||||
BigInteger c, m;
|
||||
byte[] EB, M;
|
||||
byte[][] C = reshape(sourceBytes, k);
|
||||
System.out.println(3);
|
||||
|
||||
try {
|
||||
for (int i = 0; i < C.length; i++) {
|
||||
System.out.println(4);
|
||||
if (C[i].length != k) return false;
|
||||
c = new BigInteger(C[i]);
|
||||
System.out.println(5);
|
||||
m = decrypt(c);
|
||||
System.out.println(6);
|
||||
EB = toByteArray(m, k);
|
||||
System.out.println(7);
|
||||
M = extractData(EB);
|
||||
System.out.println(8);
|
||||
out.write(M);
|
||||
System.out.println("FARD: "+Arrays.toString(M));
|
||||
}
|
||||
System.out.println(9);
|
||||
out.flush();
|
||||
//out.close();
|
||||
} catch (Exception e) {
|
||||
/*
|
||||
String errMsg = "An exception occured!%n%s%n%s%n%s";
|
||||
System.err.println(String.format(errMsg, e.getClass(), e.getMessage(), Arrays.toString(e.getStackTrace())));
|
||||
*/
|
||||
System.err.println(e.getClass().getName()+"\n"+e.getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
135
src/teavm/java/com/rhg/rsa/RSAPublicKey.java
Normal file
135
src/teavm/java/com/rhg/rsa/RSAPublicKey.java
Normal file
|
@ -0,0 +1,135 @@
|
|||
package com.rhg.rsa;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Class representing a public RSA key.
|
||||
*
|
||||
* @author Rob
|
||||
* @version 05/30/2010
|
||||
*/
|
||||
public class RSAPublicKey extends RSAKey
|
||||
{
|
||||
/** The public exponent. */
|
||||
private BigInteger e;
|
||||
|
||||
/** Default constructor. */
|
||||
public RSAPublicKey() {
|
||||
super(null);
|
||||
setPubExp(null);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Main constructor. */
|
||||
public RSAPublicKey(BigInteger modulus, BigInteger pubExp)
|
||||
{
|
||||
super(modulus);
|
||||
setPubExp(pubExp);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Performs the classical RSA computation. */
|
||||
protected BigInteger encrypt(BigInteger m) {
|
||||
return m.modPow(getPubExp(), getModulus());
|
||||
}
|
||||
|
||||
/** Returns the public exponent. */
|
||||
public BigInteger getPubExp() {
|
||||
return e;
|
||||
}
|
||||
|
||||
/** Sets the public exponent. */
|
||||
public void setPubExp(BigInteger pubExp)
|
||||
{
|
||||
e = weedOut(pubExp);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Uses the key and returns true if encryption was successful. */
|
||||
/*
|
||||
public boolean use(String source, String destination) {
|
||||
byte[] sourceBytes = getBytes(source);
|
||||
if (isNull(sourceBytes)) {
|
||||
System.err.println(String.format("%s contained nothing.", source));
|
||||
return false;
|
||||
}
|
||||
|
||||
int k = getModulusByteSize();
|
||||
byte BT = 0x02;
|
||||
byte[] C, M;
|
||||
byte[][] D = reshape(sourceBytes, k - 11);
|
||||
ByteArrayOutputStream EB = new ByteArrayOutputStream(k);
|
||||
FileOutputStream out = null;
|
||||
BigInteger m, c;
|
||||
|
||||
try {
|
||||
out = new FileOutputStream(destination);
|
||||
for (int i = 0; i < D.length; i++) {
|
||||
EB.reset();
|
||||
EB.write(0x00); EB.write(BT); EB.write(makePaddingString(k - D[i].length - 3));
|
||||
EB.write(0x00); EB.write(D[i]);
|
||||
M = EB.toByteArray();
|
||||
m = new BigInteger(M);
|
||||
c = encrypt(m);
|
||||
C = toByteArray(c, k);
|
||||
out.write(C);
|
||||
}
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
String errMsg = "An exception occured!%n%s%n%s%n%s";
|
||||
System.err.println(String.format(errMsg, e.getClass(), e.getMessage(), e.getStackTrace()));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
public boolean use(byte[] sourceBytes, ByteArrayOutputStream out) {
|
||||
System.out.println(1);
|
||||
int k = getModulusByteSize();
|
||||
System.out.println(2);
|
||||
byte BT = 0x02;
|
||||
byte[] C, M;
|
||||
byte[][] D = reshape(sourceBytes, k - 11);
|
||||
System.out.println(3);
|
||||
ByteArrayOutputStream EB = new ByteArrayOutputStream(k);
|
||||
System.out.println(4);
|
||||
BigInteger m, c;
|
||||
|
||||
try {
|
||||
for (int i = 0; i < D.length; i++) {
|
||||
System.out.println(5);
|
||||
EB.reset();
|
||||
System.out.println(6);
|
||||
EB.write(0x00); EB.write(BT); EB.write(makePaddingString(k - D[i].length - 3));
|
||||
System.out.println(7);
|
||||
EB.write(0x00); EB.write(D[i]);
|
||||
System.out.println(8);
|
||||
M = EB.toByteArray();
|
||||
System.out.println(9);
|
||||
m = new BigInteger(M);
|
||||
System.out.println(10);
|
||||
c = encrypt(m);
|
||||
System.out.println(11);
|
||||
C = toByteArray(c, k);
|
||||
System.out.println(12);
|
||||
out.write(C);
|
||||
System.out.println("FARD: "+Arrays.toString(C));
|
||||
}
|
||||
out.flush();
|
||||
//out.close();
|
||||
} catch (Exception e) {
|
||||
/*
|
||||
String errMsg = "An exception occured!%n%s%n%s%n%s";
|
||||
System.err.println(String.format(errMsg, e.getClass(), e.getMessage(), Arrays.toString(e.getStackTrace())));
|
||||
*/
|
||||
System.err.println(e.getClass().getName()+"\n"+e.getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,12 +1,20 @@
|
|||
package me.ayunami2000.ayuncraft;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.security.spec.RSAPublicKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.rhg.rsa.RSAPrivateKey;
|
||||
import com.rhg.rsa.RSAPublicKey;
|
||||
import me.ayunami2000.ayuncraft.java.security.Key;
|
||||
import me.ayunami2000.ayuncraft.java.security.PrivateKey;
|
||||
import me.ayunami2000.ayuncraft.java.security.PublicKey;
|
||||
|
||||
import net.lax1dude.eaglercraft.Base64;
|
||||
import org.bouncycastle.crypto.BufferedBlockCipher;
|
||||
import org.bouncycastle.crypto.engines.AESFastEngine;
|
||||
import org.bouncycastle.crypto.io.CipherInputStream;
|
||||
|
@ -17,6 +25,9 @@ import org.bouncycastle.crypto.params.ParametersWithIV;
|
|||
|
||||
import me.ayunami2000.ayuncraft.javax.crypto.SecretKey;
|
||||
import me.ayunami2000.ayuncraft.javax.crypto.spec.SecretKeySpec;
|
||||
import org.teavm.jso.JSBody;
|
||||
import org.teavm.jso.typedarrays.ArrayBuffer;
|
||||
import org.teavm.jso.typedarrays.Uint8Array;
|
||||
|
||||
public class CryptManager
|
||||
{
|
||||
|
@ -24,16 +35,41 @@ public class CryptManager
|
|||
/**
|
||||
* Generate a new shared secret AES key from a static preset key
|
||||
*/
|
||||
private static final byte[] baseSharedKey = new byte[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
|
||||
private static final byte[] baseSharedKey = new byte[] {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
|
||||
public static SecretKey createNewSharedKey(){
|
||||
return new SecretKeySpec(baseSharedKey,"AES");
|
||||
}
|
||||
|
||||
/*
|
||||
@JSBody(params = {"base64enc"}, script = "window.servercertraw=base64enc;window.servercert=new x509.X509Certificate(base64enc);")
|
||||
private static native void setupDecode(String base64enc);
|
||||
|
||||
@JSBody(params = {}, script = "return window.servercert.publickey.algorithm.name;")
|
||||
private static native String decodeAlgorithm();
|
||||
|
||||
@JSBody(params = {}, script = "return window.servercert.publickey.algorithm.publicExponent;")
|
||||
private static native Uint8Array decodePublicExponent();
|
||||
|
||||
@JSBody(params = {}, script = "return window.servercert.publickey.algorithm.modulusLength;")
|
||||
private static native int decodeModulusLength();
|
||||
|
||||
@JSBody(params = {}, script = "return new Uint8Array(window.servercert.publickey.rawData);")
|
||||
private static native Uint8Array decodeData();
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create a new PublicKey from encoded X.509 data
|
||||
*/
|
||||
public static PublicKey decodePublicKey(byte[] par0ArrayOfByte)
|
||||
{
|
||||
/*
|
||||
setupDecode(Base64.encodeBase64String(par0ArrayOfByte));
|
||||
Uint8Array a = decodeData();
|
||||
byte[] b = new byte[a.getByteLength()];
|
||||
for(int i = 0; i < b.length; ++i) {
|
||||
b[i] = (byte) (a.get(i) & 0xFF);
|
||||
}
|
||||
*/
|
||||
return new ModifiablePublicKey("RSA","X.509",par0ArrayOfByte);
|
||||
}
|
||||
|
||||
|
@ -45,16 +81,44 @@ public class CryptManager
|
|||
return new SecretKeySpec(decryptData(par0PrivateKey, par1ArrayOfByte), "AES");
|
||||
}
|
||||
|
||||
private static RSA rsa=new RSA(2048);
|
||||
/*
|
||||
private static RSA rsa=new RSA(1024);
|
||||
|
||||
static {
|
||||
rsa.setModulus(BigInteger.valueOf(16));
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Encrypt byte[] data with RSA public key
|
||||
*/
|
||||
public static byte[] encryptData(Key par0Key, byte[] par1ArrayOfByte)
|
||||
{
|
||||
///*
|
||||
try {
|
||||
//System.out.println(Arrays.toString(par0Key.getEncoded()));
|
||||
//System.out.println(Arrays.toString(par1ArrayOfByte));
|
||||
RSAPublicKey rsaPublicKey = new RSAPublicKey(BigInteger.valueOf(16), new BigInteger(par0Key.getEncoded()));
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
rsaPublicKey.use(par1ArrayOfByte, baos);
|
||||
return baos.toByteArray();
|
||||
}catch(Exception e){
|
||||
System.err.println(e.getClass().getName() + "\n" + e.getMessage());
|
||||
//e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
//*/
|
||||
/*
|
||||
try {
|
||||
rsa.setPublicKey(new BigInteger(par0Key.getEncoded()));
|
||||
BigInteger res = rsa.encrypt(new BigInteger(par1ArrayOfByte));
|
||||
return res.toByteArray();
|
||||
}catch(Exception e){
|
||||
System.err.println(e.getClass().getName() + "\n" + e.getMessage());
|
||||
//e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,9 +126,28 @@ public class CryptManager
|
|||
*/
|
||||
public static byte[] decryptData(Key par0Key, byte[] par1ArrayOfByte)
|
||||
{
|
||||
///*
|
||||
try{
|
||||
RSAPrivateKey rsaPrivateKey = new RSAPrivateKey(BigInteger.valueOf(16),new BigInteger(par0Key.getEncoded()));
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
rsaPrivateKey.use(par1ArrayOfByte,baos);
|
||||
return baos.toByteArray();
|
||||
}catch(Exception e){
|
||||
System.err.println(e.getClass().getName() + "\n" + e.getMessage());
|
||||
return null;
|
||||
}
|
||||
//*/
|
||||
/*
|
||||
try {
|
||||
rsa.setPrivateKey(new BigInteger(par0Key.getEncoded()));
|
||||
BigInteger res = rsa.decrypt(new BigInteger(par1ArrayOfByte));
|
||||
return res.toByteArray();
|
||||
}catch(Exception e){
|
||||
System.err.println(e.getClass().getName() + "\n" + e.getMessage());
|
||||
//e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -67,6 +67,14 @@ public class RSA {
|
|||
privateKey=k;
|
||||
}
|
||||
|
||||
void setModulus(BigInteger k) {
|
||||
modulus=k;
|
||||
}
|
||||
|
||||
void setModulus() {
|
||||
modulus=one;
|
||||
}
|
||||
|
||||
|
||||
BigInteger getPublicKey() {
|
||||
return publicKey;
|
||||
|
@ -76,6 +84,10 @@ public class RSA {
|
|||
return privateKey;
|
||||
}
|
||||
|
||||
BigInteger getModulus() {
|
||||
return modulus;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String s = "";
|
||||
s += "public = " + publicKey + "\n";
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
package java.security.spec;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* This class specifies an RSA public key.
|
||||
*
|
||||
* @author Jan Luehe
|
||||
*
|
||||
*
|
||||
* @see java.security.Key
|
||||
* @see java.security.KeyFactory
|
||||
* @see KeySpec
|
||||
* @see X509EncodedKeySpec
|
||||
* @see RSAPrivateKeySpec
|
||||
* @see RSAPrivateCrtKeySpec
|
||||
*/
|
||||
|
||||
public class RSAPublicKeySpec implements KeySpec {
|
||||
|
||||
private final BigInteger modulus;
|
||||
private final BigInteger publicExponent;
|
||||
private final AlgorithmParameterSpec params;
|
||||
|
||||
/**
|
||||
* Creates a new RSAPublicKeySpec.
|
||||
*
|
||||
* @param modulus the modulus
|
||||
* @param publicExponent the public exponent
|
||||
*/
|
||||
public RSAPublicKeySpec(BigInteger modulus, BigInteger publicExponent) {
|
||||
this(modulus, publicExponent, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new RSAPublicKeySpec with additional key parameters.
|
||||
*
|
||||
* @param modulus the modulus
|
||||
* @param publicExponent the public exponent
|
||||
* @param params the parameters associated with this key, may be null
|
||||
* @since 8
|
||||
*/
|
||||
public RSAPublicKeySpec(BigInteger modulus, BigInteger publicExponent,
|
||||
AlgorithmParameterSpec params) {
|
||||
this.modulus = modulus;
|
||||
this.publicExponent = publicExponent;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the modulus.
|
||||
*
|
||||
* @return the modulus
|
||||
*/
|
||||
public BigInteger getModulus() {
|
||||
return this.modulus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the public exponent.
|
||||
*
|
||||
* @return the public exponent
|
||||
*/
|
||||
public BigInteger getPublicExponent() {
|
||||
return this.publicExponent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the parameters associated with this key, may be null if not
|
||||
* present.
|
||||
*
|
||||
* @return the parameters associated with this key
|
||||
* @since 8
|
||||
*/
|
||||
public AlgorithmParameterSpec getParams() {
|
||||
return this.params;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user