2011년 5월 19일 목요일

JAVA에서 RSA

자바에서 RSA를 쓰려면 먼저.. lib를 include해야한다.

다음에 제시된 주소로 접근하면 lib를 받을 수 있다.

http://www.bouncycastle.org/latest_releases.html


그다음에 다음 소스를 붙여넣기 그리고 Go

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class MainClass {
public static void main(String[] args) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
keyGenerator.init(128);
Key blowfishKey = keyGenerator.generateKey();

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.genKeyPair();

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());

byte[] blowfishKeyBytes = blowfishKey.getEncoded();
System.out.println(new String(blowfishKeyBytes));
byte[] cipherText = cipher.doFinal(blowfishKeyBytes);
System.out.println(new String(cipherText));
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());

byte[] decryptedKeyBytes = cipher.doFinal(cipherText);
System.out.println(new String(decryptedKeyBytes));
SecretKey newBlowfishKey = new SecretKeySpec(decryptedKeyBytes, "Blowfish");
}
}

댓글 없음:

댓글 쓰기