[转]JAVA AES 加密算法

本文转自:http://blog.sina.com.cn/s/blog_7c8eb1590100svr0.html

package com.siro.tools;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AESPlus {

    public static String encrypt(String strKey, String strIn) throws Exception {
        SecretKeySpec skeySpec = getKey(strKey);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(strIn.getBytes());

        return new BASE64Encoder().encode(encrypted);
    }

    public static String decrypt(String strKey, String strIn) throws Exception {
        SecretKeySpec skeySpec = getKey(strKey);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] encrypted1 = new BASE64Decoder().decodeBuffer(strIn);

        byte[] original = cipher.doFinal(encrypted1);
        String originalString = new String(original);
        return originalString;
    }

    private static SecretKeySpec getKey(String strKey) throws Exception {
        byte[] arrBTmp = strKey.getBytes();
        byte[] arrB = new byte[16]; // 创建一个空的16位字节数组(默认值为0)

        for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
            arrB[i] = arrBTmp[i];
        }

        SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES");

        return skeySpec;
    }

    public static void main(String[] args) throws Exception {
        String Code = "中文ABc123";
        String key = "1q2w3e4r";
        String codE;

        codE = AESPlus.encrypt(key, Code);

        System.out.println("原文:" + Code);
        System.out.println("密钥:" + key);
        System.out.println("密文:" + codE);
        System.out.println("解密:" + AESPlus.decrypt(key, codE));
    }
}

转载于:https://www.cnblogs.com/freeliver54/archive/2011/10/09/2203342.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/news/380176.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java中Scanner类中 next()与nextLine()的区别

问题&#xff1a;提示用户输入一个英文字符串或者要解密的字符串&#xff0c;然后通过扫描仪获取用户输入的字符串&#xff0c;经过加密或者解密后&#xff0c;把字符串输出。 import java.util.Scanner;public class Encryption {public static void main(String[] args) {Sca…

操作系统中的处理机调度调度_操作系统中的流程分类和调度

操作系统中的处理机调度调度处理 (Process) In the operating system, there are numerous task and application program run simultaneously. A program is stored in the hard disk or any other form of secondary storage. When the program is executed it must be loade…

NX机制及绕过策略-ret2libc

程序&#xff1a; 1.c #include <stdio.h> void exploit() {system("/bin/sh"); } void func() {char str[0x20];read(0,str,0x50); } int main() {func();return 0; }0x01 NX介绍 溢出攻击的本质在于冯诺依曼计算机模型对数据和代码没有明确区分这一先天性缺…

网站SEO策略的制定

在对一个网站做SEO的时候&#xff0c;SEO技术水平类似的人&#xff0c;营销效果可能天壤之别&#xff0c;这是因为网站SEO策略的制定的不同&#xff01;-----这个是最根本的。 SEO技术非常的简单&#xff0c;因为SEO不是去寻找搜索引擎的漏洞&#xff0c;而是根据搜索引…

Python | 程序从列表中删除范围内的所有元素

Given a list and we have to remove elements in a range from the list in Python. 给定一个列表&#xff0c;我们必须从Python中的列表中删除范围内的元素。 删除列表(开始索引&#xff0c;结束索引) (del list(start_index, end_index)) del() method is used to remove a…

面向对象 (接口 Interface)

1&#xff0c;面向对象(接口的概述及其特点) A:接口概述 从狭义的角度讲就是指java中的interface从广义的角度讲对外提供规则的都是接口 B:接口特点 a:接口用关键字interface表示 interface 接口名 {}b:类实现接口用implements表示 class 类名 implements 接口名 {}c:接口…

android unbound prefix

少一个命名空间加上就行了&#xff1a;xmlns:android"http://schemas.android.com/apk/res/android" 转载于:https://www.cnblogs.com/nizuimeiabc1/archive/2011/10/09/4254310.html

【竞赛题解】第22次CCF计算机软件能力认证 B

今天&#xff08;准确说是昨天&#xff0c;一下子就过12点了&#xff09;下午刚参加了CSP认证考试&#xff0c;大概是考了220&#xff08;前两题AC&#xff0c;第三题太折磨了懒得看了&#xff0c;后面两题各混了10分&#xff09;&#xff0c;唯一有点参与感的就是B题了&#x…

