- // 'key' should be 32 bytes for AES256, will be null-padded otherwise
- char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused)
- bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)
- CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
- keyPtr, kCCKeySizeAES256,
- NULL /* initialization vector (optional) */,
- [self bytes], dataLength, /* input */
- buffer, bufferSize, /* output */
- &numBytesEncrypted);
- private static final String AES_SECRET = "PreDefinedKey";
- /**
- * Method for AES encryption
- * @param raw
- * @param plain
- * @return
- * @throws Exception
- */
- private static byte[] encrypt(byte[] raw, byte[] plain) throws Exception {
- SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES/ECB/PKCS7Padding");
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
- cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
- byte[] encrypted = cipher.doFinal(plain);
- return encrypted;
- }
- /**
- * AES decryption
- * @param encryptMsg
- * @return
- * @throws Exception
- */
- public static String AESDecrypt(String encryptMsg)
- throws Exception {
- byte[] rawKey = getRawKey(AES_SECRET.getBytes());
- //byte[] enc = toByte(encryptMsg);
- byte[] enc = Base64.decode(encryptMsg, 0);
- byte[] result = decrypt(rawKey, enc);
- return new String(result);
- }
- /**
- * Method for AES decryption
- * @param raw
- * @param encrypted
- * @return
- * @throws Exception
- */
- private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
- SecretKeySpec keySpec = new SecretKeySpec(raw, "AES/ECB/PKCS7Padding");
- Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
- cipher.init(Cipher.DECRYPT_MODE, keySpec);
- byte[] decrypted = cipher.doFinal(encrypted);
- return decrypted;
- }
- public static byte[] getRawKey(byte[] seed) throws Exception {
- KeyGenerator kgen = KeyGenerator.getInstance("AES");
- SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
- sr.setSeed(seed);
- //Init for 256bit AES key
- kgen.init(256);
- SecretKey secret = kgen.generateKey();
- //Get secret raw key
- byte[] raw = secret.getEncoded();
- return seed;
- }
- // zeros by default
- byte[] rawKey = new byte[32];
- // if you don't specify the encoding you might get weird results
- byte[] keyBytes = AES_SECRET.getBytes("ASCII");
- System.arraycopy(keyBytes, 0, rawKey, 0, keyBytes.length);
- SecretKey key = new SecretKeySpec(rawKey, "AES");
- Cipher cipher = ...
- // rest of your decryption code
Friday, February 7, 2014
Subscribe to:
Post Comments (Atom)
Ukraine politics Intel ( BACK@WAR @EmbaixadaRusPt "this was is about money" they say Trump is n negociating bcz he had a stroke US intel is deep working in agreement since minerals r crucial who holds Power now?)
https://www.yahoo.com/news/ukraine-delays-critical-minerals-deal-130009574.html?fr=sycsrp_catchall
No comments:
Post a Comment