Example usage for org.bouncycastle.util.encoders Base64 encode

List of usage examples for org.bouncycastle.util.encoders Base64 encode

Introduction

In this page you can find the example usage for org.bouncycastle.util.encoders Base64 encode.

Prototype

public static byte[] encode(byte[] data) 

Source Link

Document

encode the input data producing a base 64 encoded byte array.

Usage

From source file:de.dimm.vsm.license.HWIDLicenseTicket.java

public static String generate_hwid() throws IOException {
    try {/*from   www. j  a va2s. c  o  m*/
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();

        while (en.hasMoreElements()) {
            NetworkInterface ni = en.nextElement();

            // WE SKIP EMPTY OR TOO SHORT HW-ADDRESS, MUST BE AT LEAST 6 BYTE (48BIT)
            if (ni.getName().startsWith("lo") || ni.getHardwareAddress() == null
                    || ni.getHardwareAddress().length < 6)
                continue;

            byte[] mac = ni.getHardwareAddress();
            int sum = 0;
            for (int i = 0; i < 6; i++) {
                byte b = mac[i];
                sum += b;
            }
            // WE DO NOT ACCEPT EMPTY, THIS IS PROBABLY A VIRTUAL MACHINE
            if (sum == 0)
                continue;

            if (test_vm_machine)
                continue;

            String str_mac = new String(Base64.encode(mac), "UTF-8");
            return str_mac;
        }
    } catch (Exception exc) {
        throw new IOException(exc.getLocalizedMessage());
    }
    if (test_vm_machine)
        System.err.println("WARNING!!!! TEST VM HWID!!!!!");

    // IF WE GET HERE, WE CANNOT LICENSE VIA MAC, WE USE TIMESTAMP OF LICENSE DIRECTORY
    File lic_file = new File(VM_LICFILE);
    if (!lic_file.exists()) {
        create_virtual_hw_lic_file();
    }

    String str_mac = read_virtual_hw_lic_file();

    return str_mac;
}

From source file:de.dimm.vsm.license.HWIDLicenseTicket.java

static String read_virtual_hw_lic_file() {
    File lic_file = new File(VM_LICFILE);
    if (lic_file.exists()) {
        byte[] mac = new byte[6];
        FileInputStream fr = null;
        int rlen = 0;
        try {/*from   w ww .j a  v a 2s  . c o m*/
            fr = new FileInputStream(lic_file);
            rlen = fr.read(mac);

            if (rlen == 6) {
                String str_mac = new String(Base64.encode(mac), "UTF-8");
                return str_mac;
            }
        } catch (IOException iOException) {
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException iOException) {
                }
            }
        }
    }
    return null;
}

From source file:de.dimm.vsm.license.HWIDLicenseTicket.java

@Override
public boolean isValid() {
    if (!super.isValid()) {
        return false;
    }/*from w  w w .  jav a2s  .c o m*/
    try {
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();

        while (en.hasMoreElements()) {
            byte[] mac = en.nextElement().getHardwareAddress();

            if (test_vm_machine)
                continue;

            if (mac != null) {
                String str_mac = new String(Base64.encode(mac), "UTF-8");
                if (str_mac.compareToIgnoreCase(hwid) == 0) {
                    return true;
                }
            }
        }
        String vhwid = read_virtual_hw_lic_file();
        if (vhwid != null && vhwid.compareTo(hwid) == 0) {
            return true;
        }

        lastErrMessage = "HWID_does_not_match";
    } catch (Exception exc) {
        lastErrMessage = "Cannot_check_HWID: " + exc.getLocalizedMessage();
        if (ll != null)
            ll.log_msg(LogListener.LVL_ERR, LogListener.TYP_LICENSE, lastErrMessage);
    }
    return false;
}

From source file:de.dimm.vsm.license.LicenseTicket.java

public String calculate_key() throws IOException {

    String data = get_license_hash_str();
    try {/*w  w  w.j a  v  a2s .  c om*/

        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(hash_key.getBytes(), HMAC_SHA1_ALGORITHM);

        // get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());

        // base64-encode the hmac is atleast 20 byte long
        String result = new String(Base64.encode(rawHmac)).toUpperCase();
        StringBuilder sb = new StringBuilder();
        int idx = 0;
        for (int blocks = 0; blocks < 5; blocks++) {
            if (blocks > 0)
                sb.append('-');
            for (int chars = 0; chars < 4; chars++) {
                // DO NOT ALLOW NONLITERALS OR '0'
                while (!Character.isLetterOrDigit(result.charAt(idx)) || result.charAt(idx) == '0') {
                    idx++;
                    // WRAPAROUND
                    if (idx >= result.length())
                        idx = 0;
                }

                sb.append(result.charAt(idx));
                idx++;
                if (idx >= result.length())
                    idx = 0;
            }
        }
        return sb.toString();

    } catch (Exception e) {
        throw new IOException("Failed to generate license key : " + e.getMessage());
    }
}