gbd调试64位程序关键

程序&#xff1a; 4.c&#xff1a; #include<stdio.h> void exploit() {system("/bin/sh"); } void main() {char buf[20];gets(buf); }编译&#xff1a; gcc -no-pie -fno-stack-protector -m64 -o 4.exe 4.cNX保护&#xff0c;栈数据不可执行 使用命令&…

C#全局鼠标键盘Hook (备查)

using System; using System.Collections.Generic; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace DCIEngine.FrameWork.Snap { /// <summary> /// 这个类可以让你得到一个在…

fcfs调度算法_FCFS:先来先服务调度算法

fcfs调度算法The FCFS, which stands for First Come First Serve Scheduling Algorithm, is a non-preemptive scheduling algorithm, which means that if a process once starts executing in the processor, then it cannot be preempted in between the processing. Thus,…

亲和数

Problem Description 古希腊数学家毕达哥拉斯在自然数研究中发现&#xff0c;220的所有真约数(即不是自身的约数)之和为&#xff1a; 1245101120224455110&#xff1d;284。 1* 220220&#xff1b;2* 110220&#xff1b;4* 55220&#xff1b;5* 44220&#xff1b;10*20220;…

转:JNI jstring与c++字符串类型转换函数

jstring与c字符串类型转换函数 jstring str2jstring(JNIEnv* env,const char* pat) {//定义java String类 strClassjclass strClass (env)->FindClass("Ljava/lang/String;");//获取String(byte[],String)的构造器,用于将本地byte[]数组转换为一个新Stringjmetho…

python字符串转浮点数_如何在Python中检查字符串是否为数字(浮点数)?

python字符串转浮点数Using python it is very to interconvert the datatypes of a variable. A string can be easily converted to an integer or a float. However, asserting a string to be a float is a task by itself. Python provides an option to assert if a stri…

nhibernate学习之三级联(Ternary Associations)篇

1) 学习目标通过进一步学习Nhibernate基础知识&#xff0c;掌握用Nhiberate实现对级联的支持&#xff0c;通过一个简单的用户角色权限系统来体验nhibernate对级联的强大支持。2&#xff09;开发环境和必要准备 开发环境为:windows 2003,Visual studio .Net 2005,Sql server 200…

【竞赛题解】Codeforces Round #715 (Div. 2) C

C. The Sports Festival 题意&#xff1a;对于给定的整型数组aaa&#xff0c;每次选择其中一个元素aia_iai​&#xff08;不能重复选择同一元素&#xff09;&#xff0c;每次计算已选择的元素的极差&#xff08;最大元素减最小元素的差&#xff09;&#xff0c;输出最后极差和…

C和汇编---sizeof运算符和strlen函数

sizeof sizeof是C语言的内置运算符&#xff0c;以字节为单位给出指定类型的大小。 程序&#xff1a; #include <stdio.h>int main(void) {int a8;int b sizeof(a);//printf("a占用字节%u\n",sizeof(a));printf("a占用字节%d\n",b);return 0; }反汇…

Java接口程序练习

题目&#xff1a; 编写一个接口程序&#xff0c;其中定义一个计算体积的方法。然后&#xff0c;在设计应用程序实现这个接口&#xff0c;分别计算矩形柱面体积和圆形柱面体积。 代码如下&#xff1a; import java.util.*;//导入扫描仪&#xff1b; public class clown {publi…

[原]Asp.net替换不同版本的Dll文件碰到的问题以及解决办法.

情景还原: 今天一个朋友说网站不能上传图片,我检查后发现一直卡住在上传页面,一直滚动,是个Fckeditor控件2.6.3的. 经过google以后得到的结论是图片上传成功,但是没有返回结果,在服务器上可以看到上传的图片. 说明是上传控件有问题,程序不能返回结果. 再google以后发现有人已经…

叠筐

Problem Description 需要的时候&#xff0c;就把一个个大小差一圈的筐叠上去&#xff0c;使得从上往下看时&#xff0c;边筐花色交错。这个工作现在要让计算机来完成&#xff0c;得看你的了。 Input 输入是一个个的三元组&#xff0c;分别是&#xff0c;外筐尺寸n&#xff…