Example usage for java.security NoSuchAlgorithmException getMessage

List of usage examples for java.security NoSuchAlgorithmException getMessage

Introduction

In this page you can find the example usage for java.security NoSuchAlgorithmException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.clustercontrol.util.KeyCheck.java

/**
 * ????????//ww  w  . j  a v a2s.com
 * 
 * @param args
 */
public static void main(String[] args) {
    PrivateKey privateKey = null;
    PublicKey publicKey = null;

    /// ??????? true
    /// ???????? false (?)
    boolean flag = false;
    if (flag) {
        try {
            // ?
            privateKey = getPrivateKey(
                    "???????privateKey.txt??");

            // ?
            publicKey = getPublicKey("???????");
            // publicKey = getPublicKey(publicKeyStr);
        } catch (Exception e) {
            System.out.println("hoge" + e.getMessage());
        }
    } else {
        KeyPairGenerator generator;
        try {
            generator = KeyPairGenerator.getInstance(ALGORITHM);
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            // ?? 1024
            generator.initialize(1024, random);
            KeyPair keyPair = generator.generateKeyPair();
            privateKey = keyPair.getPrivate();
            publicKey = keyPair.getPublic();
        } catch (NoSuchAlgorithmException ex) {
            System.out.println(ex.getMessage());
        }
    }

    //
    // ?
    System.out.println("?");
    System.out.println(byte2String(privateKey.getEncoded()));
    System.out.println("?");
    System.out.println(byte2String(publicKey.getEncoded()));

    // ???????
    String string = "20140701_nttdata";
    byte[] src = string.getBytes();
    System.out.println("??String");
    System.out.println(string);
    System.out.println("??byte");
    System.out.println(byte2String(src));

    // ?
    try {
        String encStr = encrypt(string, privateKey);
        System.out.println("?");
        System.out.println(encStr);

        // ?
        String decStr = decrypt(encStr, publicKey);
        System.out.println("?");
        System.out.println(decStr);
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}

From source file:org.scantegrity.scanner.Scanner.java

/**
 * Main Logic loop./*from   ww  w  .  ja  v  a2 s .co m*/
 * 
 * @param args CLI flags
 */
public static void main(String[] args) {
    processCmdLine(args);

    //Create the locations we should output files too
    createOutputDirectories();

    //register logging handlers if any
    c_myId = c_config.getPollID();
    c_log = new Logging(c_outDirs, c_myId, Level.FINEST);
    c_log.log(Level.INFO, "Logging Intialized");
    c_log.log(Level.INFO, "Scanner ID Number: " + c_myId);

    c_log.log(Level.INFO, "Initializing Cryptographic hash and rng.");
    try {
        c_hash = MessageDigest.getInstance("SHA1");
    } catch (NoSuchAlgorithmException l_e) {
        l_e.printStackTrace();
        c_hash = null;
        c_log.log(Level.SEVERE, "Unable to initialize hash. Reason: " + l_e.getMessage());
    }
    try {
        c_csprng = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException l_e) {
        l_e.printStackTrace();
        c_csprng = null;
        c_log.log(Level.SEVERE, "Unable to initialize RNG. Reason: " + l_e.getMessage());
    }

    //init counters
    c_scanCount = new Integer(0);
    c_ballotCount = new Integer(0);
    c_errorCount = new Integer(0);
    checkForPreviousCounters();
    writeCounters();

    //init ballot storage
    c_ballotIds = new Vector<Integer>();
    //TODO: Change this size to be variable...
    c_store = initializeBallotStore(c_outDirs, 100 * 1024 * 1024);

    //start the election
    c_log.log(Level.SEVERE, "Election Started");

    playAudioClip(2);

    //main loop
    //TODO: terminating condition, button, or special ballot???
    while (true) {
        BufferedImage l_ballotImg[] = null;
        Ballot l_ballot = null;

        //process image into ballot
        l_ballotImg = getBallotImages();

        if (l_ballotImg == null || (l_ballotImg[0] == null && l_ballotImg[1] == null))
            continue;

        //scan count
        c_scanCount++;
        writeCounters();

        playAudioClip(0);

        for (int l_c = 0; l_c < l_ballotImg.length; l_c++) {
            //Ignore empties
            if (l_ballotImg[l_c] == null) {
                c_log.log(Level.WARNING,
                        "Only 1 ballot object returned." + " Make sure the scanner supports duplex");
                continue;
            }
            //Ignore blank pages
            if (DrunkDriver.isDrunk(l_ballotImg[l_c], 10))
                continue;

            l_ballot = getBallot(l_ballotImg[l_c]);

            if (l_ballot == null)
                continue;

            //update ballot counter
            c_ballotCount++;
            writeCounters();

            l_ballot.setScannerId(c_myId);

            if (isDuplicate(l_ballot)) {
                c_log.log(Level.WARNING, "Duplicate Ballot detected. ID : " + l_ballot.getId());
                l_ballot.setCounted(false);
                l_ballot.addNote("Duplicate Ballot");
            }

            //check if the ballot is a "starting ballot"

            //check if the ballot is a "closing ballot"

            //else
            saveBallot(l_ballot);
        }
        //resume scanning
    }

    //end election (ballot handler)

    //turn off storage

    //disconnect devices 

    //turn off log 

    //quit
    //endElection(); 
}

From source file:Main.java

public static byte[] getMessageDigest(byte[] data, String algorithm) {
    try {// ww  w  .  j  a  v a  2  s. com
        final MessageDigest md = MessageDigest.getInstance(algorithm);
        final byte[] digest = md.digest(data);
        return digest;
    } catch (final NoSuchAlgorithmException e) {
        Log.e(e.getMessage(), e.getMessage());
        throw new RuntimeException(e);
    }
}

From source file:Main.java

static MessageDigest getDigest(String algorithm) {
    try {//from  w  w  w.j  a va  2s . c  om
        return MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:MD5.java

/**
 * MD5 BASE64 checksum for the specified input string.
 * /*from ww w.j ava 2  s.  c o m*/
 * @param input -
 *          Specified input string
 * @return String - MD5 BASE64 sum
 */
public static String checkMD5(String input) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input.getBytes());
        byte[] enc = md.digest();
        String md5Sum = new sun.misc.BASE64Encoder().encode(enc);
        return md5Sum;
    } catch (NoSuchAlgorithmException nsae) {
        System.out.println(nsae.getMessage());
        return null;
    }
}

