Mar 15, 2009
[Tiger] RSA 暗号の鍵を DB に格納する方法
RSA 暗号の公開鍵を DB に格納しようと苦心惨憺したときのメモ。 以下のサイトが非常に参考になった。多謝。
- Java Cryptgraphy
- http://tokyo.cool.ne.jp/hharuki/java/81crypt.html
- @//メモ - JavaSE RSA暗号
- http://hondou.homedns.org/pukiwiki/index.php?cmd=read&page=JavaSE%20RSA%B0%C5%B9%E6
コード
ここではテストのためファイルから公開鍵、秘密鍵を取得している。
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.mail.internet.MimeUtility;
...
public void test() throws Exception {
String data = "hogehogehogehoge";
// 公開鍵の読み込み
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
is = new FileInputStream(new File("./mykey.x509"));
int ch = 0;
while ((ch = is.read()) >= 0) {
baos.write(ch);
}
} finally {
if(is != null) {
is.close();
}
}
// 暗号化。
byte[] encrypted = this.encrypt(data.getBytes(), baos.toByteArray());
System.out.println("" + new String(encrypted));
// 秘密鍵の読み込み
baos = new ByteArrayOutputStream();
try {
is = new FileInputStream(new File("./mykey.pkcs8"));
ch = 0;
while ((ch = is.read()) >= 0) {
baos.write(ch);
}
} finally {
if(is != null) {
is.close();
}
}
// 復号化。
byte[] decrypted = this.decrypt(encrypted, baos.toByteArray());
System.out.println("" + new String(decrypted));
}
public byte[] encrypt(final byte[] text, final byte[] publicKey)
throws Exception {
PublicKey key = KeyFactory.getInstance("RSA").generatePublic(
new X509EncodedKeySpec(this.decodeBase64(publicKey)));
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CipherOutputStream cos = new CipherOutputStream(baos, cipher);
cos.write(text);
cos.close();
return baos.toByteArray();
}
public byte[] decrypt(final byte[] text, final byte[] privateKey)
throws Exception {
PrivateKey key = KeyFactory.getInstance("RSA").generatePrivate(
new PKCS8EncodedKeySpec(this.decodeBase64(privateKey)));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
CipherOutputStream cos = new CipherOutputStream(baos, cipher);
cos.write(text);
cos.close();
return baos.toByteArray();
}
protected byte[] decodeBase64(final byte[] text) throws MessagingException,
IOException {
InputStream is = MimeUtility.decode(new ByteArrayInputStream(text), "base64");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = 0;
while ((ch = is.read()) >= 0) {
baos.write(ch);
}
return baos.toByteArray();
}
鍵の作成
下記で作成した mykey.x509, mykey.pkcs8 がそれぞれ公開鍵、秘密鍵になる。
$ openssl genrsa -des 1024 > ./mykey.pem Generating RSA private key, 1024 bit long modulus ...............++++++ ............++++++ e is 65537 (0x10001) Enter pass phrase: Verifying - Enter pass phrase: $ openssl pkcs8 -topk8 -in ./mykey.pem -out ./mykey.pkcs8 -nocrypt Enter pass phrase for ./mykey.pem: $ openssl rsa -in ./mykey.pem -pubout -out ./mykey.x509 Enter pass phrase for ./mykey.pem: writing RSA key $ cp ./mykey.pkcs8 ./mykey.pkcs8.original $ vi ./mykey.pkcs8 $ diff ./mykey.pkcs8.original ./mykey.pkcs8 1d0 < -----BEGIN PRIVATE KEY----- 16d14 < -----END PRIVATE KEY----- $ cp ./mykey.x509 ./mykey.x509.original $ vi ./mykey.x509 $ diff ./mykey.x509.original ./mykey.x509 1d0 < -----BEGIN PUBLIC KEY----- 6d4 < -----END PUBLIC KEY----- $
Nov 08, 2008
[Tiger] Java SE のサポート終了ポリシーをメモ
2008/10/30 を以て Java SE 1.4 が EOSL を迎えた。 1 年後の 2009/10/30 には Java SE 5.0 までもが EOSL を迎える。 Java SE for Business も考慮しなくてはいけないかもしれない。
- Java SE & Java SE for Business Support Road Map
- http://java.sun.com/products/archive/eol.policy.html
- Sun End of Service Life (EOSL) Policy
- http://www.sun.com/service/eosl/index.jsp
- Java SE for Business
- http://www.sun.com/software/javaseforbusiness/
- Java SE for Business(日本語)
- http://jp.sun.com/products/software/javaseforbusiness/index.html
Apr 06, 2006
[Tiger] Java の Unicode 4 サポート
Java が Java 5.0 で Unicode 4 にバージョンアップしてたことを最近知った。 Unicode 4 は 1 文字 16bit では表現できないコード体系になっているらしい。 Java は 1 文字 16bit を前提としている言語になっている。 char は 16bit で確保されているし。 Unicode 4 のサポートは結構オオゴトな気がするのだが、どうなっているのだろう。 というわけで、少々調べてみることにした。
- JSR-000204 Unicode Supplementary Character Support (Final Release)
- http://jcp.org/aboutJava/communityprocess/final/jsr204/index.html
- The Java Language Specification
- http://java.sun.com/docs/books/jls/index.html
- JSR-000204 Unicode Supplementary Character Support (Proposed Final Draft)
- http://jcp.org/aboutJava/communityprocess/first/jsr204/index.html
- Java プラットフォームにおける補助文字のサポート
- http://java.sun.com/developer/technicalArticles/Intl/Supplementary/index_ja.html
- Unicode 4.0 の補助文字のサポート Supplementary Char
- http://www.javainthebox.net/laboratory/J2SE1.5/MiscAPI/SupplementaryChar/SupplementaryChar.html
Unicode 4 のコード体系
Unicode 3 までの Unicode は 1 文字 16bit で固定されていた。 Unicode 4 では、コード体系が下記の様に拡張されている。
- 0x0000 - 0xFFFF ・・・ BMP(Basic Multilingual Plane, 基本多言語面)
-
0x10000 - 0x1FFFFF ・・・ SMP(Supplementary Multilingual Plane, 補助的多言語面)
- 0x10000 - 0x1FFFF ・・・ Plane 1
- 0x20000 - 0x2FFFF ・・・ Plane 2
- 0x30000 - 0x3FFFF ・・・ Plane 3
- 0x40000 - 0x4FFFF ・・・ Plane 4
- 0x50000 - 0x5FFFF ・・・ Plane 5
- 0x60000 - 0x6FFFF ・・・ Plane 6
- 0x70000 - 0x7FFFF ・・・ Plane 7
- 0x80000 - 0x8FFFF ・・・ Plane 8
- 0x90000 - 0x9FFFF ・・・ Plane 9
- 0xA0000 - 0xAFFFF ・・・ Plane 10
- 0xB0000 - 0xBFFFF ・・・ Plane 11
- 0xC0000 - 0xCFFFF ・・・ Plane 12
- 0xD0000 - 0xDFFFF ・・・ Plane 13
- 0xE0000 - 0xEFFFF ・・・ Plane 14
- 0xF0000 - 0xFFFFF ・・・ Plane 15
- 0x100000 - 0x10FFFF ・・・ Plane 16
の辺りは覚えておいた方がよさそうか。
- Supplementary Character
- A Unicode encoded character having a supplementary code point.
- Supplementary Code Point
- A Unicode code point between U+10000 and U+10FFFF.
- Supplementary Planes
- Planes 1 through 16, consisting of the supplementary code points.
- Surrogate Character
- A misnomer. It would be an encoded character having a surrogate code point, which is impossible. Do not use this term.
- Surrogate Code Point
- A Unicode code point in the range U+D800 through U+DFFF. Reserved for use by UTF-16, where a pair of surrogate code units (a high surrogate followed by a low surrogate) “stand in” for a supplementary code point.
- Surrogate Pair
- A representation for a single abstract character that consists of a sequence of two 16-bit code units, where the first value of the pair is a high-surrogate code unit, and the second is a low-surrogate code unit. (See definition D27 in Section 3.8, Surrogates.)



