Dazed和困惑的Java安全& BouncyCastle API [英] Dazed and confused by Java Security & BouncyCastle APIs

查看:131
本文介绍了Dazed和困惑的Java安全& BouncyCastle API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图理解Java的BouncyCastle加密API。不幸的是,我发现Java密码学一般被服务提供商接口和术语遮盖,我无法围绕什么实际上做什么。我已经尝试重复阅读必要的文档,但它只是难以理解,引入了许多概念,远远超出了我需要的。

I've been trying to make sense of the BouncyCastle cryptography APIs for Java. Unfortunately, I'm finding Java cryptography in general to be so obscured by service provider interfaces and jargon that I can't wrap my head around what anything actually does. I've tried reading the necessary documentation repeatedly but it just stays incomprehensible, introducing many concepts far beyond what I think should be needed.

全部我真的想要一个类,执行以下操作:

All I really want is a class that does the following:

public class KeyPair {
    public byte[] public;
    public byte[] private;
}

public class RSACrypto {
    public static KeyPair generateRSAKeyPair() { /*implementation*/}
    public static byte[] encrypt(byte[] data, byte[] publicKey) { /*impl*/}
    public static byte[] decrypt(byte[] encryptedData, byte[] privateKey) { /*impl*/ }
}

如果这是一个令人难以置信的复杂问题,请问我真正想要的。任何指针在哪里读Java加密和BouncyCastle是非常欢迎。

Apologies if this is an incredibly complicated question to ask as "all I really want". Any pointers on where to read up on Java cryptography and BouncyCastle are very welcome. Any overviews of how the Java crypto systems actually are laid out are extremely welcome.

推荐答案

import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;

public class RSACrypto
{

  /* A 1024-bit key will encrypt messages up to 117 bytes long. */
  private static final int KEY_SIZE = 1024;

  private static final String XFORM = 
    "RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING";

  public static KeyPair generateRSAKeyPair()
    throws GeneralSecurityException
  {
    KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
    gen.initialize(KEY_SIZE);
    return gen.generateKeyPair();
  }

  public static byte[] encrypt(byte[] plaintext, PublicKey pub)
    throws GeneralSecurityException
  {
    Cipher cipher = Cipher.getInstance(XFORM);
    cipher.init(Cipher.ENCRYPT_MODE, pub);
    return cipher.doFinal(plaintext);
  }

  public static byte[] decrypt(byte[] ciphertext, PrivateKey pvt)
    throws GeneralSecurityException
  {
    Cipher cipher = Cipher.getInstance(XFORM);
    cipher.init(Cipher.DECRYPT_MODE, pvt);
    return cipher.doFinal(ciphertext);
  }

  public static void main(String... argv)
    throws Exception
  {
    KeyPair pair = RSACrypto.generateRSAKeyPair();
    byte[] plaintext = "A short secret message.".getBytes("UTF-8");
    byte[] ciphertext = RSACrypto.encrypt(plaintext, pair.getPublic());
    byte[] recovered = RSACrypto.decrypt(ciphertext, pair.getPrivate());
    System.out.println(new String(recovered, "UTF-8"));
  }

}

这篇关于Dazed和困惑的Java安全& BouncyCastle API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