From source file:de.htwg_konstanz.in.uce.dht.dht_access.UceDhtKadAdapter.java

License:Open Source License

public boolean put(String key, byte[] value) throws InterruptedException {
    MD4 md4 = new MD4();
    byte[] keyHash = md4.digest(key.getBytes());
    String hashString = org.jmule.core.utils.Convert.byteToHexString(keyHash);
    // publish note
    Publisher publisher = jKadManager.getPublisher();
    TagList tagList = new TagList();
    tagList.addTag(new StringTag(TAG_FILENAME, "filename"));
    tagList.addTag(new IntTag(TAG_FILESIZE, 4043844));
    tagList.addTag(new IntTag(TAG_FILERATING, ED2KConstants.FILE_QUALITY_EXCELLENT));
    tagList.addTag(new StringTag(TAG_DESCRIPTION, new String(Base64.encode(value))));
    final CountDownLatch latch = new CountDownLatch(1);
    try {//from  ww w  .  ja v a  2  s .c o  m
        publisher.publishNote(new Int128(new FileHash(hashString)),
                new PublishItem(new FileHash(hashString), tagList));
        final PublishTask publishTask = publisher.getPublishTask(new Int128(new FileHash(hashString)));
        publisher.addListener(new PublisherListener() {

            public void publishTaskStopped(PublishTask task) {
                if (task.equals(publishTask)) {
                    latch.countDown();
                }
            }

            public void publishTaskStarted(PublishTask task) {
                // TODO Auto-generated method stub

            }

            public void publishTaskRemoved(PublishTask task) {
                // TODO Auto-generated method stub

            }

            public void publishTaskAdded(PublishTask task) {
                // TODO Auto-generated method stub

            }
        });
    } catch (JKadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
    latch.await();
    // TODO wie erfolg prfen?
    return true;
}

From source file:de.hybris.platform.media.storage.impl.MediaCacheRecreatorTest.java

License:Open Source License

private void createRandomCachedFiles(final int num, final String folderName) throws IOException {
    for (int i = 0; i < num; i++) {
        final String location = RandomStringUtils.randomAlphabetic(10);
        Files.createTempFile(Paths.get(System.getProperty("java.io.tmpdir"), folderName),
                new String(Base64.encode(location.getBytes()))
                        + DefaultLocalMediaFileCacheService.CACHE_FILE_NAME_DELIM,
                ".bin");
    }/* w  ww .j av  a 2  s .  com*/
}

From source file:de.mendelson.util.security.BCCryptoHelper.java

/**
 * Calculates the hash value for a passed byte array, base 64 encoded
 *
 * @param digestAlgOID digest OID algorithm, e.g. "1.3.14.3.2.26"
 */// w w  w  .  java2  s .c o m
public String calculateMIC(InputStream dataStream, String digestAlgOID)
        throws GeneralSecurityException, MessagingException, IOException {
    if (dataStream == null) {
        throw new GeneralSecurityException("calculateMIC: Unable to calculate MIC - processed data is absent");
    }
    MessageDigest messageDigest = MessageDigest.getInstance(digestAlgOID, "BC");
    DigestInputStream digestInputStream = new DigestInputStream(dataStream, messageDigest);
    //perform filter operation
    for (byte buf[] = new byte[4096]; digestInputStream.read(buf) >= 0;) {
    }
    byte mic[] = digestInputStream.getMessageDigest().digest();
    digestInputStream.close();
    String micString = new String(Base64.encode(mic));
    return (micString);
}

From source file:de.petendi.commons.crypto.connector.BCConnector.java

License:Apache License

@Override
public byte[] base64Encode(byte[] toEncode) {
    return Base64.encode(toEncode);
}

From source file:edu.amrita.selabs.cumulus.lib.RSAKeyUtil.java

License:Open Source License

public String getEncodedPrivateKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
    initKeyFactory();/*from  w ww .j a  v  a2  s . co  m*/

    PKCS8EncodedKeySpec priv = fact.getKeySpec(privKey, PKCS8EncodedKeySpec.class);

    String s = new String(Base64.encode(priv.getEncoded()));

    return s;
}

From source file:edu.amrita.selabs.cumulus.lib.RSAKeyUtil.java

License:Open Source License

public String getEncodedPublicKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
    initKeyFactory();/*w w  w  . ja va  2  s.c o m*/
    X509EncodedKeySpec pub = fact.getKeySpec(pubKey, X509EncodedKeySpec.class);

    String s = new String(Base64.encode(pub.getEncoded()));
    return s;
}