Java Code Examples for org.bouncycastle.util.encoders.Hex#encode()

The following examples show how to use org.bouncycastle.util.encoders.Hex#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RsaEncryption.java    From cstc with GNU General Public License v3.0 6 votes vote down vote up
protected byte[] perform(byte[] input) throws Exception {

		if( ! this.certAvailable.isSelected() )
			throw new IllegalArgumentException("No certificate available.");
		
		String padding = (String)paddings.getSelectedItem();
		Cipher cipher = Cipher.getInstance(String.format("%s/%s/%s", algorithm, cipherMode, padding));
        cipher.init(Cipher.ENCRYPT_MODE, this.cert.getPublicKey());
        
        String selectedInputMode = (String)inputMode.getSelectedItem();
        String selectedOutputMode = (String)outputMode.getSelectedItem();
        
        if( selectedInputMode.equals("Hex") )
        	input = Hex.decode(input);
        if( selectedInputMode.equals("Base64") )
        	input = Base64.decode(input);
      
		byte[] encrypted = cipher.doFinal(input);
		
		if( selectedOutputMode.equals("Hex") )
			encrypted = Hex.encode(encrypted);
		if( selectedOutputMode.equals("Base64") )
			encrypted = Base64.encode(encrypted);

		return encrypted;
	}
 
Example 2
Source File: MaskedField.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
private byte[] blake2bHash(byte[] in) {
    final Blake2bDigest hash = new Blake2bDigest(null, 32, null, defaultSalt);
    hash.update(in, 0, in.length);
    final byte[] out = new byte[hash.getDigestSize()];
    hash.doFinal(out, 0);
    return Hex.encode(out);
}
 
Example 3
Source File: SM3Digest.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    byte[] md = new byte[32];
    String strMsg = "123456";
    String strHash = "debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732";
    byte[] msg1 = strMsg.getBytes();
    System.out.println("签名原文:" + strMsg);
    SM3Digest sm3 = new SM3Digest();
    sm3.update(msg1, 0, msg1.length);
    sm3.doFinal(md, 0);
    String s = new String(Hex.encode(md));
    System.out.println(s.toUpperCase().compareTo(strHash.toUpperCase()));
}
 
Example 4
Source File: SM3Digest.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] hash(byte[] input) {
    byte[] md = new byte[32];
    logger.debug("sm3 hash origData:{}", input);
    SM3Digest sm3 = new SM3Digest();
    sm3.update(input, 0, input.length);
    sm3.doFinal(md, 0);
    String s = new String(Hex.encode(md));
    logger.debug("sm3 hash data:{}", s);
    return md;
}
 
Example 5
Source File: SM3Digest.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] hash(byte[] input, int offset, int length) {
    byte[] md = new byte[32];
    logger.debug("sm3 hash origData:{}", input);
    SM3Digest sm3 = new SM3Digest();
    sm3.update(input, offset, length);
    sm3.doFinal(md, 0);
    String s = new String(Hex.encode(md));
    logger.debug("sm3 hash data:{}", s);
    return md;
}
 
Example 6
Source File: CmsCryptoDES.java    From oneops with Apache License 2.0 5 votes vote down vote up
/**
 * Generate des key.
 *
 * @param file the file
 * @throws java.io.IOException Signals that an I/O exception has occurred.
 */
public static void generateDESKey(String file) throws IOException {
    DESedeKeyGenerator kg = new DESedeKeyGenerator();
    KeyGenerationParameters kgp = new KeyGenerationParameters(
            new SecureRandom(),
            DESedeParameters.DES_EDE_KEY_LENGTH * 8);
    kg.init(kgp);
    byte[] key = kg.generateKey();
    BufferedOutputStream keystream =
            new BufferedOutputStream(new FileOutputStream(file));
    byte[] keyhex = Hex.encode(key);
    keystream.write(keyhex, 0, keyhex.length);
    keystream.flush();
    keystream.close();
}
 
Example 7
Source File: Hmac.java    From cstc with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected byte[] perform(byte[] input) throws Exception {
	byte[] key = this.keyTxt.getText().getBytes();
	String algo = "Hmac" + (String) hashAlgoBox.getSelectedItem();
	SecretKeySpec signingKey = new SecretKeySpec(key, algo);
	Mac mac = Mac.getInstance(algo);
	mac.init(signingKey);
	return Hex.encode(mac.doFinal(input));
}
 
Example 8
Source File: ToHex.java    From cstc with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected byte[] perform(byte[] input) throws Exception {
	String selectedKey = (String) this.delimiterBox.getSelectedItem();
	Delimiter delimiter = ToHex.delimiters.get(selectedKey);

	if (delimiter.value.length == 0) { // No delimiter
		return Hex.encode(input);
	}

	ByteArrayOutputStream out = new ByteArrayOutputStream();

	if (input.length > 0 && delimiter.writeAtStart) {
		out.write(delimiter.value);
	}

	for (int i = 0; i < input.length - 1; i++) {
		out.write(Hex.encode(new byte[] { input[i] }));
		out.write(delimiter.value);
	}

	if (input.length > 0) {
		out.write(Hex.encode(new byte[] { input[input.length - 1] })); // wow
		if (delimiter.writeAtEnd) {
			out.write(delimiter.value);
		}
	}

	return out.toByteArray();
}
 
