pkcs1解密 springboot_Java AES128加密解密 示例源码(AES/ECB/PKCS7Padding)

这篇博客介绍了如何在Java中实现AES128位ECB模式加解密,使用PKCS7填充,并提供了一个完整的示例代码。通过核心代码展示了如何设置密钥,初始化Cipher并进行加密解密操作。此外,还特别提到了在SpringBoot项目中可能的应用场景。
摘要由CSDN通过智能技术生成

【实例简介】

等同于 C# 的AES 加密解密方法ECB/PKCS7Padding,如下:

AesCryptoServiceProvider aes = new AesCryptoServiceProvider();

aes.Key = Encoding.Default.GetBytes(key);

aes.Mode = CipherMode.ECB;

aes.Padding = PaddingMode.PKCS7;

【实例截图】

【核心代码】

import java.security.Security;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;

import javax.crypto.Mac;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import javax.crypto.Cipher;

import javax.crypto.Mac;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import javax.crypto.Cipher;

import javax.crypto.Mac;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;/**

*

* @author Ajit

*

* AES 128 bit ECB PKCS7 padding example

*

*/

public class AES128ECBwithPKCS7 {

//add new bouncycastle ciphers

static {

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

}

public static void main(String[] args) {

// encryption key should be multiple of 16 character long

String key = "abcdefghijklmnop";

String data = "0123456789";

String encrypted = AES128ECBwithPKCS7.encrypt(data, key);

System.out.println("加密后数据: " encrypted);

String decrypted = AES128ECBwithPKCS7.decrypt(AES128ECBwithPKCS7.encrypt(data, key), key);

System.out.println("解密后数据: " decrypted);

}

/**

* encrypt input text

*

* @param input

* @param key

* @return

*/

public static String encrypt(String input, String key) {

byte[] crypted = null;

try {

SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");

cipher.init(Cipher.ENCRYPT_MODE, skey);

crypted = cipher.doFinal(input.getBytes());

} catch (Exception e) {

System.out.println(e.toString());

e.printStackTrace();

}

//return new String(Base64.encodeBase64(crypted));

return new String(Hex.encodeHex(crypted)).toUpperCase();

// return new String(Base64.encodeBase64(crypted));

}

/**

* decrypt input text

*

* @param input

* @param key

* @return

*/

public static String decrypt(String input, String key) {

byte[] output = null;

try {

SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");

cipher.init(Cipher.DECRYPT_MODE, skey);

// output = cipher.doFinal(Base64.decodeBase64(input));

output = cipher.doFinal(Hex.decodeHex(input.toCharArray()));

} catch (Exception e) {

System.out.println(e.toString());

}

return new String(output);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值