.NET与Java互通AES算法加密解密
/// <summary>AES加密</summary>
/// <param name="text">明文</param>
/// <param name="key">密钥,长度为16的字符串</param>
/// <param name="iv">偏移量,长度为16的字符串</param>
/// <returns>密文</returns>
public static string EncodeAES(string text, string key,string iv)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.Zeros;
rijndaelCipher.KeySize = 128;
rijndaelCipher.BlockSize = 128;
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[16];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = Encoding.UTF8.GetBytes(iv);
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte[] plainText = Encoding.UTF8.GetBytes(text);
byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
return Convert.ToBase64String(cipherBytes);
} /// <summary>AES解密</summary>
/// <param name="text">密文</param>
/// <param name="key">密钥,长度为16的字符串</param>
/// <param name="iv">偏移量,长度为16的字符串</param>
/// <returns>明文</returns>
public static string DecodeAES(string text, string key,string iv)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.Zeros;
rijndaelCipher.KeySize = 128;
rijndaelCipher.BlockSize = 128;
byte[] encryptedData = Convert.FromBase64String(text);
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[16];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = Encoding.UTF8.GetBytes(iv);
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(plainText);
}
上面代码为C# 需要引用System.Security.Cryptography命名空间
Java,需要以下引用:
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
另外需要对String和byte[]相互转换的类,我自己写的Base64Helper
/**
* @author miracle.qu
* @see AES算法加密明文
* @param data 明文
* @param key 密钥,长度16
* @param iv 偏移量,长度16
* @return 密文
*/
public static String encryptAES(String data,String key,String iv) throws Exception {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length; if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
} byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext); return Base64Helper.encode(encrypted).trim(); } catch (Exception e) {
e.printStackTrace();
return null;
}
} /**
* @author miracle.qu
* @see AES算法解密密文
* @param data 密文
* @param key 密钥,长度16
* @param iv 偏移量,长度16
* @return 明文
*/
public static String decryptAES(String data,String key,String iv) throws Exception {
try
{
byte[] encrypted1 = Base64Helper.decode(data); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString.trim();
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
其中Base64Helper类是个简单的类,引用:
import org.apache.commons.codec.binary.Base64;
/**
* 编码
* @param byteArray
* @return
*/
public static String encode(byte[] byteArray) {
return new String(new Base64().encode(byteArray));
} /**
* 解码
* @param base64EncodedString
* @return
*/
public static byte[] decode(String base64EncodedString) {
return new Base64().decode(base64EncodedString);
}
摘抄 :http://blog.csdn.net/mr_qu/article/details/8433370
https://en.wikipedia.org/wiki/Initialization_vector
.NET与Java互通AES算法加密解密的更多相关文章
- C#与Java互通AES算法加密解密
/// <summary>AES加密</summary> /// <param name="text">明文</param> /// ...
- .NET/android/java/iOS AES通用加密解密(修正安卓)
移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如.NET和android或者iOS的打交道.为了让数据交互更安全,我们需要对数据进行加密传输.今天研究了一下,把几种语言的加密都 ...
- .NET/android/java/iOS AES通用加密解密
移动端越来越火了,我们在开发过程中,总会碰到要和移动端打交道的场景,比如.NET和android或者iOS的打交道.为了让数据交互更安全,我们需要对数据进行加密传输.今天研究了一下,把几种语言的加密都 ...
- PHP实现sha1加密AES算法加密解密数据
一.加密代码如下: /** * * @param string $string 需要加密的字符串 * @param string $key 密钥 * @return string */ public ...
- JAVA实现AES的加密和解密算法
原文 JAVA实现AES的加密和解密算法 import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import ja ...
- Java对称与非对称加密解密,AES与RSA
加密技术可以分为对称与非对称两种. 对称加密,解密,即加密与解密用的是同一把秘钥,常用的对称加密技术有DES,AES等 而非对称技术,加密与解密用的是不同的秘钥,常用的非对称加密技术有RSA等 为什么 ...
- JAVA中AES对称加密和解密
AES对称加密和解密 package demo.security; import java.io.IOException; import java.io.UnsupportedEncodingExce ...
- ORACLE 字段AES算法加密、解密
ORACLE 字段AES算法加密.解密(解决中文乱码问题)2014年02月12日 17:13:37 华智互联 阅读数:97971.加解密函数入口 CREATE OR REPLACE FUNCTION ...
- PHP AES的加密解密
AES加密算法 密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DE ...
随机推荐
- [tmux] Customize tmux with tmux.conf
You can modify tmux's behavior with your tmux configuration file, .tmux.conf. You can use your tmux ...
- [tmux] Zoom and resize to view a particular pane within tmux
Sometimes you'll want to zoom in on a specific pane within a tmux window. We'll learn to use C-b z t ...
- 一步步学习Linux多任务编程
系统调用 01.什么是系统调用? 02.Linux系统调用之I/O操作(文件操作) 03.文件描述符的复制:dup(), dup2() 多进程实现多任务 04.进程的介绍 05.Linux可执行文件结 ...
- springMVC返回json数据乱码问题及@RequestMapping 详解
原文地址:https://blog.csdn.net/u010127245/article/details/51774074 一.@RequestMapping RequestMapping是一个用来 ...
- 第六章:任务执行——Java并发编程实战
任务:通常是一些抽象的且离散的工作单元.大多数并发应用程序都是围绕"任务执行"来构造的,把程序的工作分给多个任务,可以简化程序的组织结构便于维护 一.在线程中执行任务 任务的独立性 ...
- java及jdbc与sql之间日期的转换
javaSE中主要为日期字符串和日期对象之间的转换 JDBC中主要是util中Date与sql中作为数据库中Date的转换 public class DateDemo { public static ...
- android --Activity生命周期具体解释
一. 再探Activity生命周期 为了研究activity的生命周期,简单測试代码例如以下. package com.example.testactivity; import android.app ...
- redis举例调用两种方式方式
在以下的代码演示样例中.将给出两种最为经常使用的Redis命令操作方式,既普通调用方式和基于管线的调用方式. 注:在阅读代码时请留意凝视. 1 #include <stdio.h> ...
- HDU 3605Escape(缩点+网络流之最大流)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3605 本来打算昨天写两道题的,结果这个题卡住了,最后才发现是最后的推断条件出错了,推断满流的条件应该是 ...
- QSettings提供了非常方便的注册表读写
Qt的QSettings提供了非常方便的注册表读写 写入: //写入注册表 void setRegValue( QString strPath, QString strItemName, QStrin ...