All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vite.api.utils.crypto.CryptoUtils Maven / Gradle / Ivy

The newest version!
package vite.api.utils.crypto;

import net.i2p.crypto.eddsa.Utils;
import net.i2p.crypto.eddsa.math.Field;
import net.i2p.crypto.eddsa.math.FieldElement;
import net.i2p.crypto.eddsa.math.GroupElement;
import net.i2p.crypto.eddsa.math.ed25519.Ed25519LittleEndianEncoding;
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveSpec;
import net.i2p.crypto.eddsa.spec.EdDSANamedCurveTable;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.util.encoders.Hex;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CryptoUtils {


    private synchronized static byte[] computeSecret2(byte[] priA, byte[] pubB) throws InvalidKeyException {

        Curve25519 curve25519 = Curve25519.create();
        byte[] asekey = curve25519.agreement(pubB, priA);


        return asekey;
    }

    public static byte[] toX25519Pk(byte[] pub) {

        byte p32[] = new byte[32];
        System.arraycopy(pub, 0, p32, 0, pub.length);

        EdDSANamedCurveSpec mixed = EdDSANamedCurveTable.getByName("Ed25519");
        //mixed.getB()


        GroupElement a = new GroupElement(mixed.getCurve(), p32, true);
        Field ED25519_FIELD = new Field(
                256, // b
                Utils.hexToBytes("edffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f"), // q
                new Ed25519LittleEndianEncoding());
        FieldElement x = ED25519_FIELD.ZERO;
        FieldElement one_mimus_y = ED25519_FIELD.ZERO;

        one_mimus_y = one_mimus_y.addOne();
        one_mimus_y = one_mimus_y.subtract(a.getY());
        x = x.addOne();
        x = x.add(a.getY());
        one_mimus_y = one_mimus_y.invert();
        x = x.multiply(one_mimus_y);
        byte[] re = x.toByteArray();


        //   Ed25519.getDescriptor

        return re;
    }

    public static byte[] toX25519Sk(byte[] pri) {
        byte[] digest = SignUtils.gethash512to32(pri);
        digest[0] &= 248;
        digest[31] &= 127;
        digest[31] |= 64;
        return digest;
    }

    /**
     * @param prikeyA
     * @param pubKeyB
     * @return
     * @throws InvalidKeyException
     */
    /**
     * public static byte[] generateAESKeyByEd25519KeycomputeSecret(byte[] prikeyA, byte[] pubKeyB) throws InvalidKeyException {
     * byte[] prikeyByte = toX25519Sk(prikeyA);
     * byte[] pubkeyByte = toX25519Pk(pubKeyB);
     * byte[] secretKey = computeSecret(prikeyByte, pubkeyByte);
     * return secretKey;
     * }
     **/

    public static byte[] generateAESKeyByEd25519KeycomputeSecret(byte[] prikeyA, byte[] pubKeyB) throws InvalidKeyException {
        byte[] prikeyByte = toX25519Sk(prikeyA);
        byte[] pubkeyByte = toX25519Pk(pubKeyB);
        byte[] secretKey = null;
        secretKey = computeSecret2(prikeyByte, pubkeyByte);
        return secretKey;
    }


    public static String getSHA256Str(String str) {
        MessageDigest messageDigest;
        String encdeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
            encdeStr = Hex.toHexString(hash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encdeStr;
    }

    public static void main(String[] args) throws Exception {
/**
 Key key = WalletClient.getKeyPairFromMnemonics("orchard else ball universe ginger manage tide gauge detect defy market trigger brother rhythm sauce flower club cute hero sunset essay wink rack share", 0);

 String pubkey = "6125296e76bf16192657902c44381acb5e648cc7a0def8d6808e1d519c38320a";
 System.out.println("pubkey" + pubkey);
 byte[] pubresult = toX25519Pk(Hex.decode(pubkey));
 System.out.println(Hex.toHexString(pubresult));


 String prikey = "418f03dec7547e26c03e362562a303bc263a72efe0c00f9d7657722116a1ca6b";
 System.out.println("prikey" + prikey);

 byte[] priresult = toX25519Sk(Hex.decode(prikey));
 System.out.println(Hex.toHexString(priresult));


 byte[] aes = computeSecret(priresult, pubresult);

 System.out.println("---" + Hex.toHexString(aes));


 String pubkey2 = "3fe14836f0c442ffa7d28e977f44533088e0fc269979e99f7e7c986c2bde6602";
 System.out.println("pubkey" + pubkey2);
 byte[] pubresult2 = toX25519Pk(Hex.decode(pubkey2));
 System.out.println(Hex.toHexString(pubresult2));


 String prikey2 = "67e0749b3de4223f1a1c2f3f56bbbdfac17d8dce1a5666596163fd2322bcfa51";
 System.out.println("prikey" + prikey2);

 byte[] priresult2 = toX25519Sk(Hex.decode(prikey2));
 System.out.println(Hex.toHexString(priresult2));


 byte[] aes2 = computeSecret(priresult2, pubresult2);

 System.out.println("---" + Hex.toHexString(aes2));
 **/
        //  String aaa = "1jyVFWurD8q9Hlv7RY0Oqi9olAU0EWAa7p7J7divYBqy823sGuUB7Af090DubDJUwXnurI9QTcP0\\nTuVNMsdXp+7AgS7ssaJu/MEmo8N+NnG1Z2BS8euTAb39tiu9lqhs02dkWmHTqt1D1HWM7pbTgvFo\\nzodOWp5I3456ARmIbaIUuESdhKl7opf4R1HQIeYZlojKQ0/BDL1YqUqrCp/5Un3vxEHvZJofsIMX\\nM3vCdEXmYPfaxmNHWg/wQwoIXfMb7u83YON3m9gYQiq/KsTA0S7LzXuCLp4kP0AYM0KY5Yh2OVIH\\nJwmsBnp4k3XZUfle0JXP8T3Kv2dg8WbDGfbpzhdBWv0jsWrOcH71H6i4N4LxK+CAifTRVLszAo6i\\nqOUXVDswhiQSUeQhtq2QL2D1+ERZ3CxVWtZZFQF1vH2VBBcT/bb/VhfgIQ3mWaVRE7HN5uLbn3Px\\nbQoReE1YgyuSvECQS70jwHndYUCaOXRp7p0nRK/qIVyVuFkKO9X40svPA30EHAVYkeizmizwdPAc\\npX4ZIMQ02DFkUPOeMzjXxGd8LTTSphVYftlUWTdVQFPid313D6dniTUY072GOCpDhIt7GxoX7FRs\\nAr0W4DkyMay9Hs2XTDXrlqtEupB3tx6BmyknGmv9xLgTzDpbMU/bLMKMqUB2ZlIqAsiTBj7Pfeh+\\n1cfqJfRl8qIMbfF4mSztqMqWV4Cx9BfVHLROozQEPYUzmDvdWedPT8Od7Jc/FOYlyR9NViKmbZvS\\nmyrp98PrYdJ9BP1KPp5K6v3tiGY59xUr7Q2x4YhLMwX7/dmSZbecB5nIsiqbG9VzCHZv8Dkg5cDt\\npu4NTF/kVf47Hln7fcy9USZJPvJk3js9kay7pbmcoO8L3gSkAWACiCbHXeZAZg4ZMV7bjH+OdbNg\\nQZWZfMXTJlUOii+CSW6NbnZVqOWETyRlmrCgSdyQBR8nXoYDw3DTsQkbNaccEmuk1T74VR4UaJFG\\n9tO6/Al0NsiQgirNiTWLsEudNBXZ1fU4Rf9jBciE3vE/pyADsw/E5Uze+iveKweBmC7iuP4G49sw\\ntnd6ZLu9nxYLJKOmGKyJTEhMaZkoDWWDFhHvwtnMHkhJbYsnovkEZDMOTHDAT52WQhK/+7xnaeFf\\nZV4lN9UvCHGt10urDcbitx6zYeJOb217o/iiKfgG62OF3lbtQZfbGRjIgcEf13kDsRhfaEbtSe8T\\nYFueOHK9cU3xtO8DaV1A42rTyOnI6Kz759wjRwFZ4p2ZpqNkpoP54LSNnacJHJ0myOdVKyTg6Mqc\\nQ2lwJZQ+fDQ/MMHeCvbcWJy5349I/dKhjHqBvMwUUIk/qCfPZHsGuMxn/nxOU9LIoSt5/5lvUijc\\nMC5rjm6y4emIAV96y0h+qlzW4E0f2B7rfg==";

        for (int i = 0; i < 1; i++) {
            Base64 base64 = new Base64();
            //String tmp = base64.encodeAsString("111".getBytes("utf-8"));

            //    System.out.println(tmp);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        byte[] aesKey = CryptoUtils.generateAESKeyByEd25519KeycomputeSecret(Hex.decode("f6b5056b2faee2190426cafda6c87039af5a3b88656b70a5b4421f8d176157d5"), base64.decode("ZHLdAN96JFjaQk0kvKCQLQJYRn5hfzueM4JcnrNYaAA="));
                        byte[] aesKey2 = CryptoUtils.generateAESKeyByEd25519KeycomputeSecret(Hex.decode("f6b5056b2faee2190426cafda6c87039af5a3b88656b70a5b4421f8d176157d5"), base64.decode("ZHLdAN96JFjaQk0kvKCQLQJYRn5hfzueM4JcnrNYaAA="));
                        System.out.println(Hex.toHexString(aesKey));
                        System.out.println(Hex.toHexString(aesKey2));

                    } catch (InvalidKeyException e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }


        //uploadFile:seceret:private f6b5056b2faee2190426cafda6c87039af5a3b88656b70a5b4421f8d176157d5  public6472dd00df7a2458da424d24bca0902d0258467e617f3b9e33825c9eb3586800   pubbase64ZHLdAN96JFjaQk0kvKCQLQJYRn5hfzueM4JcnrNYaAA=
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy