List of usage examples for javax.crypto Cipher update
public final int update(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException
From source file:hjow.hgtable.util.SecurityUtil.java
/** * <p>? ? ?? ? .</p>/*w ww . j ava 2 s . co m*/ * * @param text : ? ? * @param key : ? ? * @param algorithm : ? ? (null AES ) * @return ?? ? */ public static String decrypt(String text, String key, String algorithm) { try { String passwords = hash(key, null); String methods = algorithm; if (methods == null) methods = "AES"; String paddings; int need_keySize = -1; boolean useIv = false; byte[] befores = text.getBytes("UTF-8"); byte[] keyByte = passwords.getBytes("UTF-8"); if (methods.equalsIgnoreCase("DES")) { paddings = "DES/CBC/PKCS5Padding"; need_keySize = 8; useIv = true; } else if (methods.equalsIgnoreCase("DESede")) { paddings = "TripleDES/ECB/PKCS5Padding"; need_keySize = 168; useIv = true; } else if (methods.equalsIgnoreCase("AES")) { paddings = "AES"; need_keySize = 16; useIv = false; } else return null; befores = Base64.decodeBase64(befores); byte[] checkKeyByte = new byte[need_keySize]; byte[] ivBytes = new byte[checkKeyByte.length]; for (int i = 0; i < checkKeyByte.length; i++) { if (i < keyByte.length) { checkKeyByte[i] = keyByte[i]; } else { checkKeyByte[i] = 0; } } keyByte = checkKeyByte; SecretKeySpec keySpec = new SecretKeySpec(keyByte, methods); IvParameterSpec ivSpec = null; if (useIv) ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = null; try { cipher = Cipher.getInstance(paddings); if (useIv) { cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); } else { cipher.init(Cipher.DECRYPT_MODE, keySpec); } byte[] outputs = new byte[cipher.getOutputSize(befores.length)]; for (int i = 0; i < outputs.length; i++) { outputs[i] = 0; } int enc_len = cipher.update(befores, 0, befores.length, outputs, 0); enc_len = enc_len + cipher.doFinal(outputs, enc_len); return new String(outputs, "UTF-8").trim(); } catch (Throwable e) { Main.logError(e, Manager.applyStringTable("On decryption")); return text; } } catch (Throwable e) { } return null; }
From source file:hjow.hgtable.util.SecurityUtil.java
/** * <p>? .</p>/* w w w . j a v a 2 s .c om*/ * * @param text : ?? ? ? * @param key : ? ? * @param algorithm : (null AES ) * @return ? ? */ public static String encrypt(String text, String key, String algorithm) { try { String passwords = hash(key, null); String methods = algorithm; if (methods == null) methods = "AES"; String paddings; int need_keySize = -1; boolean useIv = false; byte[] befores = text.trim().getBytes("UTF-8"); byte[] keyByte = passwords.getBytes("UTF-8"); if (methods.equalsIgnoreCase("DES")) { paddings = "DES/CBC/PKCS5Padding"; need_keySize = 8; useIv = true; } else if (methods.equalsIgnoreCase("DESede")) { paddings = "TripleDES/ECB/PKCS5Padding"; need_keySize = 24; useIv = true; } else if (methods.equalsIgnoreCase("AES")) { paddings = "AES"; need_keySize = 16; useIv = false; } else return null; byte[] checkKeyByte = new byte[need_keySize]; byte[] ivBytes = new byte[checkKeyByte.length]; for (int i = 0; i < checkKeyByte.length; i++) { if (i < keyByte.length) { checkKeyByte[i] = keyByte[i]; } else { checkKeyByte[i] = 0; } } keyByte = checkKeyByte; SecretKeySpec keySpec = new SecretKeySpec(keyByte, algorithm); IvParameterSpec ivSpec = null; if (useIv) ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = null; byte[] outputs; try { cipher = Cipher.getInstance(paddings); if (useIv) { cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); } else { cipher.init(Cipher.ENCRYPT_MODE, keySpec); } outputs = new byte[cipher.getOutputSize(befores.length)]; for (int i = 0; i < outputs.length; i++) { outputs[i] = 0; } int enc_len = cipher.update(befores, 0, befores.length, outputs, 0); enc_len = enc_len + cipher.doFinal(outputs, enc_len); return new String(Base64.encodeBase64(outputs), "UTF-8"); } catch (Throwable e) { Main.logError(e, Manager.applyStringTable("On encrypting")); return null; } } catch (Throwable e) { } return null; }
From source file:com.zotoh.core.crypto.JavaOfuscator.java
private String encrypt(String clearText) throws Exception { if (isEmpty(clearText)) { return clearText; }/* w ww. j a v a 2s. co m*/ Cipher c = getCipher(Cipher.ENCRYPT_MODE); ByteOStream baos = new ByteOStream(); byte[] p = asBytes(clearText); byte[] out = new byte[Math.max(4096, c.getOutputSize(p.length))]; int n = c.update(p, 0, p.length, out, 0); if (n > 0) { baos.write(out, 0, n); } n = c.doFinal(out, 0); if (n > 0) { baos.write(out, 0, n); } return Base64.encodeBase64URLSafeString(baos.asBytes()); }
From source file:com.zotoh.core.crypto.JavaOfuscator.java
private String decrypt(String encoded) throws Exception { if (isEmpty(encoded)) { return encoded; }/*from w ww . j a va 2s . co m*/ Cipher c = getCipher(Cipher.DECRYPT_MODE); ByteOStream baos = new ByteOStream(); byte[] p = Base64.decodeBase64(encoded); byte[] out = new byte[Math.max(4096, c.getOutputSize(p.length))]; int n = c.update(p, 0, p.length, out, 0); if (n > 0) { baos.write(out, 0, n); } n = c.doFinal(out, 0); if (n > 0) { baos.write(out, 0, n); } return asString(baos.asBytes()); }
From source file:org.noroomattheinn.tesla.Tesla.java
private String[] getAPIMaterial() { try {/*from ww w .java2 s. com*/ SecretKeySpec key = new SecretKeySpec(apiName.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); String[] m = new String[2]; byte[] plainText = new byte[ci.length]; cipher.init(Cipher.DECRYPT_MODE, key); int ptLength = cipher.update(ci, 0, ci.length, plainText, 0); cipher.doFinal(plainText, ptLength); m[0] = new String(plainText); cipher.init(Cipher.DECRYPT_MODE, key); ptLength = cipher.update(cs, 0, cs.length, plainText, 0); cipher.doFinal(plainText, ptLength); m[1] = new String(plainText); return m; } catch (NoSuchAlgorithmException | InvalidKeyException | ShortBufferException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) { Tesla.logger.severe("Could not decrypt APIMaterial"); throw new Error("Logic Error: Can't decrypt APIMaterial"); } }
From source file:com.example.nfcreaderorigin.MainActivity.java
private byte[] Encrypter(byte[] input) throws Exception { // Security.addProvider(new MainActivity().BouncyCastleProvider()); byte[] keyBytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 };// ww w . j a v a 2 s . co m SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); //mTxtInput.setText(new String(data)); //System.out.println(new String(input)); // encryption pass cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); return cipherText; //System.out.println(new String(cipherText)); //System.out.println(ctLength); // mTxtCipher.setText(new String(cipherText)); // mTxtCtLenght.setText(String.valueOf(ctLength)); // decryption pass }