Example 9
Source File: ToolMAC_BCP.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * HmacSHA224Hex消息摘要
 *
 * @param data
 *         待做消息摘要处理的数据
 * @param key
 *         密钥
 *
 * @return String 消息摘要
 *
 * @throws Exception
 */
public static String encodeHmacSHA224Hex(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {

    // 执行消息摘要
    byte[] b = encodeHmacSHA224(data, key);

    // 做十六进制转换
    return new String(Hex.encode(b));
}
 
Example 10
Source File: ToolMD.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * TigerHex加密
 *
 * @param data
 *         待加密数据
 *
 * @return byte[] 消息摘要
 *
 * @throws Exception
 */
public static String encodeTigerHex(byte[] data) throws NoSuchAlgorithmException {

    // 执行消息摘要
    byte[] b = encodeTiger(data);

    // 做十六进制编码处理
    return new String(Hex.encode(b));
}
 
Example 11
Source File: ToolHmacRipeMD.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * HmacRipeMD128Hex消息摘要
 *
 * @param data
 *         待做消息摘要处理的数据
 * @param key
 *         密钥
 *
 * @return byte[] 消息摘要
 *
 * @throws Exception
 */
public static String encodeHmacRipeMD128Hex(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {

    // 执行消息摘要
    byte[] b = encodeHmacRipeMD128(data, key);

    // 做十六进制转换
    return new String(Hex.encode(b));
}
 
Example 12
Source File: ToolMD2.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * MD2加密
 *
 * @param data
 *         待加密数据
 *
 * @return byte[] 消息摘要
 *
 * @throws Exception
 */
public static String encodeMD2Hex(byte[] data) throws NoSuchAlgorithmException {
    // 执行消息摘要
    byte[] b = encodeMD2(data);

    // 做十六进制编码处理
    return new String(Hex.encode(b));
}
 
Example 13
Source File: ToolMD5.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * MD5加密
 *
 * @param data
 *         待加密数据
 *
 * @return String 消息摘要
 *
 * @throws NoSuchAlgorithmException
 */
public static String encodeMD5Hex(byte[] data) throws NoSuchAlgorithmException {
    // 执行消息摘要
    byte[] b = encodeMD5(data);

    // 做十六进制编码处理
    return new String(Hex.encode(b));
}
 
Example 14
Source File: ToolSHA1.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * SHA-1加密
 *
 * @param data
 *         待加密数据
 *
 * @return byte[] 消息摘要
 *
 * @throws Exception
 */
public static String encodeSHAHex(byte[] data) throws NoSuchAlgorithmException {
    // 执行消息摘要
    byte[] b = encodeSHA(data);

    // 做十六进制编码处理
    return new String(Hex.encode(b));
}
 
Example 15
Source File: ToolSHA2.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * SHA-224加密
 *
 * @param data
 *         待加密数据
 *
 * @return byte[] 消息摘要
 *
 * @throws Exception
 */
public static String encodeSHA224Hex(byte[] data) throws NoSuchAlgorithmException {
    // 执行消息摘要
    byte[] b = encodeSHA224(data);

    // 做十六进制编码处理
    return new String(Hex.encode(b));
}
 
Example 16
Source File: ToolHmacRipeMD.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * HmacRipeMD160Hex消息摘要
 *
 * @param data
 *         待做消息摘要处理的数据
 * @param key
 *         密钥
 *
 * @return String 消息摘要
 *
 * @throws Exception
 */
public static String encodeHmacRipeMD160Hex(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {

    // 执行消息摘要
    byte[] b = encodeHmacRipeMD160(data, key);

    // 做十六进制转换
    return new String(Hex.encode(b));
}
 
Example 17
Source File: ToolMD.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * GOST3411Hex加密
 *
 * @param data
 *         待加密数据
 *
 * @return byte[] 消息摘要
 *
 * @throws Exception
 */
public static String encodeGOST3411Hex(byte[] data) throws NoSuchAlgorithmException {

    // 执行消息摘要
    byte[] b = encodeGOST3411(data);

    // 做十六进制编码处理
    return new String(Hex.encode(b));
}
 
Example 18
Source File: ToolMAC_BCP.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * HmacMD2Hex消息摘要
 *
 * @param data
 *         待做消息摘要处理的数据
 * @param key
 *         密钥
 *
 * @return byte[] 消息摘要
 *
 * @throws Exception
 */
public static String encodeHmacMD2Hex(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {

    // 执行消息摘要
    byte[] b = encodeHmacMD2(data, key);

    // 做十六进制转换
    return new String(Hex.encode(b));
}
 
Example 19
Source File: ToolMAC_BCP.java    From protools with Apache License 2.0 3 votes vote down vote up
/**
 * HmacMD4Hex消息摘要
 *
 * @param data
 *         待做消息摘要处理的数据
 * @param key
 *         密钥
 *
 * @return String 消息摘要
 *
 * @throws Exception
 */
public static String encodeHmacMD4Hex(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {

    // 执行消息摘要
    byte[] b = encodeHmacMD4(data, key);

    // 做十六进制转换
    return new String(Hex.encode(b));
}
 
Example 20
Source File: AbstractSAMLArtifact.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Gets the hex encoded artifact.
 * 
 * @return hex encoded artifact
 */
public String hexEncode() {
    return new String(Hex.encode(getArtifactBytes()));
}