用java写一段加密函数
时间: 2023-03-14 07:01:23 浏览: 55
我可以为你提供一段示例代码,可以帮助你使用Java语言来编写加密函数:import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;public class AESEncryption {public static byte[] encrypt(String key, String data) throws Exception {byte[] keyBytes = key.getBytes();SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");cipher.init(Cipher.ENCRYPT_MODE, secretKey);byte[] encryptedData = cipher.doFinal(data.getBytes());return encryptedData;}}
相关问题
用Java 写段 RSA 加密算法
以下是使用Java实现RSA加密算法的示例代码:
```java
import java.math.BigInteger;
import java.util.Random;
public class RSA {
private BigInteger n, d, e;
private int bitlen = 1024;
public RSA() {
Random r = new Random();
BigInteger p = BigInteger.probablePrime(bitlen / 2, r);
BigInteger q = BigInteger.probablePrime(bitlen / 2, r);
n = p.multiply(q);
BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(bitlen / 2, r);
while (m.gcd(e).intValue() > 1) {
e = BigInteger.probablePrime(bitlen / 2, r);
}
d = e.modInverse(m);
}
public RSA(BigInteger e, BigInteger d, BigInteger n) {
this.e = e;
this.d = d;
this.n = n;
}
public synchronized BigInteger[] encrypt(byte[] message) {
BigInteger[] cipher = new BigInteger[message.length];
for (int i = 0; i < message.length; i++) {
cipher[i] = BigInteger.valueOf(message[i]).modPow(e, n);
}
return cipher;
}
public synchronized byte[] decrypt(BigInteger[] cipher) {
byte[] message = new byte[cipher.length];
for (int i = 0; i < message.length; i++) {
message[i] = cipher[i].modPow(d, n).byteValue();
}
return message;
}
public static void main(String[] args) {
RSA rsa = new RSA();
String text1 = "Hello World";
System.out.println("Original Text 1: " + text1);
//加密
byte[] plaintext1 = text1.getBytes();
BigInteger[] ciphertext1 = rsa.encrypt(plaintext1);
System.out.print("Ciphertext 1: ");
for (int i = 0; i < ciphertext1.length; i++) {
System.out.print(ciphertext1[i].toString() + " ");
}
System.out.println();
//解密
byte[] decrypted1 = rsa.decrypt(ciphertext1);
System.out.println("Decrypted Text 1: " + new String(decrypted1));
String text2 = "RSA is an asymmetric cryptographic algorithm";
System.out.println("Original Text 2: " + text2);
//加密
byte[] plaintext2 = text2.getBytes();
BigInteger[] ciphertext2 = rsa.encrypt(plaintext2);
System.out.print("Ciphertext 2: ");
for (int i = 0; i < ciphertext2.length; i++) {
System.out.print(ciphertext2[i].toString() + " ");
}
System.out.println();
//解密
byte[] decrypted2 = rsa.decrypt(ciphertext2);
System.out.println("Decrypted Text 2: " + new String(decrypted2));
}
}
```
上述代码实现了一个简单的RSA加密算法,包括密钥生成、加密和解密功能。在main函数中,我们加密了两个字符串并进行了解密。
麻烦写一段RSA加密PKCS1 JAVA代码
好的,以下是一个使用RSA加密PKCS1的JAVA代码示例,其中使用了Bouncy Castle库:
```java
import org.bouncycastle.crypto.AsymmetricBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.RSAEngine;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.util.PublicKeyFactory;
import org.bouncycastle.crypto.util.PrivateKeyFactory;
import org.bouncycastle.crypto.util.PKCS1Encoding;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Security;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.RSAPublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
public class RSAPKCS1Example {
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
// 生成RSA密钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGenerator.initialize(2048, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 获取公钥和私钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// 加密数据
byte[] data = "Hello, World!".getBytes("UTF-8");
byte[] encryptedData = encrypt(data, publicKey);
// 解密数据
byte[] decryptedData = decrypt(encryptedData, privateKey);
String decryptedText = new String(decryptedData, "UTF-8");
// 输出结果
System.out.println("原始数据:" + new String(data, "UTF-8"));
System.out.println("加密后数据:" + bytesToHex(encryptedData));
System.out.println("解密后数据:" + decryptedText);
}
public static byte[] encrypt(byte[] data, RSAPublicKey publicKey) throws Exception {
AsymmetricKeyParameter keyParameters = PublicKeyFactory.createKey(publicKey.getEncoded());
AsymmetricBlockCipher cipher = new PKCS1Encoding(new RSAEngine());
cipher.init(true, keyParameters);
return cipher.processBlock(data, 0, data.length);
}
public static byte[] decrypt(byte[] encryptedData, RSAPrivateKey privateKey) throws Exception {
AsymmetricKeyParameter keyParameters = PrivateKeyFactory.createKey(privateKey.getEncoded());
AsymmetricBlockCipher cipher = new PKCS1Encoding(new RSAEngine());
cipher.init(false, keyParameters);
return cipher.processBlock(encryptedData, 0, encryptedData.length);
}
public static String bytesToHex(byte[] data) {
StringBuilder builder = new StringBuilder();
for (byte b : data) {
builder.append(String.format("%02X", b));
}
return builder.toString();
}
}
```
在上面的示例中,我们使用了Bouncy Castle库来进行RSA加密和解密,并使用了PKCS1编码方式。首先,我们使用`KeyPairGenerator`生成一个2048位的RSA密钥对,并从中获取公钥和私钥。然后,我们将原始数据进行加密,并将加密后的数据存储在`encryptedData`数组中。最后,我们使用私钥对加密后的数据进行解密,并将解密后的数据存储在`decryptedData`数组中。注意,我们使用了一个辅助函数`bytesToHex`将字节数组转换为十六进制字符串,以便于输出结果。