java key iv对称加密_java实现对称加密AES和DES的加密、解密

目前主流的加密方式有:1、对称加密:AES、DES      2、非对称加密:RSA、DSA。

本文主要讲解java实现调用AES/DES加密算法包,调用过程最精要的就是下面两句话:

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

cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);

其中,DES是采用的算法,CBC是工作模式,DES一共有电子密码本模式(ECB)、加密分组链接模式(CBC)、加密反馈模式(CFB)和输出反馈模式(OFB)四种模式,PKCS5Padding是填充模式,还有其它的填充模式。

而方法cipher.init();一共有三个参数:Cipher.ENCRYPT_MODE, key, zeroIv,Cipher.ENCRYPT_MODE是加密/解密工作模式,key是密钥(模式) zeroIv就是初始化向量。工作模式、填充模式、初始化向量这三种因素一个都不能少。否则,如果你不指定的话,那么就要程序就要调用默认实现。

下面分别实现对称加密/解密的代码。

加密:

加密方式: AES128(CBC/PKCS5Padding) + Base64,  私钥:lianghuilonglong,要加密的字符串:abcdefg。

实现如下:

public String encrypt(){

String text = "abcdefg"; //要加密的字符串

String key = "lianghuilonglong";  //私钥: AES固定格式为128/192/256 bits.即:16/24/32bytes。DES固定格式为128bits,即8bytes。

String iv = "aabbccddeeffgghh";  //初始化向量参数,AES 为16bytes. DES 为8bytes.

Key keySpec = new SecretKeySpec(key.getBytes(), "AES"); //两个参数,第一个为私钥字节数组, 第二个为加密方式 AES或者DES

IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes); // 初始化向量

Cipher cipher = Cipher.getIntance("AES/CBC/PKCS5Padding");  // 实例化加密类,参数为加密方式,要写全

cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);   // 初始化,此方法可以采用三种方式,按服务器要求来添加。

//(1)无第三个参数

//(2)第三个参数为SecureRandom random = new SecureRandom();中random对象,随机数。(AES不可采用这种方法)

//(3)采用此代码中的IVParameterSpec

//cipher.init(Cipher.ENCRYPT_MODE, keySpec);

//SecureRandom random = new SecureRandom();

//cipher.init(Cipher.ENCRYPT_MODE, keySpec, random);

byte [] b = cipher.doFinal(text.getBytes());//加密操作,返回加密后的字节数组,然后需要编码。主要编解码方式有Base64, HEX, UUE,7bit等等。

String ret = Base64.encode(b); //Base64、HEX等编解码

}

解密:

解密逻辑:将服务器返回的加密字符串,先用Base64、HEX等解码成byte[],再用加密时相同的加密方式及key进行解密。加密与解密代码几乎相同。唯一区别为在Cipher类init时,工作模式为Cipher.DECRYPT_MODE。

加密方式: AES128(CBC/PKCS5Padding) + Base64, 私钥:lianghuilonglong

代码实现:

public String deCiphering(){

String keySpec = "lianghuilonglong";

String textDeCipher = "UstyI8JoQOty8egSMFQfig=="; //从服务器返回的加密字符串,需要解密的字符串

byte [] byte = Base64.decode(textDeCipher);   //先用Base64解码

IvParaterSpec ivSpec = new IvParaterSpec("abcdefghabcdefgh".getBytes());

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

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

cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); //与加密时不同MODE:Cipher.DECRYPT_MODE

byte [] ret = cipher.doFinal(byte);

return new String(ret, "utf-8");

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenSSL 是一个开源的加密库,支持对称加密、非对称加密、哈希算法等多种加密方式。对称加密是指加密解密使用相同的密钥,常见的对称加密算法有 DES、3DESAES 等。 在 OpenSSL 中,使用 EVP 接口进行对称加密操作。下面是一个使用 AES-256-CBC 算法进行加密解密的示例代码: ```c #include <openssl/evp.h> #include <string.h> int aes_encrypt(const unsigned char *plaintext, int plaintext_len, const unsigned char *key, const unsigned char *iv, unsigned char *ciphertext) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; /* Create and initialise the context */ if(!(ctx = EVP_CIPHER_CTX_new())) return -1; /* Initialise the encryption operation. */ if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) return -1; /* Provide the message to be encrypted, and obtain the encrypted output. * EVP_EncryptUpdate can be called multiple times if necessary */ if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) return -1; ciphertext_len = len; /* Finalise the encryption. */ if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) return -1; ciphertext_len += len; /* Clean up */ EVP_CIPHER_CTX_free(ctx); return ciphertext_len; } int aes_decrypt(const unsigned char *ciphertext, int ciphertext_len, const unsigned char *key, const unsigned char *iv, unsigned char *plaintext) { EVP_CIPHER_CTX *ctx; int len; int plaintext_len; int ret; /* Create and initialise the context */ if(!(ctx = EVP_CIPHER_CTX_new())) return -1; /* Initialise the decryption operation. */ if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) return -1; /* Provide the message to be decrypted, and obtain the plaintext output. * EVP_DecryptUpdate can be called multiple times if necessary */ if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) return -1; plaintext_len = len; /* Finalise the decryption. */ ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len); if(ret > 0) { plaintext_len += len; } else { /* Error */ EVP_CIPHER_CTX_free(ctx); return -1; } /* Clean up */ EVP_CIPHER_CTX_free(ctx); return plaintext_len; } ``` 其中,`aes_encrypt` 函数用于加密,`aes_decrypt` 函数用于解密。`plaintext` 是明文,`plaintext_len` 是明文长度,`key` 是密钥,`iv` 是初始化向量,`ciphertext` 是密文,`ciphertext_len` 是密文长度,`plaintext` 是解密后的明文。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值