List of usage examples for java.security NoSuchAlgorithmException toString
public String toString()
From source file:main.java.vasolsim.common.file.__NONUSEDREFERENCE_VaSOLSimExam.java
/** * Creates a cipher around a 128bit AES encryption initialized to decryption mode. * * @param key the key for the cipher//from ww w . j a v a2s.c o m * * @return initialized cipher, null if there is an error. The popup manager is used to notify errors. */ protected static Cipher getDecryptionCipher(byte[] key) throws VaSolSimException { byte[] validatedKey = new byte[algorithmExpectedKeyLengthInBytes]; /* * Holy crap yes I know this is terribly insecure but if you give this function an encryption key smaller * than 128 bits or of a value than the one you have set in teh static fields we have bigger problems to * deal with. Also, its for school, who is actually going to read this but me haha. If you do, email me with * the subject line LAWL YOUR CRYPTO IS REALLY BAD and we can share a laugh; * * Anyways, if the key is too short, repeat the byte sequence (copy it) until the byte array key is long * enough. */ if (key.length < algorithmExpectedKeyLengthInBytes) { int invalidatedKeyIndex = 0; for (int index = 0; index < algorithmExpectedKeyLengthInBytes; index++) { validatedKey[index] = key[invalidatedKeyIndex++]; if (index >= key.length) invalidatedKeyIndex = 0; } } else if (key.length > algorithmExpectedKeyLengthInBytes) { if (!(key.length == 32 || key.length == 64)) throw new VaSolSimException( ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nBAD KEY: HIGHER KEY NOT POT (256, 512)"); if (key.length == 32) { byte[] xorKey = new byte[16]; byte[] lowerHalf = new byte[16]; byte[] higherHalf = new byte[16]; System.arraycopy(key, 0, lowerHalf, 0, 16); System.arraycopy(key, 16, higherHalf, 0, 16); for (int index = 0; index < 16; index++) { int xorBytes = (int) lowerHalf[index] ^ (int) higherHalf[index]; xorKey[index] = (byte) (0xff & xorBytes); } validatedKey = xorKey; } else { byte[] lowerQuarterOne = new byte[16]; byte[] lowerQuarterTwo = new byte[16]; byte[] higherQuarterOne = new byte[16]; byte[] higherQuarterTwo = new byte[16]; byte[] lowerHalf = new byte[16]; byte[] higherHalf = new byte[16]; byte[] xorKey = new byte[16]; System.arraycopy(key, 0, lowerQuarterOne, 0, 16); System.arraycopy(key, 16, lowerQuarterTwo, 0, 16); System.arraycopy(key, 32, higherQuarterOne, 0, 16); System.arraycopy(key, 48, higherQuarterTwo, 0, 16); for (int index = 0; index < 16; index++) { int xorBytes = (int) lowerQuarterOne[index] ^ (int) lowerQuarterTwo[index]; lowerHalf[index] = (byte) (0xff & xorBytes); } for (int index = 0; index < 16; index++) { int xorBytes = (int) higherQuarterOne[index] ^ (int) higherQuarterTwo[index]; higherHalf[index] = (byte) (0xff & xorBytes); } for (int index = 0; index < 16; index++) { int xorBytes = (int) lowerHalf[index] ^ (int) higherHalf[index]; xorKey[index] = (byte) (0xff & xorBytes); } validatedKey = xorKey; } } else if (key.length == algorithmExpectedKeyLengthInBytes) validatedKey = key; Cipher cipher = null; try { System.out.println(validatedKey.length); cipher = Cipher.getInstance(serviceProviderInterface, serviceProvider); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(validatedKey, algorithm), new IvParameterSpec(IV)); } catch (NoSuchAlgorithmException e) { throw new VaSolSimException(ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nBAD ALGORITHM\n" + e.toString() + "\n" + e.getCause() + "\n" + ExceptionUtils.getStackTrace(e), e); } catch (NoSuchProviderException e) { throw new VaSolSimException(ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nBAD PROVIDER\n" + e.toString() + "\n" + e.getCause() + "\n" + ExceptionUtils.getStackTrace(e), e); } catch (NoSuchPaddingException e) { throw new VaSolSimException(ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nNO SUCH PADDING\n" + e.toString() + "\n" + e.getCause() + "\n" + ExceptionUtils.getStackTrace(e), e); } catch (InvalidKeyException e) { throw new VaSolSimException(ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nBAD KEY\n" + e.toString() + "\n" + e.getCause() + "\n" + ExceptionUtils.getStackTrace(e), e); } catch (InvalidAlgorithmParameterException e) { throw new VaSolSimException(ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nBAD ALGORITHM PARAMS\n" + e.toString() + "\n" + e.getCause() + "\n" + ExceptionUtils.getStackTrace(e), e); } return cipher; }
From source file:main.java.vasolsim.common.file.__NONUSEDREFERENCE_VaSOLSimExam.java
/** * Creates a cipher around a statically defined DEFAULT_ENCRYPTION_ALGORITHM, AES 128bit by default, initialized to * encryption mode.//from w ww.j a va2 s.c o m * * @param key secure hash of some sort of the key * * @return initialized cipher, null if internal exception * * @throws VaSolSimException any internal cryptographic related exceptions throw this for debugging to the user */ protected static Cipher getEncryptionCipher(byte[] key) throws VaSolSimException { byte[] validatedKey = new byte[algorithmExpectedKeyLengthInBytes]; /* * Holy crap yes I know this is terribly insecure but if you give this function an encryption key smaller * than 128 bits or of a value than the one you have set in teh static fields we have bigger problems to * deal with. Also, its for school, who is actually going to read this but me haha. If you do, email me with * the subject line LAWL YOUR CRYPTO IS REALLY BAD and we can share a laugh; * * Anyways, if the key is too short, repeat the byte sequence (copy it) until the byte array key is long * enough. */ if (key.length < algorithmExpectedKeyLengthInBytes) { int invalidatedKeyIndex = 0; for (int index = 0; index < algorithmExpectedKeyLengthInBytes; index++) { validatedKey[index] = key[invalidatedKeyIndex++]; if (index >= key.length) invalidatedKeyIndex = 0; } } else if (key.length > algorithmExpectedKeyLengthInBytes) { if (!(key.length == 32 || key.length == 64)) throw new VaSolSimException( ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nBAD KEY: HIGHER KEY NOT POT (256, 512)"); if (key.length == 32) { byte[] xorKey = new byte[16]; byte[] lowerHalf = new byte[16]; byte[] higherHalf = new byte[16]; System.arraycopy(key, 0, lowerHalf, 0, 16); System.arraycopy(key, 16, higherHalf, 0, 16); for (int index = 0; index < 16; index++) { int xorBytes = (int) lowerHalf[index] ^ (int) higherHalf[index]; xorKey[index] = (byte) (0xff & xorBytes); } validatedKey = xorKey; } else { byte[] lowerQuarterOne = new byte[16]; byte[] lowerQuarterTwo = new byte[16]; byte[] higherQuarterOne = new byte[16]; byte[] higherQuarterTwo = new byte[16]; byte[] lowerHalf = new byte[16]; byte[] higherHalf = new byte[16]; byte[] xorKey = new byte[16]; System.arraycopy(key, 0, lowerQuarterOne, 0, 16); System.arraycopy(key, 16, lowerQuarterTwo, 0, 16); System.arraycopy(key, 32, higherQuarterOne, 0, 16); System.arraycopy(key, 48, higherQuarterTwo, 0, 16); for (int index = 0; index < 16; index++) { int xorBytes = (int) lowerQuarterOne[index] ^ (int) lowerQuarterTwo[index]; lowerHalf[index] = (byte) (0xff & xorBytes); } for (int index = 0; index < 16; index++) { int xorBytes = (int) higherQuarterOne[index] ^ (int) higherQuarterTwo[index]; higherHalf[index] = (byte) (0xff & xorBytes); } for (int index = 0; index < 16; index++) { int xorBytes = (int) lowerHalf[index] ^ (int) higherHalf[index]; xorKey[index] = (byte) (0xff & xorBytes); } validatedKey = xorKey; } } else if (key.length == algorithmExpectedKeyLengthInBytes) validatedKey = key; /* * Initialize the Cipher */ Cipher cipher = null; try { cipher = Cipher.getInstance(serviceProviderInterface, serviceProvider); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(validatedKey, algorithm), new IvParameterSpec(IV)); } catch (NoSuchAlgorithmException e) { throw new VaSolSimException(ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nBAD ALGORITHM\n" + e.toString() + "\n" + e.getCause() + "\n" + ExceptionUtils.getStackTrace(e), e); } catch (NoSuchProviderException e) { throw new VaSolSimException(ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nBAD PROVIDER\n" + e.toString() + "\n" + e.getCause() + "\n" + ExceptionUtils.getStackTrace(e), e); } catch (NoSuchPaddingException e) { throw new VaSolSimException(ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nNO SUCH PADDING\n" + e.toString() + "\n" + e.getCause() + "\n" + ExceptionUtils.getStackTrace(e), e); } catch (InvalidKeyException e) { throw new VaSolSimException(ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nBAD KEY\n" + e.toString() + "\n" + e.getCause() + "\n" + ExceptionUtils.getStackTrace(e), e); } catch (InvalidAlgorithmParameterException e) { throw new VaSolSimException(ERROR_MESSAGE_GENERIC_CRYPTO + "\n\nBAD ALGORITHM PARAMS\n" + e.toString() + "\n" + e.getCause() + "\n" + ExceptionUtils.getStackTrace(e), e); } return cipher; }
From source file:net.issarlk.androbunny.inkbunny.File.java
public URI getLargerCachedImage(int w, int h) { Dimension2D wanted = new Dimension2D(w, h); if (this.file_urls == null || this.file_urls.length < 1) { return null; }//from w w w .j av a2 s.co m int i; URI result = null; Dimension2D resultDim = null; java.io.File cachedir = AndrobunnyAppSingleton.androbunnyapp.getCacheDir(); for (i = 2; i >= 0; i--) { byte[] bytesOfMessage = this.file_urls[i].toString().getBytes(); MessageDigest md; try { md = MessageDigest.getInstance("MD5"); byte[] thedigest = md.digest(bytesOfMessage); StringBuffer hexString = new StringBuffer(); for (int j = 0; j < thedigest.length; j++) { hexString.append(Integer.toHexString(0xFF & thedigest[j])); } java.io.File cachefile = new java.io.File(cachedir, hexString.toString()); if (cachefile.exists()) { if (resultDim == null || (!larger(this.dimensions[i], resultDim) && larger(this.dimensions[i], wanted))) { result = this.file_urls[i]; resultDim = this.dimensions[i]; } } Log.d(TAG, "getLargerCachedImage(" + w + ", " + h + ") returning " + result); } catch (NoSuchAlgorithmException e) { Log.e(TAG, e.toString()); } } return result; }
From source file:org.kaaproject.kaa.server.transports.http.transport.HttpTestClient.java
/** * Initialization of request keys and encoder/decoder * * @param serverPublicKey - server public key * @throws Exception - if key generation failed. *///from w w w .j a v a2 s .c o m private void init(PublicKey serverPublicKey) throws Exception { KeyPairGenerator clientKeyGen; try { clientKeyGen = KeyPairGenerator.getInstance("RSA"); clientKeyGen.initialize(2048); KeyPair clientKeyPair = clientKeyGen.genKeyPair(); clientPrivateKey = clientKeyPair.getPrivate(); clientPublicKey = clientKeyPair.getPublic(); } catch (NoSuchAlgorithmException e) { throw new Exception(e.toString()); } crypt = new MessageEncoderDecoder(clientPrivateKey, clientPublicKey, serverPublicKey); try { key = crypt.getEncodedSessionKey(); } catch (GeneralSecurityException e) { throw new Exception(e.toString()); } ByteBuffer publicKeyBuffer = ByteBuffer .wrap(EndpointObjectHash.fromSha1(clientPublicKey.getEncoded()).getData()); clientPublicKeyHash = EndpointObjectHash.fromBytes(publicKeyBuffer.array()); }
From source file:org.texai.torrent.domainEntity.MetaInfo.java
/** Returns the hex-encoded digest of the piece hashes. * * @return the hex-encoded digest of the piece hashes *//* w w w .jav a 2 s . com*/ public String getDigestedPieceHashes() { try { final MessageDigest digest = MessageDigest.getInstance("SHA"); return hexEncode(digest.digest(pieceHashes)); } catch (final NoSuchAlgorithmException nsa) { throw new InternalError(nsa.toString()); // NOPMD } }
From source file:org.texai.torrent.domainEntity.MetaInfo.java
/** Calculates the info hash. * * @return the info hash/*from w w w.j a v a2 s .c om*/ */ private byte[] calculateInfoHash() { final Map<String, Object> info = createInfoMap(); final byte[] infoBytes = BEncoder.bencode(info); try { final MessageDigest digest = MessageDigest.getInstance("SHA"); return digest.digest(infoBytes); } catch (final NoSuchAlgorithmException nsa) { throw new InternalError(nsa.toString()); // NOPMD } }
From source file:com.all.client.model.ContactFolder.java
private String createId(String name) { String hashcode = name + System.currentTimeMillis() + new Random().nextLong(); try {/* ww w .j av a 2s .com*/ MessageDigest md = MessageDigest.getInstance("SHA1"); md.update(hashcode.getBytes()); hashcode = Hashcoder.toHex(md.digest()); } catch (NoSuchAlgorithmException e) { log.error(e.toString()); } return hashcode; }
From source file:cz.muni.fi.japanesedictionary.entity.Translation.java
public String getIndexHash() { StringBuilder hashString = new StringBuilder(); hashString.append(getJapaneseKeb()).append(getJapaneseReb()); try {/*w w w.j ava2 s . c o m*/ MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); byte[] hashBytes = messageDigest.digest(hashString.toString().getBytes()); return new BigInteger(1, hashBytes).toString(16); } catch (NoSuchAlgorithmException e) { Log.e(LOG_TAG, "getting getIndexHash() hash failed: " + e.toString()); } return null; }
From source file:com.mozilla.simplepush.simplepushdemoapp.MainActivity.java
private String genSignature(UrlEncodedFormEntity body) throws IOException { String content = EntityUtils.toString(body); SecretKeySpec key = new SecretKeySpec(this.SharedSecret.getBytes("UTF-8"), "HmacSHA256"); try {//ww w . j a va 2 s . c o m Mac mac = Mac.getInstance("HmacSHA256"); mac.init(key); byte[] bytes = mac.doFinal(content.getBytes("UTF-8")); return bytesToHex(bytes); } catch (NoSuchAlgorithmException x) { this.err("Invalid hash algo specified, failing " + x.toString()); throw new IOException("HmacSHA256 unavailable"); } catch (InvalidKeyException x) { this.err("Invalid key specified, failing " + x.toString()); throw new IOException("Invalid Key"); } }