JAVA与Ruby互通 AES加密解密代码,算法ECB/PKCS5PADDING

本文展示了使用Java和Ruby两种语言实现AES加密和解密的方法。在Java中,通过Apache Commons Lang和Java Cryptography Extension进行加密解密;在Ruby中,利用OpenSSL库完成相同操作。代码详细展示了如何设置密钥、初始化向量并进行加解密过程。
摘要由CSDN通过智能技术生成

JAVA加解密方法

import org.apache.commons.lang3.Validate;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


public class AesTools {
    public static final String KEY = "eyJhbGciOiJIUzUxMiJ9eyJhbGciOiJIUzUx";
    private static SecretKeySpec getKey() {
        byte[] arrBTemp = KEY.getBytes();
        byte[] arrB = new byte[16];
        for (int i = 0; i < arrBTemp.length && i < arrB.length; i++) {
            arrB[i] = arrBTemp[i];
        }
        SecretKeySpec keySpec = new SecretKeySpec(arrB, "AES");
        return keySpec;
    }
     // 加密
    public static String encrypt(String content) {
        try {
            Validate.notBlank(content);
            SecretKeySpec keySpec = getKey();
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv);
            byte[] encrypted = cipher.doFinal(content.getBytes());
            return new BASE64Encoder().encode(encrypted);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("encrypt error");
        }
    }

	//解密 
    public static String decrypt(String content) {
        try {
            Validate.notBlank(content);
            byte[] base64Str = new BASE64Decoder().decodeBuffer(content);
            SecretKeySpec keySpec = getKey();
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keySpec, iv);
            byte[] decryptByte =cipher.doFinal(base64Str);
            return new String(decryptByte);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("decrypt error");
        }

    }

    public static void main(String[] args) {
        System.out.println(encrypt("sdsdsfsdf"));
        System.out.println(decrypt("J34NoQqnqSIK10+RsBnXww=="));
    }
}

ruby加解密方法


require 'openssl'
require 'base64'

def encrypt_data(encryptStr)
key = "eyJhbGciOiJIUzUxMiJ9eyJhbGciOiJIUzUx"
iv =  "0102030405060708"
key = key
cipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
cipher.encrypt
cipher.key = key
cipher.iv = iv
text = cipher.update(encryptStr) + cipher.final
encrypted_text = Base64.strict_encode64(text)
 return encrypted_text
end

 

def decrypt_data(decryptStr)
key = "eyJhbGciOiJIUzUxMiJ9eyJhbGciOiJIUzUx"
iv =  "0102030405060708"
key = key
cipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
cipher.decrypt
cipher.key = key
cipher.iv = iv
text = cipher.update( Base64::strict_decode64(decryptStr)) + cipher.final
 
 return text
end
 
puts encrypt_data("sdsdsfsdf")
puts decrypt_data("J34NoQqnqSIK10+RsBnXww==")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值