From source file:Main.java

public static String md5One(String s) {
    MessageDigest md = null;/* w  w  w .  j  av a  2s.  com*/
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
    md.update(s.getBytes());
    return byteArrayToHexString(md.digest());
}

From source file:Main.java

public static String MD5(String md5) {
    if (md5 == null) {
        return null;
    }//from w ww.j a v  a 2s . c om
    try {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuilder sb = new StringBuilder();
        for (byte anArray : array) {
            sb.append(Integer.toHexString((anArray & 0xFF) | 0x100).substring(1, 3));
        }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
        Log.e("tmessages", e.getMessage());
    }
    return null;
}

From source file:Main.java

/**
 * Generates a public/private key pair that meets Thali's security requirements
 * @return//  w  w  w.j a v  a 2  s.  c  om
 */
public static KeyPair GenerateThaliAcceptablePublicPrivateKeyPair() {
    KeyPairGenerator keyPairGenerator = null;
    try {
        keyPairGenerator = KeyPairGenerator.getInstance(KeyTypeIdentifier);
        // TODO: http://android-developers.blogspot.com/2013/08/some-securerandom-thoughts.html talks about security
        // failures in Android caused by improperly initialized RNGs. It would appear that this issue doesn't
        // apply to the latest version of Android. But obviously this is something that has to be further investigated
        // to make sure we are doing this correctly.
        keyPairGenerator.initialize(KeySizeInBits, new SecureRandom());
        return keyPairGenerator.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.innoq.liqid.utils.SHACrypt.java

public static String encrypt(final String plaintext) {
    MessageDigest md = null;//w  w  w .  j  a  va  2s .  c o m
    try {
        md = MessageDigest.getInstance("SHA");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
    try {
        md.update(plaintext.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage());
    }
    byte[] raw = md.digest();
    return Base64.encodeBase64String(raw);
}

From source file:Main.java

public static String md5Three(String clientId, String pwd, String timestamp) {
    clientId = clientId == null ? "" : clientId;
    pwd = pwd == null ? "" : pwd;
    timestamp = timestamp == null ? "" : timestamp;
    while (timestamp.length() < 10) {
        timestamp = "0" + timestamp;
    }/*from  www  . ja v  a  2  s. co  m*/
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
    md.update(clientId.getBytes());
    md.update(new byte[7]);
    md.update(pwd.getBytes());
    md.update(timestamp.getBytes());

    return byteArrayToHexString(md.digest());
}