java linux aes_java AES 加密和linux解密

java AES 加密:package app;

import static java.nio.charset.StandardCharsets.US_ASCII;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.net.URLEncoder;

import java.security.MessageDigest;

import java.security.SecureRandom;

import java.util.Arrays;

import java.util.Base64;

import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

public class Test{

/** OpenSSL's magic initial bytes. */

private static final String SALTED_STR = "Salted__";

private static final byte[] SALTED_MAGIC = SALTED_STR.getBytes(US_ASCII);

/**

*

* @param password The password / key to encrypt with.

* @param data The data to encrypt

* @return A base64 encoded string containing the encrypted data.

*/

static String encrypt(String password, String clearText) throws Exception{

final byte[] pass = password.getBytes(US_ASCII);

final byte[] salt = (new SecureRandom()).generateSeed(8);

final byte[] inBytes = clearText.getBytes(UTF_8);

final byte[] passAndSalt = array_concat(pass, salt);

byte[] hash = new byte[0];

byte[] keyAndIv = new byte[0];

for (int i = 0; i < 3 && keyAndIv.length < 48; i++) {

final byte[] hashData = array_concat(hash, passAndSalt);

final MessageDigest md = MessageDigest.getInstance("MD5");

hash = md.digest(hashData);

keyAndIv = array_concat(keyAndIv, hash);

}

final byte[] keyValue = Arrays.copyOfRange(keyAndIv, 0, 16);

final byte[] iv = Arrays.copyOfRange(keyAndIv, 16, 32);

final SecretKeySpec key = new SecretKeySpec(keyValue, "AES");

final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));

byte[] data = cipher.doFinal(inBytes);

data = array_concat(array_concat(SALTED_MAGIC, salt), data);

return Base64.getEncoder().encodeToString( data );

}

/**

* @see http://stackoverflow.com/questions/32508961/java-equivalent-of-an-openssl-aes-cbc-encryption for what looks like a useful answer. The not-yet-commons-ssl also has an implementation

* @param password

* @param source The encrypted data

* @return

*/

static String decrypt(String password, String source) throws Exception{

final byte[] pass = password.getBytes(US_ASCII);

final byte[] inBytes = Base64.getDecoder().decode(source);

final byte[] shouldBeMagic = Arrays.copyOfRange(inBytes, 0, SALTED_MAGIC.length);

if (!Arrays.equals(shouldBeMagic, SALTED_MAGIC)) {

throw new IllegalArgumentException("Initial bytes from input do not match OpenSSL SALTED_MAGIC salt value.");

}

final byte[] salt = Arrays.copyOfRange(inBytes, SALTED_MAGIC.length, SALTED_MAGIC.length + 8);

final byte[] passAndSalt = array_concat(pass, salt);

byte[] hash = new byte[0];

byte[] keyAndIv = new byte[0];

for (int i = 0; i < 3 && keyAndIv.length < 48; i++) {

final byte[] hashData = array_concat(hash, passAndSalt);

final MessageDigest md = MessageDigest.getInstance("MD5");

hash = md.digest(hashData);

keyAndIv = array_concat(keyAndIv, hash);

}

final byte[] keyValue = Arrays.copyOfRange(keyAndIv, 0, 32);

final SecretKeySpec key = new SecretKeySpec(keyValue, "AES");

final byte[] iv = Arrays.copyOfRange(keyAndIv, 32, 48);

final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));

final byte[] clear = cipher.doFinal(inBytes, 16, inBytes.length - 16);

return new String(clear, UTF_8);

}

private static byte[] array_concat(final byte[] a, final byte[] b) {

final byte[] c = new byte[a.length + b.length];

System.arraycopy(a, 0, c, 0, a.length);

System.arraycopy(b, 0, c, a.length, b.length);

return c;

}

public static void main(String[] args){

try {

String a = encrypt("tecmint","suning@123");

System.out.println(a);

} catch (Exception e) {

e.printStackTrace();

}

}

}

shell 调用系统函数解密:SPASS=$(echo ${SPASS} | openssl enc -aes-128-cbc -a -d -pass pass:tecmint)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值