Java:如何使用Cipher类对字符串进行加密解密
来源:互联网 发布:awr sql statistics 编辑:程序博客网 时间:2024/05/22 00:31
原文地址
http://www.c-sharpcorner.com/UploadFile/433c33/working-with-cipher-class-in-java-for-achieving-cryptograph/
Introduction
In this article we are going to describe cryptographic data security using the cipher class of Java. I describe a Cipher class with its constructor or method details. And I give you an example; that's why you can easily understand its uses.
Encryption and Decryption
Encryption is the conversion of data into a form, called a cipher text, that cannot be easily understood by unauthorized people. Decryption is the process of converting encrypted data back into its original form, so it can be understood. In other words, cryptography and encryption is the process of transforming information (referred to as plaintext) using an algorithm (called a cipher) to make it unreadable to anyone except those possessing a special knowledge, usually referred to as a key. The result of the process is encrypted information (in cryptography, referred to as cipher text). The reverse process, i.e., to make the encrypted information readable again, is referred to as decryption.
Cipher class
In Java Cipher is a sprat class and this class is given in the javax.crypto package. This class is specially designed for encryption and decryption. It provides the functionality of a cryptographic cipher for encryption and decryption. It forms the core of the Java Cryptographic Extension (JCE) framework. In order to create a Cipher object, the application calls the Cipher's getInstance method, and passes the name of the requested transformation to it. Optionally, the name of a provider may be specified.
Constructor details:
This class has only one constructor and the constructor takes three arguments.
Syntax
protected Cipher(CipherSpi cipherSpi ,Provider provider,String transformation)
Argument summary
cipherSpi - the delegate
provider - the provider
transformation - the transformation
Example
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
public class EncryptionExample {
public static void main(String[] args) throws Exception {
// create a key generator based upon the Blowfish cipher
KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");
// create a key
SecretKey secretkey = keygenerator.generateKey();
// create a cipher based upon Blowfish
Cipher cipher = Cipher.getInstance("Blowfish");
// initialise cipher to with secret key
cipher.init(Cipher.ENCRYPT_MODE, secretkey);
// get the text to encrypt
String inputText = JOptionPane.showInputDialog("Input your message: ");
// encrypt message
byte[] encrypted = cipher.doFinal(inputText.getBytes());
// re-initialise the cipher to be in decrypt mode
cipher.init(Cipher.DECRYPT_MODE, secretkey);
// decrypt message
byte[] decrypted = cipher.doFinal(encrypted);
// and display the results
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
"Enter text for Encryption: " + new String(encrypted) + "\n"
+ "Decryted tag: " + new String(decrypted));
// end example
System.exit(0);
}
}
OUTPUT
cmd output
The following dialog box will appear:
Now you enter your message in the following dialog box:
The following dialog box show you the message in the encrypted form and decrypted form.
- Java:如何使用Cipher类对字符串进行加密解密
- 使用JAVA对字符串进行DES加密解密
- Java对字符串进行加密解密的工具类
- java Cipher加密、解密
- 使用Java Base64解密算对openssl的base64加密字符串进行解密
- 如何对字符串加密解密
- Java中对字符串进行加密和解密
- Java中对字符串进行加密和解密
- Java中对字符串进行加密和解密(转)
- Java中对字符串进行加密和解密(转)
- Java中对字符串进行加密和解密
- Java小案例——对字符串进行加密解密
- java 利用HashMap对字符串进行加密和解密
- Java cipher加密与解密
- 【Java】通过DES加密和解密工具,对字符串进行加密和解密操作
- 怎样对字符串进行加密/解密
- js对字符串进行加密和解密
- js 对字符串进行加密解密
- JS操作Cookie写入和读取实例代码
- php读写文件与读写数据库的效率比较分享
- Android类库
- 对自己大学的期望与目标
- 使用unity-tweak-tool进行更多的ui设置
- Java:如何使用Cipher类对字符串进行加密解密
- Android:关于Jar Mismatch, support-v4的问题
- input button 变手型
- 我对大学专业的期望与目标
- 项目经验分享
- 我的大学专业期望与目标
- Java:如何把SecretKey保存下来
- 对计算机专业学习的期望与目标
- Apache的配置