1
00:00:00.05 --> 00:00:02.06
- [Narrator] We're gonna start
our encryption operations

2
00:00:02.06 --> 00:00:04.04
with symmetric encryption.

3
00:00:04.04 --> 00:00:08.07
And in order to do that, we
need to create a shared key.

4
00:00:08.07 --> 00:00:10.02
Let's jump into our IVE

5
00:00:10.02 --> 00:00:13.08
and open up the crypto utilities project.

6
00:00:13.08 --> 00:00:15.07
And in here, in source main Java,

7
00:00:15.07 --> 00:00:19.06
I want to create a new package.

8
00:00:19.06 --> 00:00:21.02
And the package that we're
gonna create is going

9
00:00:21.02 --> 00:00:27.03
to be com.frankmoley.crypto.symmetric.

10
00:00:27.03 --> 00:00:29.03
Now, in that package,
we're going to create

11
00:00:29.03 --> 00:00:32.05
a new Java class file,
and we're gonna call that

12
00:00:32.05 --> 00:00:37.08
symmetric.encryption.utils.

13
00:00:37.08 --> 00:00:39.07
Now, to this class, we're gonna create

14
00:00:39.07 --> 00:00:41.04
a few data points here.

15
00:00:41.04 --> 00:00:43.04
The first thing we're
gonna do is private static

16
00:00:43.04 --> 00:00:48.06
final string, and we will
call that string AES,

17
00:00:48.06 --> 00:00:51.04
and we will set its value to simply AES.

18
00:00:51.04 --> 00:00:53.06
Now, I do this because I
don't like magic strings

19
00:00:53.06 --> 00:00:55.02
in my code, and really,

20
00:00:55.02 --> 00:00:58.03
that's just good programming practice.

21
00:00:58.03 --> 00:01:00.08
Now let's create a
method to actually create

22
00:01:00.08 --> 00:01:02.06
our secret key.

23
00:01:02.06 --> 00:01:09.01
So public static SecretKey create AES key.

24
00:01:09.01 --> 00:01:11.01
Now this is gonna throw an exception,

25
00:01:11.01 --> 00:01:12.06
and for the purposes of this course,

26
00:01:12.06 --> 00:01:15.05
we're not gonna actually
catch that exception.

27
00:01:15.05 --> 00:01:17.08
Let's go ahead and import that secret key

28
00:01:17.08 --> 00:01:20.04
from Javex.crypto.

29
00:01:20.04 --> 00:01:23.02
Now we're gonna ahead and create the key.

30
00:01:23.02 --> 00:01:25.00
The first thing that I
wanna do is I wanna use

31
00:01:25.00 --> 00:01:29.04
the secure random class
from Javex.security.

32
00:01:29.04 --> 00:01:32.01
And I'm gonna create a
new instance of that.

33
00:01:32.01 --> 00:01:35.03
And we're gonna use that
inner key generator here.

34
00:01:35.03 --> 00:01:37.09
So let's create a key generator,

35
00:01:37.09 --> 00:01:41.00
and we'll simply call it the same name.

36
00:01:41.00 --> 00:01:44.00
And we're gonna do a key
generator dot get instance,

37
00:01:44.00 --> 00:01:48.04
and we're gonna pass at our AES string.

38
00:01:48.04 --> 00:01:51.05
Now we need to actually
init our key generator,

39
00:01:51.05 --> 00:01:54.06
and we're gonna call
it init with a key size

40
00:01:54.06 --> 00:02:00.05
of 256 bit, and we're gonna
pass it in that secure random

41
00:02:00.05 --> 00:02:04.00
in order to seed the initialization.

42
00:02:04.00 --> 00:02:08.04
And now we will simply
return key generator

43
00:02:08.04 --> 00:02:10.05
dot generate key.

44
00:02:10.05 --> 00:02:12.06
That's all there is to it.

45
00:02:12.06 --> 00:02:15.03
Let's go ahead and create a
test now, so we can actually

46
00:02:15.03 --> 00:02:19.05
go through and run this thing.

47
00:02:19.05 --> 00:02:21.07
So we'll create that test.

48
00:02:21.07 --> 00:02:24.04
Now, in order to implement
our test, we're gonna

49
00:02:24.04 --> 00:02:28.05
simply create an instance of secret key,

50
00:02:28.05 --> 00:02:32.08
and we're gonna get it from
the symmetric encryption utils

51
00:02:32.08 --> 00:02:36.05
dot create AES key method.

52
00:02:36.05 --> 00:02:38.03
Now, because of the way that this works,

53
00:02:38.03 --> 00:02:44.09
we need to throw an exception
from our actual method here,

54
00:02:44.09 --> 00:02:48.08
and we will simply assert
not null on the key,

55
00:02:48.08 --> 00:02:50.07
and that's simply to
make sure that our J unit

56
00:02:50.07 --> 00:02:53.06
actually operates, but
what I'd also like to do

57
00:02:53.06 --> 00:02:57.09
is call a system dot out print
line, and we're gonna use

58
00:02:57.09 --> 00:03:02.02
a built in class called
the data type converter,

59
00:03:02.02 --> 00:03:05.06
and we are going to print a hex binary

60
00:03:05.06 --> 00:03:08.03
of the key dot get encoded.

61
00:03:08.03 --> 00:03:10.06
Now, this will get the biter
rate and convert it into

62
00:03:10.06 --> 00:03:13.06
a hex string, so that
we can actually see it.

63
00:03:13.06 --> 00:03:16.04
Let's go ahead and run our test.

64
00:03:16.04 --> 00:03:20.03
We'll let compilation continue.

65
00:03:20.03 --> 00:03:23.06
And our console window will pop up here.

66
00:03:23.06 --> 00:03:26.01
And you will see that not
only did our test pass,

67
00:03:26.01 --> 00:03:27.09
but we got a nice hex string

68
00:03:27.09 --> 00:03:30.07
that is the representation of our key.

69
00:03:30.07 --> 00:03:33.02
Now, because this project
has Maven built in,

70
00:03:33.02 --> 00:03:36.02
I want to show you that
we can pop open a terminal

71
00:03:36.02 --> 00:03:39.06
window to where our code
is actually located,

72
00:03:39.06 --> 00:03:43.00
and we can run a Maven clean verify,

73
00:03:43.00 --> 00:03:45.08
and it will actually do the same thing.

74
00:03:45.08 --> 00:03:48.00
And that's the reason we
brought Maven onto this,

75
00:03:48.00 --> 00:03:50.07
and you can definitely
do this if you don't want

76
00:03:50.07 --> 00:03:54.00
to run it in your IDE,
or if you're running this

77
00:03:54.00 --> 00:03:56.08
through VI instead of the traditional IDE,

78
00:03:56.08 --> 00:04:00.03
and I just wanted to show you
that in order to get going,

79
00:04:00.03 --> 00:04:02.06
but at this point, we've got an AES key,

80
00:04:02.06 --> 00:04:04.08
we validated that we can create it,

81
00:04:04.08 --> 00:04:06.08
so now it's time to encrypt some data.

