You are on page 1of 4

The Secret Key Interface

http://www.herongyang.com/Cryptography/JCE-Secret-Key-Interface-javax-crypto-
SecretKey.html
What is a Secret Key? A secrete key is the key used in a symmetric encryption algorithm, where
the same key is used both the encryption process and the decryption process.
Known symmetric encryption algorithms:
Blowfish - he block cipher designed by Bruce !chneier.
"#! - he "igital #ncryption !tandard as described in $%&! &'B ()-*.
"#!ede - riple "#! #ncryption +"#!-#"#,.
he secrect key concept is supported in -"K through the -.# +-a/a .ryptography #0tension,
package. he first thing % want to learn in -.# is the 1a/a0.crypto.!ecretKey interface.
1a/a0.crypto.!ecretKey is an interface pro/iding a grouping point for /arious secret keys. %t
e0tents 1a/a.security.Key, and inherits 2 methods:
getAlgorithm+, - 3eturns the algorithm name used to generate the key.
get#ncoded+, - 3eturns the key as a byte array in its primary encoding format.
get$ormat+, - 3eturns the name of the primary encoding format of this key.
Last update: 2006.
--
http://www.herongyang.com/JDK/Secret-Key-Test-rogram-JceSecretKeyTest.html
he following sample program shows you how to generate a secret key, write it a file, and read it
back.
/**
* JceSecretKeyTest.java
* Copyright (c) 2002 by Dr. Herong ang
*/
i!port java.io.*"
i!port java.sec#rity.spec.*"
i!port java$.crypto.*"
i!port java$.crypto.spec.*"
c%ass JceSecretKeyTest &
p#b%ic static voi' !ain(String() a) &
i* (a.%ength+,) &
Syste!.o#t.print%n(-.sage/-)"
Syste!.o#t.print%n(-java JceSecretKeyTest 0eySi1e o#tp#t-
2- a%gorith!-)"
ret#rn"
3
int 0eySi1e 4 5nteger.parse5nt(a(0))"
String o#tp#t 4 a(6)"
String a%gorith! 4 a(2)" // 7%o8*ish9 D:S9 D:Se'e9 H!ac;D<
try &
8riteKey(0eySi1e9o#tp#t9a%gorith!)"
rea'Key(o#tp#t9a%gorith!)"
3 catch (:$ception e) &
Syste!.o#t.print%n(-:$ception/ -2e)"
ret#rn"
3
3
private static voi' 8riteKey(int 0eySi1e9 String o#tp#t9
String a%gorith!) thro8s :$ception &
Key=enerator 0g 4 Key=enerator.get5nstance(a%gorith!)"
0g.init(0eySi1e)"
Syste!.o#t.print%n()"
Syste!.o#t.print%n(-Key=enerator >bject 5n*o/ -)"
Syste!.o#t.print%n(-?%gorith! 4 -20g.get?%gorith!())"
Syste!.o#t.print%n(-@rovi'er 4 -20g.get@rovi'er())"
Syste!.o#t.print%n(-Key Si1e 4 -20eySi1e)"
Syste!.o#t.print%n(-toString 4 -20g.toString())"
SecretKey 0y 4 0g.generateKey()"
String *% 4 o#tp#t2-.0ey-"
Ai%e>#tp#tStrea! *os 4 ne8 Ai%e>#tp#tStrea!(*%)"
byte() 0b 4 0y.get:nco'e'()"
*os.8rite(0b)"
*os.c%ose()"
Syste!.o#t.print%n()"
Syste!.o#t.print%n(-SecretKey >bject 5n*o/ -)"
Syste!.o#t.print%n(-?%gorith! 4 -20y.get?%gorith!())"
Syste!.o#t.print%n(-Save' Ai%e 4 -2*%)"
Syste!.o#t.print%n(-Si1e 4 -20b.%ength)"
Syste!.o#t.print%n(-Aor!at 4 -20y.getAor!at())"
Syste!.o#t.print%n(-toString 4 -20y.toString())"
3
private static voi' rea'Key(String inp#t9 String a%gorith!)
thro8s :$ception &
String *% 4 inp#t2-.0ey-"
Ai%e5np#tStrea! *is 4 ne8 Ai%e5np#tStrea!(*%)"
int 0% 4 *is.avai%ab%e()"
byte() 0b 4 ne8 byte(0%)"
*is.rea'(0b)"
*is.c%ose()"
KeySpec 0s 4 n#%%"
SecretKey 0y 4 n#%%"
SecretKeyAactory 0* 4 n#%%"
i* (a%gorith!.eB#a%s5gnoreCase(-D:S-)) &
0s 4 ne8 D:SKeySpec(0b)"
0* 4 SecretKeyAactory.get5nstance(-D:S-)"
0y 4 0*.generateSecret(0s)"
3 e%se i* (a%gorith!.eB#a%s5gnoreCase(-D:Se'e-)) &
0s 4 ne8 D:Se'eKeySpec(0b)"
0* 4 SecretKeyAactory.get5nstance(-D:Se'e-)"
0y 4 0*.generateSecret(0s)"
3 e%se &
0s 4 ne8 SecretKeySpec(0b9a%gorith!)"
0y 4 ne8 SecretKeySpec(0b9a%gorith!)"
3
Syste!.o#t.print%n()"
Syste!.o#t.print%n(-KeySpec >bject 5n*o/ -)"
Syste!.o#t.print%n(-Save' Ai%e 4 -2*%)"
Syste!.o#t.print%n(-Cength 4 -20b.%ength)"
Syste!.o#t.print%n(-toString 4 -20s.toString())"
Syste!.o#t.print%n()"
Syste!.o#t.print%n(-SecretKey >bject 5n*o/ -)"
Syste!.o#t.print%n(-?%gorith! 4 -20y.get?%gorith!())"
Syste!.o#t.print%n(-toString 4 -20y.toString())"
3
3
4ere is the result of my first test. %t is done with -"K 5.(.5.
java Dcp . JceSecretKeyTest <E 0ey6 7%o8*ish
Key=enerator >bject 5n*o/
?%gorith! 4 7%o8*ish
@rovi'er 4 S#nJC: version 6.F2
Key Si1e 4 <E
toString 4 java$.crypto.Key=eneratorGaHcFHH
SecretKey >bject 5n*o/
?%gorith! 4 7%o8*ish
Save' Ai%e 4 0ey6.0ey
Si1e 4 I
Aor!at 4 J?K
toString 4 java$.crypto.spec.SecretKeySpecG2EH<020e
KeySpec >bject 5n*o/
Save' Ai%e 4 0ey6.0ey
Cength 4 I
toString 4 java$.crypto.spec.SecretKeySpecG2EH<020e
SecretKey >bject 5n*o/
?%gorith! 4 7%o8*ish
toString 4 java$.crypto.spec.SecretKeySpecG2EH<020e
he program seems to be working:
!ince % am not specifying the pro/ider name, the implementation of the Blowfish
algorithm pro/ided in the default security package was selected. 6f course, !un is the
pro/ider of the default security package.
he Blowfish key is only 7 bytes when 8encoded8 in 3AW format.
When importing the blowfish key back from the 7 raw bytes, !ecretKey!cep class is used
instead of !ecretKey$actory class.
%n the second test, % wants to try "#! algorithm:
java Dcp . JceSecretKeyTest <E 0ey2 D:S
Key=enerator >bject 5n*o/
?%gorith! 4 D:S
@rovi'er 4 S#nJC: version 6.F2
Key Si1e 4 <E
toString 4 java$.crypto.Key=eneratorGLLHb0H
SecretKey >bject 5n*o/
?%gorith! 4 D:S
Save' Ai%e 4 0ey2.0ey
Si1e 4 H
Aor!at 4 J?K
toString 4 co!.s#n.crypto.provi'er.D:SKeyG***eILH<
KeySpec >bject 5n*o/
Save' Ai%e 4 0ey2.0ey
Cength 4 H
toString 4 java$.crypto.spec.D:SKeySpecG60E0H2
SecretKey >bject 5n*o/
?%gorith! 4 D:S
toString 4 co!.s#n.crypto.provi'er.D:SKeyG***eILH<
6f course, you can continue testing with "#!ede and 4mac9":.
Last update: 2006.
--

You might also like