Created
June 23, 2015 09:54
-
-
Save iolo/866c01db7a7ffc29b217 to your computer and use it in GitHub Desktop.
aes encrypt/descrypt both java and node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.crypto.Cipher; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.security.Key; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Base64; | |
/** | |
* @author iolo | |
*/ | |
public class CryptoUtil { | |
private static final Logger L = LoggerFactory.getLogger(CryptoUtil.class); | |
private static final String ALGORITHM = "AES/ECB/PKCS5Padding"; | |
private static final byte[] KEY_BYTES = { | |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 | |
}; | |
public static String encrypt(String str) { | |
try { | |
// str(utf8) -> bytes -> encrypt -> bytes -> base64(ascii) | |
return new String(Base64.getEncoder().encode(encrypt(str.getBytes("UTF-8"))), "ISO-8859-1"); | |
} catch (Exception e) { | |
//UnsupportedEncodingException... | |
if (L.isWarnEnabled()) { | |
L.warn("encrypt error:", e); | |
} | |
return str; | |
} | |
} | |
public static byte[] encrypt(byte[] data) { | |
try { | |
Cipher cipher = Cipher.getInstance(ALGORITHM); | |
cipher.init(Cipher.ENCRYPT_MODE, getEncryptionKey()); | |
return cipher.doFinal(data); | |
} catch (Exception e) { | |
// GeneralSecurityException | |
if (L.isWarnEnabled()) { | |
L.warn("encrypt error:", e); | |
} | |
return data; | |
} | |
} | |
public static String decrypt(String str) { | |
try { | |
// base64(ascii) -> bytes --> decrypt -> bytes -> str(utf8) | |
return new String(decrypt(Base64.getDecoder().decode(str.getBytes("ISO-8859-1"))), "UTF-8"); | |
} catch (Exception e) { | |
//UnsupportedEncodingException... | |
if (L.isWarnEnabled()) { | |
L.warn("decrypt error:", e); | |
} | |
return str; | |
} | |
} | |
public static byte[] decrypt(byte[] data) { | |
try { | |
Cipher cipher = Cipher.getInstance(ALGORITHM); | |
cipher.init(Cipher.DECRYPT_MODE, getEncryptionKey()); | |
return cipher.doFinal(data); | |
} catch (Exception e) { | |
// GeneralSecurityException | |
if (L.isWarnEnabled()) { | |
L.warn("decrypt error:", e); | |
} | |
return data; | |
} | |
} | |
private static Key getEncryptionKey() { | |
try { | |
return new SecretKeySpec(MessageDigest.getInstance("MD5").digest(KEY_BYTES), "AES"); | |
} catch (NoSuchAlgorithmException e) { | |
throw new Error("failed to get encryption key", e); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var encrypted = 'Q7PibrZRmf14PDN09WqrqA=='; | |
var key = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; | |
var cc = crypto.createDecipher('aes-128-ecb', new Buffer(key)); | |
var decrypted = Buffer.concat([cc.update(encrypted, 'base64'), cc.final()]).toString('utf8'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var decrypted = 'hello'; | |
var key = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; | |
var cc = crypto.createCipher('aes-128-ecb', new Buffer(key)); | |
var encrypted = Buffer.concat([cc.update(decrypted, 'utf8'), cc.final()]).toString('base64'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment