List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec
public SecretKeySpec(byte[] key, int offset, int len, String algorithm)
len
bytes of key
, starting at offset
inclusive. From source file:Main.java
public static Key byteArrayKeyToSecurityKeyUsingLength(byte[] key) throws Exception { return new SecretKeySpec(key, 0, key.length, "AES"); }
From source file:Main.java
/** * Computes an Ephemeral ID.//w ww . j av a 2 s . co m * @param key AES key (Advertiser Identity Key). The first 16 bytes are used. * @param timeCounter Advertiser time counter * @param rotationExponent Advertiser rotation exponent (0 to 15) * @return Final ephemeral key of 16 bytes, of which only the first 8 bytes should be used. * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ @NonNull public static byte[] computeEID(byte[] key, int timeCounter, byte rotationExponent) throws GeneralSecurityException { // String transformation = "AES/CBC/PKCS5Padding"; String transformation = "AES/ECB/NoPadding"; @SuppressLint("GetInstance") // spec says it has to be ECB, ignore lint warning Cipher aes = Cipher.getInstance(transformation); aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, 0, 16, "AES")); byte[] tempKey = aes .doFinal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, 0x00, 0x00, (byte) ((timeCounter >>> 24) & 0xff), (byte) ((timeCounter >>> 16) & 0xff) }); // clear K lowest bits timeCounter = timeCounter >>> rotationExponent << rotationExponent; // reset cipher with a new encryption key aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(tempKey, "AES")); return aes.doFinal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, rotationExponent, (byte) ((timeCounter >>> 24) & 0xff), (byte) ((timeCounter >>> 16) & 0xff), (byte) ((timeCounter >>> 8) & 0xff), (byte) (timeCounter & 0xff) }); }
From source file:com.jopss.logico.negocio.util.CriptoUtils.java
public static String desEncode(String texto, String chave) { Cipher ecipher;//from w w w .ja va 2 s. c o m SecretKey key; String encod = null; try { key = new SecretKeySpec(chave.getBytes("UTF-8"), 0, 8, "DES"); ecipher = Cipher.getInstance("DES"); ecipher.init(Cipher.ENCRYPT_MODE, key); byte[] utf8 = texto.getBytes("UTF8"); byte[] crip = ecipher.doFinal(utf8); encod = new String(Hex.encodeHex(crip)); } catch (Exception e) { log.error(e); } return encod; }
From source file:com.jopss.logico.negocio.util.CriptoUtils.java
public static String desDecode(String texto, String chave) { Cipher dcipher;/*from ww w. j a va 2 s . c o m*/ SecretKey key; String decod = null; try { key = new SecretKeySpec(chave.getBytes(), 0, 8, "DES"); dcipher = Cipher.getInstance("DES"); dcipher.init(Cipher.DECRYPT_MODE, key); byte[] dec = Hex.decodeHex(texto.toCharArray()); byte[] utf8 = dcipher.doFinal(dec); decod = new String(utf8, "UTF8"); } catch (Exception e) { log.error(e); } return decod; }
From source file:cn.crawin.msg.gateway.http.SignUtil.java
/** * ??//from w w w . j av a 2 s. com * * @param secret APP * @param method HttpMethod * @param path * @param headers * @param querys * @param bodys * @param signHeaderPrefixList ???Header? * @return ??? */ public static String sign(String secret, String method, String path, Map<String, String> headers, Map<String, String> querys, Map<String, String> bodys, List<String> signHeaderPrefixList) { try { Mac hmacSha256 = Mac.getInstance(Constants.HMAC_SHA256); byte[] keyBytes = secret.getBytes(Constants.ENCODING); hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, Constants.HMAC_SHA256)); return new String(Base64.encodeBase64( hmacSha256.doFinal(buildStringToSign(method, path, headers, querys, bodys, signHeaderPrefixList) .getBytes(Constants.ENCODING))), Constants.ENCODING); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.example.aliyundemo.ocr.util.SignUtil.java
/** * ??//from w w w . ja v a2s . co m * * @param method HttpMethod * @param url Path+Query * @param headers Http * @param formParamMap POST?? * @param secret APP * @param signHeaderPrefixList ???Header? * @return ??? */ public static String sign(String method, String url, Map<String, String> headers, Map formParamMap, String secret, List<String> signHeaderPrefixList) { try { Mac hmacSha256 = Mac.getInstance(Constants.HMAC_SHA256); byte[] keyBytes = secret.getBytes(Constants.ENCODING); hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, Constants.HMAC_SHA256)); return new String(Base64.encodeBase64( hmacSha256.doFinal(buildStringToSign(headers, url, formParamMap, method, signHeaderPrefixList) .getBytes(Constants.ENCODING))), Constants.ENCODING); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.ljt.openapi.demo.util.SignUtil.java
/** * ??/*from www . j ava 2 s . com*/ * @param secret APP * @param method HttpMethod * @param path * @param headers * @param querys * @param bodys * @param signHeaderPrefixList ???Header? * @return ??? */ public static String sign(String secret, String method, String path, Map<String, String> headers, Map<String, String> querys, Map<String, String> bodys, List<String> signHeaderPrefixList) { try { Mac hmacSha256 = Mac.getInstance(Constants.HMAC_SHA256); byte[] keyBytes = secret.getBytes(Constants.ENCODING); hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, Constants.HMAC_SHA256)); String sign = new String(Base64.encodeBase64( hmacSha256.doFinal(buildStringToSign(method, path, headers, querys, bodys, signHeaderPrefixList) .getBytes(Constants.ENCODING))), Constants.ENCODING); logger.info("sign:" + sign); return sign; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:de.sainth.recipe.backend.security.AuthFilter.java
AuthFilter(RecipeManagerProperties properties, UserRepository userRepository) { this.userRepository = userRepository; byte[] decodedKey = Base64.getDecoder().decode(properties.getEncodedKey()); key = new SecretKeySpec(decodedKey, 0, decodedKey.length, SignatureAlgorithm.HS256.getValue()); }