Java加密、解密--AES_Base64
1 /* 2 * 3 * 4 * 5 * 6 */ 7 package weaver.interfaces.encode; 8 9 import java.security.SecureRandom; 10 11 import javax.crypto.Cipher; 12 import javax.crypto.KeyGenerator; 13 import javax.crypto.SecretKey; 14 import javax.crypto.spec.SecretKeySpec; 15 16 import weaver.general.Base64; 17 import weaver.integration.logging.Logger; 18 import weaver.integration.logging.LoggerFactory; 19 20 /** 21 * AES加密解密,Base64转码 22 * @author chengf 23 * 24 */ 25 public class AES_Base64 implements IEncode { 26 /** 27 * 系统集成日志对象,用于统一输出日志到系统集成日志文件中。 28 */ 29 private static Logger newlog = LoggerFactory.getLogger(AES_Base64.class); 30 31 /** 32 * 密码 33 */ 34 private String pwd = null; 35 36 /** 37 * 构造器 38 */ 39 public AES_Base64() { 40 41 } 42 43 /** 44 * 构造器 45 * @param pwd 密码 46 */ 47 public AES_Base64(String pwd) { 48 this.pwd = pwd; 49 } 50 51 /** 52 * 将传入的明文转换为密文 53 * 54 * @param str 明文字符串 55 * @return 加密后的密文 56 */ 57 public String encode(String str) { 58 byte[] result = null; 59 try { 60 KeyGenerator kgen = KeyGenerator.getInstance("AES"); 61 SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); 62 if (pwd == null) { 63 pwd = "ecology9"; 64 } 65 random.setSeed(pwd.getBytes()); 66 kgen.init(128, random); 67 SecretKey secretKey = kgen.generateKey(); 68 byte[] enCodeFormat = secretKey.getEncoded(); 69 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); 70 71 // 创建密码器 72 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 73 cipher.init(Cipher.ENCRYPT_MODE, key); 74 byte[] byteContent = str.getBytes(); 75 result = cipher.doFinal(byteContent); 76 77 } catch (Exception e) { 78 newlog.error("加密异常!", e); 79 return null; 80 } 81 82 return new String(Base64.encode(result)); 83 } 84 85 /** 86 * 将传入的密文转换为明文 87 * 88 * @param str 密文字符串 89 * @return 解密后的明文 90 */ 91 public String decode(String str) { 92 byte[] result = null; 93 byte[] content = Base64.decode(str.getBytes()); 94 try { 95 KeyGenerator kgen = KeyGenerator.getInstance("AES"); 96 SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); 97 if (pwd == null) { 98 pwd = "ecology9"; 99 } 100 random.setSeed(pwd.getBytes()); 101 kgen.init(128, random); 102 SecretKey secretKey = kgen.generateKey(); 103 byte[] enCodeFormat = secretKey.getEncoded(); 104 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); 105 106 // 创建密码器 107 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 108 cipher.init(Cipher.DECRYPT_MODE, key); 109 result = cipher.doFinal(content); 110 111 } catch (Exception e) { 112 newlog.error("解密异常!", e); 113 return null; 114 } 115 116 return new String(result); 117 } 118 119 /** 120 * 设置加密和解密的密钥 121 * 122 * @param pwd 加密密钥 123 * @return true:成功,false:失败 124 */ 125 public boolean setPwd(String pwd) { 126 this.pwd = pwd; 127 return true; 128 } 129 130 /** 131 * 设置加密和解密的向量 132 * @param iv 加密向量 133 * @return true:成功,false:失败 134 */ 135 public boolean setIv(String iv) { 136 return true; 137 } 138 139 /** 140 * 主程序 141 * @param args 参数 142 */ 143 public static void main(String[] args) { 144 AES_Base64 aesBase64 = new AES_Base64(); 145 String content = "测试test"; 146 // AES的密钥长度最好是16位(不是必须) 147 String password = "ecology9"; 148 aesBase64.setPwd(password); 149 // 加密 150 //System.out.println("加密前:" + content); 151 String encodeResultStr = aesBase64.encode(content); 152 //System.out.println("加密后:" + encodeResultStr); 153 //解密 154 String decodeResultStr = aesBase64.decode(encodeResultStr); 155 //System.out.println("解密后:" + decodeResultStr); 156 } 157 158 }
然后对应封装了一个PHP类:
PHP代码 class AES { //PKCS5Padding 补码方式 private function _pkcs5Pad($text, $blockSize) { $pad = $blockSize - (strlen($text) % $blockSize); return $text . str_repeat(chr($pad), $pad); } private function _pkcs5Unpad($text) { $end = substr($text, -1); $last = ord($end); $len = strlen($text) - $last; if(substr($text, $len) == str_repeat($end, $last)) { return substr($text, 0, $len); } return false; } //加密 public function encrypt($encrypt, $key) { $blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $paddedData = $this->_pkcs5Pad($encrypt, $blockSize); $ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($ivSize, MCRYPT_RAND); $key2 = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true),0,16); $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key2, $paddedData, MCRYPT_MODE_ECB, $iv); return base64_encode($encrypted); } //解密 public function decrypt($decrypt, $key) { $decoded = base64_decode($decrypt); $blockSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($blockSize, MCRYPT_RAND); $key2 = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16); $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key2, $decoded, MCRYPT_MODE_ECB, $iv); return $this->_pkcs5Unpad($decrypted); } }
参考:
https://blog.csdn.net/jzm1963173402/article/details/80155493【Java的SHA1PRING方式AES加密对应的PHP写法】